-
Notifications
You must be signed in to change notification settings - Fork 0
/
mac
executable file
·196 lines (164 loc) · 5.39 KB
/
mac
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/bin/bash
APP_TITLE="MAC resolver 0.5 by [email protected], 2012-2022"
BASEDIR="${0%/*}"
DATADIR="$BASEDIR/mac_data" # customizable
DATABASE="$DATADIR/vendors.json" # customizable
TMPFILE="/tmp/ieee-macs"
URL_PRI="https://maclookup.app/downloads/json-database/get-db"
URL_ALT=""
ARPTABLE="/proc/net/arp"
DEBUG=false
FILTER=""
PATTERN_LEN=8
function print_license {
echo -e 'Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.'
quit
}
function quit {
if $DEBUG; then echo "* cleanup and quit...";fi
if ! $MINIMAL; then echo; fi;
if [ "$TMPFILE" != "" ]; then if [ -f $TMPFILE ]; then rm $TMPFILE; fi; fi
if ! $1; then false; fi
exit
}
function intro {
echo
echo -e "\e[1;37m$APP_TITLE\e[0m"
echo
}
function check_database {
if [ ! -f $DATABASE ]; then check_datadir;update_pri;fi
}
function check_datadir {
if [ ! -d $DATADIR ]; then # create datadir if not exist
echo -ne " * creating datadir... ";mkdir -p $DATADIR&&echo "[OK]"
if [ $? -ne 0 ]; then echo " * can't create directory $DATADIR";quit false;fi # check if can create dir
fi
touch $DATABASE
if [ ! -w $DATABASE ]; then echo " * can't write database file $DATABASE";quit false;fi # check if database is writable
}
function update_pri {
check_datadir
echo -ne " * updating MAC db... "
wget -q -O $DATABASE "$URL_PRI"&&echo "[OK]"||echo "[ERROR!]"
}
function print_changelog {
echo -e '
0.5 [2022-11-28]
- new sources
- JSON db
0.41 [2021-03-05]
- bugfix in normalize_mac
0.40 [2019-06-04]
- added Wireshark database
- rework of the db and engine
- input is converted and validated
- fixed datadir wtf
- help page changes
0,31 [2014-12-22]
- datadir hotfix (wtf)
0.30 [2012-10-29]
- minimal output
- database/datadir checks
0.2 [2012-03-30]
- automated ARP table resolver
0.1 [2012-03-12]
- 1st version
'
quit
}
function help {
echo -e "\tUsage:"
echo -e "\t$0 MAC [--option]"
echo -e "\t$0 --command"
echo
echo -e "\tMAC\t\txx:xx:xx:xx:xx:xx (at least first 3 segments), allowed delimiters are ':' or '-' or none"
echo -e "\t--minimal\tMinimal output, only vendor/company information (not applicable with --arptable)"
echo
echo -e "\tCommands (standalone, higher priority):"
echo -e "\t--arptable\tResolve all macs in arptable"
echo -e "\t--license"
echo -e "\t--changelog"
echo -e "\t--update\tUpdate database"
# echo -e "\e[0;90m\t--update-alt\tUpdate database (alternative source)\e[m\n"
echo
echo
quit
}
function commands {
for par in "$@"; do
CMD=`echo "$par"|awk -F "--" '{print $2}'|awk -F "=" '{print $1}'` #get command or options
# VAL=`echo "$par"|awk -F "--" '{print $2}'|awk -F "=" '{print $2}'` #get value, not used now
case $CMD in
licence|license)
ACTION="print_license"
;;
changelog)
ACTION="print_changelog"
;;
update)
ACTION="update_pri"
;;
arptable)
ACTION="check_arptable"
;;
minimal)
FILTER=".vendorName"
;;
esac
if $DEBUG; then echo "* command $CMD value $VAL";fi
done
}
function normalize_mac {
read RAW
echo $RAW|tr '[[:lower:]]' '[[:upper:]]'|sed 's/[:-]//g;s/[0-9A-F]\{2\}/&\:/g;s/\:$//'
}
function make_pattern {
# strip first ($1) characters of string
read RAW
echo ${RAW:0:$1}
}
function check_arptable {
check_database
cat $ARPTABLE|grep -v '00:00:00:00:00:00'|tail -n +2|awk '{print $1"\t"$4}'|sort|uniq>$TMPFILE
while read LINE; do
IP=$(cut -f1 <<< ${LINE})
MAC=$(cut -f2 <<< ${LINE}|normalize_mac)
PATTERN=$(echo "$MAC"|make_pattern $PATTERN_LEN)
VENDOR=`cat $DATABASE | jq -r 'map(select(.macPrefix | startswith("'$PATTERN'"))) | .[].vendorName'`
echo -e " * $MAC - $IP\t[$PATTERN]\t[$VENDOR]"
done < $TMPFILE
quit
}
function check_mac {
MAC="$1"
check_database
cat $DATABASE | jq -r 'map(select(.macPrefix | startswith("'$MAC'"))) | .[]'"$FILTER"''
if [ "$3" != "dontquit" ]; then quit;fi
}
########################## MAIN ####################################
commands $@ # get action
if ! $MINIMAL; then intro;fi
$ACTION # call action
if [ "$1" != "" ]; then
MAC=$(echo $1|normalize_mac)
INVALID=$(sed -E 's/[0-9A-F:]//g' <<< ${MAC})
if (( ${#INVALID} > 0 )); then echo " * $MAC is not valid MAC!"; quit false; fi
if (( ${#MAC} >=8 && ${#MAC} <= 17 )); then check_mac $MAC; else echo " * MAC pattern is too short or long!"; quit false; fi
fi
help # or if passed up to here, show help
quit