X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=find-port-for-hostname;h=81bf56a02fe479e0be3408274617ddb12d9c6c51;hb=8e5fcc0e9db472b21d3d21b0882a8c469b2f575d;hp=f9f8c892a29af18ef1ae63350b07c61d5b7e8f1f;hpb=c1d915b281bc9a5a9efb977ef2a4441aec56fe0b;p=dell-switch diff --git a/find-port-for-hostname b/find-port-for-hostname index f9f8c89..81bf56a 100755 --- a/find-port-for-hostname +++ b/find-port-for-hostname @@ -4,10 +4,26 @@ host=$1 test -z "$host" && echo "Usage: $0 hostname" && exit 1 -sudo id +sudo id >/dev/null -ping -q -c 1 $host -mac=`sudo arp -a | grep $host | cut -d' ' -f4 | sort -u` -grep -r -i $mac /dev/shm/snmp-mac-port/ | tee /dev/shm/port.$host | egrep -v '(23|24|48|49)$' # remove trunk ports +# hide physical and trunk ports (> 10 mac, port nr <= 49) +cat /dev/shm/snmp-mac-port/* | cut -d' ' -f1,4 | sort | uniq -c | awk '{ if ( $1 > 10 && $3 <= 49 ) print "/"$2":"$2 " .* " $3 }' > /dev/shm/trunk.regex -echo "# "`cat /dev/shm/port.$host | cut -d: -f2- | cut -d' ' -f1 | sort -u | wc -l`" switches have $host mac $mac" +ping -c 1 $host | grep from > /dev/shm/port.$host.ping +# did we got resolved IP address? +if grep '(' /dev/shm/port.$host.ping >/dev/null ; then + ip=`cat /dev/shm/port.$host.ping | cut -d' ' -f5 | tr -d '():'` + host=`cat /dev/shm/port.$host.ping | cut -d' ' -f4` +else + ip=`cat /dev/shm/port.$host.ping | cut -d' ' -f4 | tr -d ':'` +fi +mac=`sudo arp -a -n | grep "($ip)" | cut -d' ' -f4 | sort -u` +if [ -z "$mac" ] ; then + bro=`./bro-conn-ip-vlan-mac.sh $ip` + echo "# bro $bro" + mac=`echo $bro | cut -d' ' -f3` + test -z "$mac" && exit 1 +fi +grep -r -i $mac /dev/shm/snmp-mac-port/ | tee /dev/shm/port.$host.switch | grep --file /dev/shm/trunk.regex -v | cut -d: -f2- + +echo "# $host $ip $mac on "`cat /dev/shm/port.$host.switch | cut -d: -f2- | cut -d' ' -f1 | sort -u | wc -l`" switches"