automacially find trunk ports and exclude them
authorDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 22 Jun 2018 13:53:36 +0000 (15:53 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 22 Jun 2018 13:53:36 +0000 (15:53 +0200)
find-port-for-hostname

index 92d68e3..44fa015 100755 (executable)
@@ -6,10 +6,13 @@ test -z "$host" && echo "Usage: $0 hostname" && exit 1
 
 sudo id >/dev/null
 
+# 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
+
 ping -c 1 $host | grep from > /dev/shm/port.$host.ping
-ip=`cat /dev/shm/port.$host.ping | cut -d' ' -f5 | tr -d '()':`
+ip=`cat /dev/shm/port.$host.ping | cut -d' ' -f5 | tr -d '():'`
 host=`cat /dev/shm/port.$host.ping | cut -d' ' -f4`
 mac=`sudo arp -a -n | grep "($ip)" | cut -d' ' -f4 | sort -u`
-grep -r -i $mac /dev/shm/snmp-mac-port/ | tee /dev/shm/port.$host.switch | cut -d: -f2- | egrep -v '(23|24|48|49)$' # remove trunk ports
+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"