randomize-mac 1.25 KB
Newer Older
Felix Eckhofer's avatar
Felix Eckhofer committed
1
2
3
4
5
6
7
8
#!/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)
#
9
10
11
12
13
# Note that this will fail for wifi if you immediately connect
# to another network instead of explicitly disconnecting from the
# current one. In this case the MAC set before network-manager
# started might be re-used (see init/randomize-mac.conf)!
#
Felix Eckhofer's avatar
Felix Eckhofer committed
14
15
16
# felix@tribut.de

LOGFILE="`mktemp`"
17
IFSTATE="`ip addr show dev \"$IFACE\" 2>>\"$LOGFILE\"`"
Felix Eckhofer's avatar
Felix Eckhofer committed
18

19
if [ -n "$IFSTATE" ]; then
20
21
22
	FAILED="0"
	PRESTATE="`ip addr show dev \"$IFACE\" up 2>>\"$LOGFILE\"`"

23
24
25
	[ -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
26

27
	if [ "$FAILED" = "1" ]; then
Felix Eckhofer's avatar
Felix Eckhofer committed
28
29
30
		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
31
32
	fi
else
Felix Eckhofer's avatar
Felix Eckhofer committed
33
	echo "Interface $IFACE does not exist, skipping..." | logger -t randomize-mac -p user.info
Felix Eckhofer's avatar
Felix Eckhofer committed
34
35
36
fi

rm -f "$LOGFILE"