randomize-mac 1 KB
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
if [ -n "$IFSTATE" ]; then
15
16
17
	FAILED="0"
	PRESTATE="`ip addr show dev \"$IFACE\" up 2>>\"$LOGFILE\"`"

18
19
20
	[ -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
21

22
	if [ "$FAILED" = "1" ]; then
Felix Eckhofer's avatar
Felix Eckhofer committed
23
24
25
		echo "ERROR while trying to randomize MAC for $IFACE" | cat - "$LOGFILE" | logger -t randomize-mac -p user.err
	else
		echo "MAC successfully randomized for $IFACE" | logger -t randomize-mac -p user.notice
26
27
	fi
else
Felix Eckhofer's avatar
Felix Eckhofer committed
28
	echo "Interface $IFACE does not exist, skipping..." | logger -t randomize-mac -p user.info
Felix Eckhofer's avatar
Felix Eckhofer committed
29
30
31
fi

rm -f "$LOGFILE"