关于CentOS7上配置LLDP的文章,网络上已经很多了。实现交换机端口的抓取,众多周知的有两种工具,一种是lldpad,另一种是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.16OS.............: LinuxPrefix.........: /usr/localC 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.......: embeddedReadline.......: noOptional features:SNMP support...: noCDP............: yesFDP............: yesEDP............: yesSONMP..........: yesLLDPMED........: yesDOT1...........: yesDOT3...........: yesCUSTOM.........: yesXML output.....: noOldies support.: noseccomp........: nolibbsd.........: noPrivilege separation:Enabled........: yesUser/group.....: _lldpd/_lldpdChroot.........: /usr/local/var/run/lldpdInstrumentation (for devs only):Sanitizers.....: noneCoverage.......: 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
如果一切顺利,执行 make 命令:
$ makeCC strlcpy.loCCLD libcompat.laCC log.lo
...
make[1]: Leaving directory `/home/bernat/src/lldpd-1.0.16'
接着执行 make install 完成安装:
$ make install
Making install in src/compat
...
make[1]: Leaving directory `/home/test/lldpd-1.0.16/build'
最后,执行一系列系统设置,这些步骤很琐碎,但很重要。
$ useradd _lldpd
脚本如下:
#!/bin/bashfunction show_info(){local se_dev=$1local 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 "$@"
执行结果:
如果使用的是 lldpad,不是 lldpd,可以使用以下脚本进行查询:
#!/bin/bashfunction set_dev(){
#set dev
for dev in `ls /sys/class/net/ |egrep 'e|p'`
doecho "enabling lldp for interface: $dev"lldptool set-lldp -i $dev adminStatus=rxtxlldptool -T -i $dev -V sysName enableTx=yeslldptool -T -i $dev -V portDesc enableTx=yeslldptool -T -i $dev -V sysDesc enableTx=yeslldptool -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.servicesystemctl enable lldpad.service
set_dev
}#show SW info
function show_info() {local se_dev=$1local 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}'`
doshow_info $up_dev
done
}function check_lldp() {
if [[ -n `lldptool -p` ]]; thenecho "lldp install success"
fi
}function main() {
if [[ $1 == "install" ]]; thenif [[ -f "/var/lib/lldpad/lldpad.conf" ]]; thenecho "lldp installed,exit!"elseins_lldpcheck_lldpfi
elif [[ $1 == "get" ]]; thenget_info
elseecho "install:install lldp
get:get lldp info"
fi
}
main "$@"
结果类似:
脚本来自文章 https://blog.csdn.net/BK_sys/article/details/88554593