通过tcpdump获取交换机信息shell脚本

#/bin/sh
PATH="/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/bin:/usr/bin"

url='http://xxx.xxxx.com/plugin/host/update-field/?api_key=24e718014ff2a464c1015f64c6b65dbb'
#eth0_ip=`ip addr |awk '/inet/&&/(eth0|em1|br0)/{gsub(/\/.*/,"");print $2;exit}'`
#eth0_if=`ifconfig |awk '/(eth0|em1)/{print $1}'`
eth0_ip=$(ip -o ad|awk '/inet/&&/(10|192).[0-9]/&&!/lo:/{gsub(/\/.*/,"");print $4}'|head -1)
eth0_if=$(ip -o ad|awk '/inet/&&/(10|192).[0-9]/{gsub(/\/.*/,"");print $2}'|tail -1)
send_to_ops(){
    curl --data "ip_addr=${eth0_ip}&name=$1&value=$2" $url
}

################### switch_info
cisco(){
        net=$1
        tcpdump -nn -v -i $net -s 1500 -c 1 'ether[20:2] == 0x2000' > /tmp/tmp_get_port &
        sleep 45
        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 "CISCO: "$switch > /tmp/tmp_get_port
        echo $port >> /tmp/tmp_get_port
        echo $vlan>> /tmp/tmp_get_port
}
h3c(){
        net=$1
        tcpdump -i $net ether proto 0x88cc -A -s0 -t -c 1 -v > /tmp/tmp_get_port &
        sleep 45
        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 "H3C: "$switch > /tmp/tmp_get_port
        echo $port >> /tmp/tmp_get_port
        echo $vlan>> /tmp/tmp_get_port
}

h3c $eth0_if
switch=`sed -n '1p' /tmp/tmp_get_port`
switch_port=`sed -n '2p' /tmp/tmp_get_port`
switch_vlan=`sed -n '3p' /tmp/tmp_get_port`

send_to_ops access_switch_physical_id  "$switch" 
send_to_ops access_switch_physical_port "$switch_port" 
send_to_ops access_switch_vlan "$switch_vlan" 


######################### eth1
ifconfig |grep "eth1"
if [ $? -eq 0 ];then
    eth1_ip=`ifconfig eth1|grep "inet addr" |head -1|awk '{print $2}'|awk -F':' '{print $2}'`
    #mac_eht1=`ifconfig eth1|head -1|awk '{print $5}'`
    send_to_ops eth1_ip "$eth1_ip"
fi