randomize-mac 956 Bytes
Newer Older
Felix Eckhofer's avatar
Felix Eckhofer committed
1
2
3
4
5
6
7
8
9
10
11
#!/bin/sh
# temporarily shut down an interface and randomize the mac,
# requires the macchanger tool.
#
# place in /etc/network/if-post-down.d to renew the mac everytime
# a network interface is brought down (recent versions of
# NetworkManager no longer support if-pre-up.d)
#
# felix@tribut.de

LOGFILE="`mktemp`"
12
IFSTATE="`ip addr show dev \"$IFACE\" 2>>\"$LOGFILE\"`"
Felix Eckhofer's avatar
Felix Eckhofer committed
13
14
15
PRESTATE="`ip addr show dev \"$IFACE\" up 2>>\"$LOGFILE\"`"
FAILED="0"

16
17
18
19
if [ -n "$IFSTATE" ]; then
	date >> "$LOGFILE"
	echo "Running randomize-mac for interface '$IFACE'" >> "$LOGFILE"
	echo >> "$LOGFILE"
Felix Eckhofer's avatar
Felix Eckhofer committed
20

21
22
23
	[ -n "$PRESTATE" ] && ip link set dev "$IFACE" down >>"$LOGFILE" 2>&1
	macchanger -A "$IFACE" >>"$LOGFILE" 2>&1 || FAILED=1
	[ -n "$PRESTATE" ] && ip link set dev "$IFACE" up >>"$LOGFILE" 2>&1
Felix Eckhofer's avatar
Felix Eckhofer committed
24

25
26
27
28
29
	if [ "$FAILED" = "1" ]; then
		cat "$LOGFILE" | mail -s "[`hostname`] macchanger failed" root
	fi
else
	echo "Interface $IFACE does not exist, skipping..." >&2
Felix Eckhofer's avatar
Felix Eckhofer committed
30
31
32
fi

rm -f "$LOGFILE"