-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchange-address.sh
executable file
·129 lines (115 loc) · 2.6 KB
/
change-address.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
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
#!/bin/bash
# ---------------------------------------------------------------------------
# "THE BEER-WARE LICENSE" (Revision 42):
# <jairomer@pm.me> wrote this file. As long as you retain this notice you
# can do whatever you want with this stuff. If we meet some day, and you
# think this stuff is worth it, you can buy me a beer in return.
# ---------------------------------------------------------------------------
#
# This utlity will change the MAC address of the interface indicated by the
# first parameter.
#
# Optionally, you can indicate a specific MAC address as second parameter.
#
# countdown $1: Starts a countdown.
# Paramenter: Time in seconds
function countdown {
c=$1
while [ $c -gt 0 ]
do
echo $c
sleep 1
c=$[$c-1]
done
}
# generateHexVal : Generate/Print a random hexadecimal value.
# Arguments : None, but requires shell with $RANDOM enabled.
function generateHexVal {
rand=$(echo $RANDOM % 16 | bc)
case "$rand" in
"10") hex="A"
echo $hex ;;
"11") hex="B"
echo $hex ;;
"12") hex="C"
echo $hex ;;
"13") hex="D"
echo $hex ;;
"14") hex="E"
echo $hex ;;
"15") hex="F"
echo $hex ;;
*) hex="$rand"
echo $hex ;;
esac
}
# generateRandomMac : Assemble/Print a random MAC address
# Arguments : None
function generateRandomMac {
let sep=0 # Separator loop.
let field=1 # Field loop.
mac="" # MAC address.
# A mac address is composed of 6 fiealds, 2 values each.
# XX:XX:XX:XX:XX:XX
# Generate the first 5 fields
while [ $field -lt 6 ]
do
# Create random field
while [ $sep -lt 2 ]
do
let sep=sep+1
# Get a random hexadecimal number.
hex="$(generateHexVal)"
mac="$mac$hex"
done
let sep=0
let field++
mac="$mac:"
done
let sep=0
# Generate 6th field without separator
while [ $sep -lt 2 ]
do
let sep++
hex="$(generateHexVal)"
mac="$mac$hex"
done
echo $mac
}
# we need to be root, ask for permissions.
if [ $EUID -ne 0 ]
then
sudo bash $0 $1 $2
exit
fi
# check that at least the first parameter is given.
if [ -z "$1" ]
then
echo 'supply interface to modify.'
echo
echo "Usage: $0 <interface> [<new mac>]"
exit
fi
# handle new mac.
# If provided, use that one.
# Otherwise generate a random mac.
if [ -z "$2" ]
then
mac="$(generateRandomMac)"
echo "new mac: $mac"
else
mac=$2
fi
IFACE=$1
# load new mac into selected interface.
sudo ifconfig $IFACE down
sudo ifconfig $IFACE hw ether $mac
sudo ifconfig $IFACE up
sudo ifconfig $IFACE | grep HW
# renew ip address for provided interface.
sudo dhclient -r -v $IFACE
countdown 3
sudo dhclient -v $IFACE
echo
echo done
exit