这个脚本在centos物理服务器上运行可以采集到服务器所连接的交换机信息和端口信息。利用了CDP和LLDP协议
我们线上是服务器安装完操作系统就会跑这个脚本并自动把这些信息采集到资产库里,便于以后更改机器网段做vlan自动划分
采集脚本内容如下:
#!/bin/sh function cisco(){ net=$1 tcpdump -nn -v -i $net -s 1500 -c 1 'ether[20:2] == 0x2000' > /tmp/tmp_get_port & sleep 60 killall -9 tcpdump && return switch=`egrep "Device-ID|Device-ID" /tmp/tmp_get_port|sed "s/'//g"|awk '{print $NF}'` port=`egrep "Port-ID|Port-ID" /tmp/tmp_get_port|sed "s/'//g"|awk '{print $NF}'` vlan=`grep "VLAN ID" /tmp/tmp_get_port|awk '{print $NF}'` echo $switch > /tmp/tmp_get_port echo $port >> /tmp/tmp_get_port echo $vlan>> /tmp/tmp_get_port } function h3c(){ net=$1 tcpdump -i $net ether proto 0x88cc -A -s0 -t -c 1 -v > /tmp/tmp_get_port & sleep 60 killall -9 tcpdump && cisco $net && return switch=`grep "System Name TLV" /tmp/tmp_get_port|sed -n '1p' |awk '{print $NF}'` port=`grep "Subtype Interface Name" /tmp/tmp_get_port|sed -n '1p' |awk '{print $NF}'` vlan=`grep "port vlan id" /tmp/tmp_get_port|sed -n '1p' |awk '{print $NF}'` echo $switch > /tmp/tmp_get_port echo $port >> /tmp/tmp_get_port echo $vlan>> /tmp/tmp_get_port } echo ""> /tmp/tmp_net_list netlist=`ifconfig|egrep -i "eth|em|p2p"|awk '{print $1}'|grep -v ":"` for net in `echo $netlist`;do h3c $net switch=`sed -n '1p' /tmp/tmp_get_port` port=`sed -n '2p' /tmp/tmp_get_port` vlan=`sed -n '3p' /tmp/tmp_get_port` echo $net" "$switch" "$port" "$vlan >> /tmp/tmp_net_list done