branch Attitude Adjustment packages
[12.09/packages.git] / net / mac-to-devinfo / files / netsmap-to-devinfo
1 #!/bin/sh
2 #    netsmap-to-devinfo - netdisovered then smap'ed IP addresses to  MAC IEEE 
3 #    and custom information
4 #    Copyright (C) 2009  Daniel Dickinson
5
6 #    This program is free software; you can redistribute it and/or modify
7 #    it under the terms of the GNU General Public License as published by
8 #    the Free Software Foundation; either version 2 of the License, or
9 #    (at your option) any later version.
10
11 #    This program is distributed in the hope that it will be useful,
12 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #    GNU General Public License for more details.
15
16 #    You should have received a copy of the GNU General Public License along
17 #    with this program; if not, write to the Free Software Foundation, Inc.,
18 #    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 MACTODEV=mac-to-devinfo
21 IPTODEV=ip-to-devinfo
22
23 usage() {
24     echo "$0: -i interferace -r ip-range [options]"
25     echo ""
26     echo "-r ip-range       range to probe (CIDR, e.g. 192.168.2.0/24)"
27     echo "-i interface      interface to use in scan"
28     echo "-t timeout        time to rate for arp responses for device detection"
29     echo "-p port[,port...] command-separated lists of ports to scan for SIP responses"
30     echo "-c repeat_count   Number of times to send each request (default 1)"
31     echo "-s sleepreq       ms to wait between sending arp requests"
32     echo "-x                Assume we're running on an Xcelerator IP"
33     echo "-h                help"
34 }
35
36
37 while getopts "r:i:t:p:c:s:xh" param; do
38     case "$param" in
39         h)
40             usage
41             exit 1
42             ;;
43         r)
44             IPRANGE=$OPTARG
45             ;;
46         i)
47             INTERFACE=$OPTARG
48             ;;
49         t)
50             TIMEOUT=$OPTARG
51             ;;
52         p)
53             PORTARG=$OPTARG
54             ;;
55         c)
56             REPEATCOUNT=$OPTARG
57             ;;
58         s)
59             SLEEPREQ="-s $OPTARG"
60             ;;
61         x)
62             XIP=TRUE
63             ;;
64         ?|*)
65             usage
66             exit 1
67             break
68             ;;
69
70     esac
71 done
72
73 if [ "$IPRANGE" = "" ]; then
74     echo "Must specify an ip range"
75     usage
76     exit 1
77 fi
78
79 if [ "$INTERFACE" = "" ]; then
80     echo "Must speciy the interface"
81     usage
82     exit 1
83 fi
84
85 if [ "$TIMEOUT" = "" ]; then
86     TIMEOUT=20
87     usage
88 fi
89
90 unset PORTS
91
92 if [ -n "$PORTARG" ]; then
93     PORTLIST="$PORTARG"
94     
95     if [ -z "$(echo $PORTLIST | grep ',')" ]; then
96         PORTS="$PORTLIST"
97     else
98         FIELD=1
99         oldcurport=""
100         curport="$(echo $PORTLIST | cut -f$FIELD -d, )"
101         while [ "$curport" != "$oldcurport" ]; do
102             PORTS="$curport $PORTS"
103             FIELD=$(($FIELD + 1))
104             oldcurport="$(echo $PORTLIST | cut -f$FIELD -d, )"
105             curport="$(echo $PORTLIST | cut -f$FIELD -d, )"
106         done
107     fi
108 fi
109
110
111 prep-devinfo
112
113 IFS='
114 '
115
116 if [ -n "$REPEATCOUNT" ]; then
117     REPEAT="-c $REPEATCOUNT"
118 fi
119
120 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
121     unset IFS
122     IP=$(echo $line | tr -s \  |  cut -f1 -d\  )
123     MAC=$(echo $line | tr -s \  |  cut -f2 -d\  )
124
125     if [ -n "$PORTS" ]; then
126         for port in $PORTS; do
127             
128             if [ "$XIP" = "TRUE" ]; then
129                 XIP_PORT="-x $port"
130             fi
131             for sip_device in $(smap -p $port $IP </dev/null | grep -E -v 'host.? scanned' | grep 'SIP enabled' | cut -f1 -d:  ); do
132                 if [ -x "$(which httping)" ]; then
133                     if httping -G -q -o 401,200 -c 2 -h $sip_device; then
134                         echo "$sip_device"" | ""$($MACTODEV $XIP_PORT $MAC)"" | ""http://$sip_device/"
135                         continue
136                     fi
137                 fi
138                 echo "$sip_device"" | ""$(MACTODEV $XIP_PORT $MAC)"" | "" - "
139             done
140         done
141     else
142         for sip_device in $(smap $IP </dev/null | grep -E -v 'host.? scanned' | grep 'SIP enabled' | cut -f1 -d:  ); do
143             if [ -x "$(which httping)" ]; then
144                 if httping -G -q -o 401,200 -c 2 -h $sip_device; then
145                     echo "$sip_device"" | ""$($MACTODEV $MAC $XIP_PORT)"" | ""http://$sip_device/"
146                     continue
147                 fi
148             fi
149             echo "$sip_device"" | ""$($MACTODEV $MAC $XIP_PORT)"" | "" - "
150         done
151     fi
152     IFS='
153 '
154 done
155
156 unset IFS