-
Notifications
You must be signed in to change notification settings - Fork 2
/
find-port-for-hostname
executable file
·31 lines (24 loc) · 1.09 KB
/
find-port-for-hostname
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/bin/sh -e
host=$1
test -z "$host" && echo "Usage: $0 hostname" && exit 1
sudo id >/dev/null
. ./shm-trunk.regex
# 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
# 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"