-
Notifications
You must be signed in to change notification settings - Fork 0
/
mac_query.sh
executable file
·67 lines (55 loc) · 1.86 KB
/
mac_query.sh
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/bash
die() {
echo '<?xml version="1.0"?>'
echo "<items>"
echo $@ | while read LINE
do
echo "<item uid=\"usageinfo $LINE\" arg=\"$LINE\">"
echo "<title>${LINE}</title>"
echo "<icon>icon.png</icon>"
echo "</item>"
done
exit 42
}
USAGE="usage: $0 MAC"
# because alfred doesn't have much of a path to search
PATH=/usr/local/bin:/usr/local/sbin:/opt/local/bin:/opt/local/sbin:$PATH
TMPFILE=/tmp/output
# we want case-insensitive matching
shopt -s nocasematch
# remove pending and trailing whitespace and replace other whitespace with *
QUERY=$(echo "$1" | sed -e 's/^[ \t]*//' -e 's/[ \t]*$//' -e 's/ /* /g')
# get the path to ipcalc, exit if not found
WGET=$(which wget)
ELINKS=$(which elinks)
AWK=$(which awk)
[[ -x $WGET ]] || die "Could not find wget in your path."
[[ -x $ELINKS ]] || die "Could not find elinks in your path."
[[ -x $AWK ]] || die "Could not find awk in your path."
MAC=$(echo $QUERY | perl -ne 'my $mac = $_; $mac =~ s|[^A-Za-z0-9]||g; $mac =~ s|([A-Za-z0-9]{6}).*|\1|; print $mac')
# basic MAC string test
[[ $MAC =~ ^[0-9A-Fa-f]{6} ]] || die $USAGE
echo '<?xml version="1.0"?>'
echo "<items>"
rm -f ${TMPFILE}
$WGET -O - http://standards.ieee.org/cgi-bin/ouisearch?$MAC 2>/dev/null | $ELINKS -dump | awk '/(hex)/,/^$/' | while read LINE
do
# skip blank lines
[[ -z $LINE ]] && continue
LINE=$(echo $LINE | sed 's| +| |g')
echo $LINE >> ${TMPFILE}
echo "<item uid=\"macinfo $MAC\" arg=\"$LINE\">"
echo "<title>${LINE}</title>"
echo "<icon>icon.png</icon>"
echo "</item>"
done
# if ${TMPFILE} is empty the script found no match
if [[ ! -f ${TMPFILE} ]]; then
echo "<item uid=\"macinfo $MAC\" arg=\"$MAC\">"
echo "<title>Info for $QUERY not found!</title>"
echo "<icon>icon.png</icon>"
echo "</item>"
fi
rm -f ${TMPFILE}
echo "</items>"
shopt -u nocasematch