5df1e2a8ec28ef952a0765af3b29894cd278656d
[openwrt/svn-archive/archive.git] / net / mac-to-devinfo / files / netsmap-to-devinfo
1 #!/bin/sh
2
3 MACTODEV=mac-to-devinfo
4 IPTODEV=ip-to-devinfo
5
6 usage() {
7 echo "$0: -i interferace -r ip-range [options]"
8 echo ""
9 echo "-r ip-range range to probe (CIDR, e.g. 192.168.2.0/24)"
10 echo "-i interface interface to use in scan"
11 echo "-t timeout time to rate for arp responses for device detection"
12 echo "-p port[,port...] command-separated lists of ports to scan for SIP responses"
13 echo "-c repeat_count Number of times to send each request (default 1)"
14 echo "-s sleepreq ms to wait between sending arp requests"
15 echo "-x Assume we're running on an Xcelerator IP"
16 echo "-h help"
17 }
18
19
20 while getopts "r:i:t:p:c:s:xh" param; do
21 case "$param" in
22 h)
23 usage
24 exit 1
25 ;;
26 r)
27 IPRANGE=$OPTARG
28 ;;
29 i)
30 INTERFACE=$OPTARG
31 ;;
32 t)
33 TIMEOUT=$OPTARG
34 ;;
35 p)
36 PORTARG=$OPTARG
37 ;;
38 c)
39 REPEATCOUNT=$OPTARG
40 ;;
41 s)
42 SLEEPREQ="-s $OPTARG"
43 ;;
44 x)
45 XIP=TRUE
46 ;;
47 ?|*)
48 usage
49 exit 1
50 break
51 ;;
52
53 esac
54 done
55
56 if [ "$IPRANGE" = "" ]; then
57 echo "Must specify an ip range"
58 usage
59 exit 1
60 fi
61
62 if [ "$INTERFACE" = "" ]; then
63 echo "Must speciy the interface"
64 usage
65 exit 1
66 fi
67
68 if [ "$TIMEOUT" = "" ]; then
69 TIMEOUT=20
70 usage
71 fi
72
73 unset PORTS
74
75 if [ -n "$PORTARG" ]; then
76 PORTLIST="$PORTARG"
77
78 if [ -z "$(echo $PORTLIST | grep ',')" ]; then
79 PORTS="$PORTLIST"
80 else
81 FIELD=1
82 oldcurport=""
83 curport="$(echo $PORTLIST | cut -f$FIELD -d, )"
84 while [ "$curport" != "$oldcurport" ]; do
85 PORTS="$curport $PORTS"
86 FIELD=$(($FIELD + 1))
87 oldcurport="$(echo $PORTLIST | cut -f$FIELD -d, )"
88 curport="$(echo $PORTLIST | cut -f$FIELD -d, )"
89 done
90 fi
91 fi
92
93
94 prep-devinfo
95
96 IFS='
97 '
98
99 if [ -n "$REPEATCOUNT" ]; then
100 REPEAT="-c $REPEATCOUNT"
101 fi
102
103 for line in $(sh -c "netdiscover -t $TIMEOUT -k -m -i $INTERFACE -r $IPRANGE $REPEAT $SLEEPREQ 2>/dev/null" | grep -E '[0-9]([0-9]?[0-9])?.[0-9]([0-9]?[0-9])?.[0-9]([0-9]?[0-9])?.[0-9]([0-9]?[0-9])?.[0-9]([0-9]?[0-9])?'); do
104 unset IFS
105 IP=$(echo $line | tr -s \ | cut -f1 -d\ )
106 MAC=$(echo $line | tr -s \ | cut -f2 -d\ )
107
108 if [ -n "$PORTS" ]; then
109 for port in $PORTS; do
110
111 if [ "$XIP" = "TRUE" ]; then
112 XIP_PORT="-x $port"
113 fi
114 for sip_device in $(smap -p $port $IP </dev/null | grep -E -v 'host.? scanned' | grep 'SIP enabled' | cut -f1 -d: ); do
115 if [ -x "$(which httping)" ]; then
116 if httping -G -q -o 401,200 -c 2 -h $sip_device; then
117 echo "$sip_device"" | ""$($MACTODEV $XIP_PORT $MAC)"" | ""http://$sip_device/"
118 continue
119 fi
120 fi
121 echo "$sip_device"" | ""$(MACTODEV $XIP_PORT $MAC)"" | "" - "
122 done
123 done
124 else
125 for sip_device in $(smap $IP </dev/null | grep -E -v 'host.? scanned' | grep 'SIP enabled' | cut -f1 -d: ); do
126 if [ -x "$(which httping)" ]; then
127 if httping -G -q -o 401,200 -c 2 -h $sip_device; then
128 echo "$sip_device"" | ""$($MACTODEV $MAC $XIP_PORT)"" | ""http://$sip_device/"
129 continue
130 fi
131 fi
132 echo "$sip_device"" | ""$($MACTODEV $MAC $XIP_PORT)"" | "" - "
133 done
134 fi
135 IFS='
136 '
137 done
138
139 unset IFS