znc: Enable parallel build
[openwrt/svn-archive/archive.git] / net / mac-to-devinfo / files / ip-to-devinfo
1 #!/bin/sh
2 # ip-to-devinfo - IP (through arping or ping and arp cache) to MAC IEEE and
3 # 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 ARP=arp
21 MACTODEV=mac-to-devinfo
22
23 usage() {
24 echo "Usage: $0 [-i interface] [-x port] IP-address"
25 }
26
27 if [ "$1" == "-i" ]; then
28 INTERFACE="$2"
29 if [ -z "$INTERFACE" ]; then
30 echo "-i without interface"
31 usage
32 exit 1
33 fi
34 shift
35 shift
36 fi
37
38 if [ "$1" == "-x" ]; then
39 PORT="-x $2"
40 if [ -z "$PORT" ]; then
41 echo "-x without the port"
42 usage
43 exit 1
44 fi
45 shift
46 shift
47 fi
48
49 IP="$1"
50 shift
51
52 if [ -z "$IP" ]; then
53 echo "Must specify IP address"
54 usage
55 exit 1
56 fi
57
58
59 if [ ! -x "$(which $ARP)" ]; then
60 do_arp() {
61 cat /proc/net/arp
62 }
63 ARPMACFIELD=4
64 else
65 do_arp() {
66 $ARP -n
67 }
68 ARPMACFIELD=3
69 fi
70
71 if [ -z "$INTERFACE" ]; then
72 ping -q -c 2 $IP >/dev/null
73 MAC=$(do_arp|grep "$IP "|tr -s \ | cut -f$ARPMACFIELD -d\ )
74 else
75 MAC=$(arping -f -c 5 -I $INTERFACE $IP | grep 'Unicast reply from' | cut -f2 -d \[ | cut -f1 -d\])
76 fi
77
78 $MACTODEV $PORT $MAC
79
80