• CentOS7源码安装 lldpd 并附查询脚本


        关于CentOS7上配置LLDP的文章,网络上已经很多了。实现交换机端口的抓取,众多周知的有两种工具,一种是lldpad,另一种是lldpd。

    源码安装 lldpd

        在 Fedora 和 RHEL 8,使用 dnf install lldpd 命令安装 lldpd。对于较旧的发行版,如 RHEL 7,需要在输入 yum install lldpd 命令之前安装 epel-release 包。
        安装 lldpd 包后,可以通过 /etc/sysconfig/lldpd 文件更改配置。
        首先,打开下载页面 https://lldpd.github.io/,单击最新版本进行源码包下载。
    在这里插入图片描述
        按照命令安装:

    $ tar zxvf lldpd-1.0.16.tar.gz
    $ cd lldpd-1.0.16
    $ mkdir build
    $ cd build
    $ ../configure
    checking for a BSD-compatible install... /bin/install -c
    checking whether build environment is sane... yes
    checking for a race-free mkdir -p... /bin/mkdir -p
    checking for gawk... gawk
    ...
    ------------------ Summary ------------------
     lldpd version 1.0.16
      OS.............: Linux
      Prefix.........: /usr/local
      C Compiler.....: gcc -std=gnu11  -fdiagnostics-show-option -fdiagnostics-color=auto -pipe -Wall -W -Wextra -Wformat -Wformat-security -Wfatal-errors -Winline -Wpointer-arith -fno-omit-frame-pointer -Wno-cast-align -Wno-unused-parameter -Wno-missing-field-initializers -Wno-sign-compare -fstack-protector -fstack-protector-all -fstack-protector-strong -fstack-clash-protection  -D_FORTIFY_SOURCE=2 -D_GNU_SOURCE -g -O2 
      Linker.........: /bin/ld -m elf_x86_64  -Wl,-z,relro -Wl,-z,now   
      Libevent.......: embedded
      Readline.......: no
    
     Optional features:
      SNMP support...: no
      CDP............: yes
      FDP............: yes
      EDP............: yes
      SONMP..........: yes
      LLDPMED........: yes
      DOT1...........: yes
      DOT3...........: yes
      CUSTOM.........: yes
      XML output.....: no
      Oldies support.: no
      seccomp........: no
      libbsd.........: no
    
     Privilege separation:
      Enabled........: yes
      User/group.....: _lldpd/_lldpd
      Chroot.........: /usr/local/var/run/lldpd
    
     Instrumentation (for devs only):
      Sanitizers.....: none
      Coverage.......: no
    ------------- Compiler version --------------
    gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)
    Copyright (C) 2015 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    -------------- Linker version ---------------
    GNU ld version 2.27-34.base.el7
    Copyright (C) 2016 Free Software Foundation, Inc.
    This program is free software; you may redistribute it under the terms of
    the GNU General Public License version 3 or (at your option) a later version.
    This program has absolutely no warranty.
    ---------------------------------------------
    
    Check the above options and compile with:
     make
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

        如果一切顺利,执行 make 命令:

    $ make
      CC     strlcpy.lo
      CCLD   libcompat.la
      CC     log.lo
    ...
    make[1]: Leaving directory `/home/bernat/src/lldpd-1.0.16'
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

        接着执行 make install 完成安装:

    $ make install
    Making install in src/compat
    ...
    make[1]: Leaving directory `/home/test/lldpd-1.0.16/build'
    
    • 1
    • 2
    • 3
    • 4

        最后,执行一系列系统设置,这些步骤很琐碎,但很重要。

    • 确保在系统中正确注册了 liblldpctl 库。在 make install 的输出中有 liblldpctl 库的安装过程:
      在这里插入图片描述
      在Linux上,使用 ldconfig 命令重载库,使系统可以马上使用。
    • 创建 _lldpd 用户和 _ldpd 组,满足特权分离(提高安全性)的需要。
    $ useradd _lldpd
    
    • 1
    • 创建空目录 /usr/local/var/run/lldpd,不然服务无法启动哦。
    查询交换机端口的 lldpd 脚本

        脚本如下:

    #!/bin/bash
    
    function show_info(){
        local se_dev=$1
        local ld_tool="lldpcli show neighbors ports $1 details"
        local sw_name=`$ld_tool |grep 'SysName'|awk '{print $NF}'` 
        local sw_mac=`$ld_tool |grep 'mac'|awk '{print $NF}'` 
        local sw_ifname=`$ld_tool |grep 'ifname'|awk '{print $NF}'` 
        local sw_vlan=`$ld_tool |grep 'VLAN'|awk '{print $2}'|sed 's/,//'` 
    
        echo "se_dev: $se_dev
    sw_name: $sw_name
    sw_mac: $sw_mac
    sw_ifname: $sw_ifname
    sw_vlan: $sw_vlan
    "
    }
    
    function get_info(){
    #set dev
    
    #查询所有网口
    #for dev in `ls /sys/class/net/ |egrep 'e|p'`
    
    #查询当前up的端口
    for dev in `ip a |grep 'state UP' |awk -F ': ' '{print $2}'`
    do 
        show_info $dev
    done
    }
    
    function main() {
      get_info
    }
    main "$@"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

        执行结果:
    在这里插入图片描述

    查询交换机端口的 lldpad 脚本

        如果使用的是 lldpad,不是 lldpd,可以使用以下脚本进行查询:

    #!/bin/bash
    
    function set_dev(){
    #set dev
    for dev in `ls /sys/class/net/ |egrep 'e|p'`
    do
        echo "enabling lldp for interface: $dev"
        lldptool set-lldp -i $dev adminStatus=rxtx
        lldptool -T -i $dev -V sysName enableTx=yes
        lldptool -T -i $dev -V portDesc enableTx=yes
        lldptool -T -i $dev -V sysDesc enableTx=yes
        lldptool -T -i $dev -V mngAddr enableTx=yes
    done
    }
    
    function ins_lldp(){
    #installation lldpad and lldptool
    yum install -y lldpad & >/dev/null
    
    #Start service
    systemctl start lldpad.service
    
    systemctl enable lldpad.service
    set_dev
    }
    
    #show SW info
    function show_info() {
        local se_dev=$1
        local ld_tool="lldptool -t -n -i $se_dev"
        local sw_mac=`$ld_tool |grep 'MAC:' |awk -F ': ' '{print $NF}'`
        local sw_If=`$ld_tool |grep 'Ifname:' |awk -F ': ' '{print $NF}'`
        local sw_name=`$ld_tool |grep 'System Name TLV' -A1 |tail -n1 |sed 's/\t//g'`
        local sw_ip=`$ld_tool |grep 'Management Address TLV' -A1 |tail -n1 |awk -F ': ' '{print $NF}' |sed 's/\t//g'`
        echo "se_dev: $se_dev
    sw_name: $sw_name
    sw_ip: $sw_ip
    sw_mac: $sw_mac
    sw_If: $sw_If
    "
    }
    
    function get_info() {
    set_dev >/dev/null 2>&1
    sleep 3
    for up_dev in `ip a |grep 'state UP' |awk -F ': ' '{print $2}'`
    do
        show_info $up_dev
    done
    }
    
    function check_lldp() {
    if [[ -n `lldptool -p` ]]; then
        echo "lldp install success"
    fi
    }
    
    function main() {
    if [[ $1 == "install" ]]; then
        if [[ -f "/var/lib/lldpad/lldpad.conf" ]]; then
            echo "lldp installed,exit!"
        else
            ins_lldp
            check_lldp
        fi
    elif [[ $1 == "get" ]]; then
        get_info
    else
        echo "install:install lldp
    get:get lldp info"
    fi
    }
    main "$@"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73

        结果类似:
    在这里插入图片描述

        脚本来自文章 https://blog.csdn.net/BK_sys/article/details/88554593

  • 相关阅读:
    Hive的开窗函数和Rank
    shell并发遍历目录并对文件进行处理
    Java实现整数互转罗马数字基本算法
    Linux扩展篇之Shell编程二(运算符和条件判断)
    LeetCode-104. Maximum Depth of Binary Tree [C++][Java]
    Centos系统安装Yapi(传统方式安装)
    11、JavaWeb启程——会话跟踪技术Cookie&Session
    APP兼容性测试的关键点在哪?
    java 连接SSH工具操作服务器 (构建者模式+Util类) 分享
    http+nunjucks模板引擎、koa+nunjucks模板引擎
  • 原文地址:https://blog.csdn.net/zsx0728/article/details/127878225