-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgen_random_valid_mac
More file actions
executable file
·58 lines (50 loc) · 1.48 KB
/
gen_random_valid_mac
File metadata and controls
executable file
·58 lines (50 loc) · 1.48 KB
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
#!/bin/bash
# @author sdrfnord
# @license GPLv3+
# Generate random but valid mac address for certain vendors.
link_type="$1"
## https://en.wikipedia.org/wiki/List_of_networking_hardware_vendors
if [ "$link_type" == "wlan" ]
then
vendors='(IntelCor)'
elif [ "$link_type" == "lan" ] || [ -z "$link_type" ]
then
vendors='(IntelCor|HewlettP|Fujitsu)'
else
vendors="($link_type)"
fi
wireshark_manuf_file="/usr/share/wireshark/manuf"
ethers_file="$HOME/.wireshark/ethers"
## MAC address generation {{{
if [ ! -r "$wireshark_manuf_file" ]
then
wireshark_manuf_file="$(locate --regexp 'wireshark/manuf$' --limit 1)"
if [ ! -r "$wireshark_manuf_file" ]
then
echo "manuf file not found." 1>&2
exit 1
fi
fi
rand_mac_line="$(grep --invert-match '^#' "$wireshark_manuf_file" | \
grep --extended-regexp --ignore-case "\s${vendors}\s" \
| shuf -n 1)"
rand_mac_manf_part=${rand_mac_line:0:8}
rand_mac_random_part="`openssl rand -hex 3 | sed 's/\(..\)/\1:/g; s/.$//'`"
rand_mac="${rand_mac_manf_part}:${rand_mac_random_part}"
## }}}
## Check if the MAC address is already known. {{{
if [ ! -r "$ethers_file" ] && [ -r "/etc/ethers" ]
then
ethers_file="/etc/ethers"
fi
if [ -r "$ethers_file" ]
then
if grep --ignore-case "$rand_mac" "$ethers_file"
then
echo "MAC ($rand_mac) is already known!!! See ${ethers_file}." 2>&1
exit 1
fi
fi
## }}}
echo "Manufacture which uses this MAC address: ${rand_mac_line}."
echo "Random MAC: ${rand_mac^^}"