Skip to content

Commit fc85ed9

Browse files
authored
Update MACchange.sh
added (MAJOR): 1. Better macchanger installation check 2. Additional mac randomization options to user 3. support wlan and ethernet (as opposed to wlan only) 4. check connectivity before randomization 5. option to revert back to original mac 6. store logs and minor ui interface improvement
1 parent 63e8352 commit fc85ed9

File tree

1 file changed

+66
-64
lines changed

1 file changed

+66
-64
lines changed

MACchange.sh

Lines changed: 66 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,80 @@
1-
#! /bin/bash
1+
#!/bin/bash
22

3-
echo "What type of network do you wish to randomise?"
3+
#check for root privileges
4+
if [ "$EUID" -ne 0 ]; then
5+
echo "This script must be run by a user with sudo privileges."
6+
exit 1
7+
fi
8+
9+
#check if macchanger is installed (or not)
10+
if ! command -v macchanger &> /dev/null; then
11+
echo "Macchanger binary not found. Installing..."
12+
sudo apt-get update -y && sudo apt-get install macchanger -y
13+
fi
14+
15+
echo "What type of network do you wish to randomize?"
416
echo " "
517
echo "#1. eth"
618
echo "#2. wlan"
719
echo " "
820
read input
921

10-
# Check for root privileges
11-
# $EUID is an env variable. Contains the id-value of user currently utilising the script.
12-
if [ "$EUID" -ne 0 ]; then
13-
echo "This script must be run by an user with sudo privileges (root works but is generally not recommended)."
14-
exit 1
15-
fi
16-
macchanger_bincheck=`which macchanger | grep -o macchanger > /dev/null && echo 0 || echo 1`
17-
if ["$macchnager_bincheck" -ne 1]; then
18-
echo "Mac changer binary not detected! Running Automated Installation process..."
19-
sudo apt-get update -y && sudo apt-get install macchanger | yes
20-
fi
22+
#list all the available interfaces and get user-input
23+
if [[ $input == 1 ]]; then
24+
interfaces=$(ip link show | grep eth | awk '{print $2}' | tr -d ':')
25+
echo "Available Ethernet interfaces: $interfaces"
26+
read -p "Enter the Ethernet interface you want to randomize: " type_eth
2127

22-
# Checking user input.
23-
if [[ $input == 1 ]]
24-
then
25-
echo " "
26-
echo "Enter the ethernet device you want to randomise: (e.g. eth0 [or] eth1 [or] eth2)"
27-
echo " "
28-
read type_eth
29-
30-
command1=`ifconfig | grep $type_eth`
28+
# Validate interface
29+
if ! ifconfig $type_eth &> /dev/null; then
30+
echo "$type_eth not found. Please check the device name."
31+
exit 1
32+
fi
3133

32-
# Checking if the returned output from 'command1' has any info about -
33-
# the existence of eth connection. If the string is empty, then user is shown to the basic troubleshoot.
34-
if [ -z "$command1" ];
35-
then
36-
echo "No eth0 connection! Failed to randomise MAC of eth0."
37-
echo " "
38-
echo "Troubleshooting: "
39-
echo "1. Please try entering an existing ethernet connection (i.e. check your input)"
40-
echo "2. Please try checking the connection of ethernet cable to your computer/VM is established."
41-
else
42-
# Random mac could be implemented by utilizing env variable $RANDOM
43-
mac1="00:11:22:33:44:55"
34+
#randomize the MAC address using macchanger
35+
echo "Randomizing MAC for $type_eth..."
36+
sudo macchanger -r $type_eth
37+
echo "Randomized MAC for $type_eth"
38+
39+
else
40+
interfaces=$(ip link show | grep wlan | awk '{print $2}' | tr -d ':')
41+
echo "Available WLAN interfaces: $interfaces"
42+
read -p "Enter the WLAN interface you want to randomize: " type_wlan
4443

45-
sudo ifconfig $type_eth down
46-
sudo ifconfig $type_eth hw ether $mac1
47-
sudo ifconfig $type_eth up
48-
echo " "
49-
echo "Changed MAC of $type_eth ."
50-
echo "Current MAC of $type_eth is: $mac1"
51-
fi
44+
#Validate interface
45+
if ! ifconfig $type_wlan &> /dev/null; then
46+
echo "$type_wlan not found. Please check the device name."
47+
exit 1
48+
fi
5249

53-
else
54-
echo " "
55-
echo "Enter the wlan/NIC device you want to randomise: (e.g. wlan0 [or] wlan1 [or] wlan2)"
56-
echo " "
57-
read type_wlan
58-
echo " "
50+
#randomize MAC address using macchanger
51+
echo "Randomizing MAC for $type_wlan..."
52+
sudo macchanger -r $type_wlan
53+
echo "Randomized MAC for $type_wlan"
54+
fi
5955

60-
command2=`ifconfig | grep $type_wlan`
56+
#check global network connectivity after MAC change (ping to google dns for verif.)
57+
echo "Checking network connectivity..."
58+
if ! ping -c 3 8.8.8.8 &> /dev/null; then
59+
echo "Network is not reachable after changing MAC address. Check your connection."
60+
else
61+
echo "Network is reachable."
62+
fi
6163

62-
# The same for this part of else-code. (except it is for wlan interface)
63-
if [ -z "$command2" ];
64-
then
65-
echo "wlan0/NIC Adapter not detected. Failed to randomise MAC of wlan0."
66-
echo " "
67-
echo "Troubleshooting: "
68-
echo "1. Please try entering an existing NIC (i.e. check your input)"
69-
echo "2. Please try checking the connection of NIC to your computer/VM is established."
70-
echo " "
71-
else
72-
echo "MAC Change (wlan0) status: "
73-
echo " "
74-
sudo macchanger -r $type_wlan
75-
echo " "
76-
echo "Changed MAC of NIC/$type_wlan"
77-
fi
64+
#offer user to revert to original/previous MAC address
65+
read -p "Would you like to revert to the original MAC address? (y/n): " revert
66+
if [ "$revert" == "y" ]; then
67+
if [[ $input == 1 ]]; then
68+
sudo macchanger -p $type_eth
69+
echo "Original MAC restored for $type_eth."
70+
else
71+
sudo macchanger -p $type_wlan
72+
echo "Original MAC restored for $type_wlan."
73+
fi
7874
fi
75+
76+
#log the MAC change event to a log file
77+
log_file="/var/log/mac_randomizer.log"
78+
echo "$(date) - Changed MAC for $type_eth/$type_wlan" >> $log_file
79+
80+
echo "Script finished."

0 commit comments

Comments
 (0)