-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathmac_address_changer_linux.py
60 lines (51 loc) · 2.2 KB
/
mac_address_changer_linux.py
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
import subprocess
import string
import random
import re
def get_random_mac_address():
"""Generate and return a MAC address in the format of Linux"""
# get the hexdigits uppercased
uppercased_hexdigits = ''.join(set(string.hexdigits.upper()))
# 2nd character must be 0, 2, 4, 6, 8, A, C, or E
mac = ""
for i in range(6):
for j in range(2):
if i == 0:
mac += random.choice("02468ACE")
else:
mac += random.choice(uppercased_hexdigits)
mac += ":"
return mac.strip(":")
def get_current_mac_address(iface):
# use the ifconfig command to get the interface details, including the MAC address
output = subprocess.check_output(f"ifconfig {iface}", shell=True).decode()
return re.search("ether (.+) ", output).group().split()[1].strip()
def change_mac_address(iface, new_mac_address):
# disable the network interface
subprocess.check_output(f"ifconfig {iface} down", shell=True)
# change the MAC
subprocess.check_output(f"ifconfig {iface} hw ether {new_mac_address}", shell=True)
# enable the network interface again
subprocess.check_output(f"ifconfig {iface} up", shell=True)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Python Mac Changer on Linux")
parser.add_argument("interface", help="The network interface name on Linux")
parser.add_argument("-r", "--random", action="store_true", help="Whether to generate a random MAC address")
parser.add_argument("-m", "--mac", help="The new MAC you want to change to")
args = parser.parse_args()
iface = args.interface
if args.random:
# if random parameter is set, generate a random MAC
new_mac_address = get_random_mac_address()
elif args.mac:
# if mac is set, use it instead
new_mac_address = args.mac
# get the current MAC address
old_mac_address = get_current_mac_address(iface)
print("[*] Old MAC address:", old_mac_address)
# change the MAC address
change_mac_address(iface, new_mac_address)
# check if it's really changed
new_mac_address = get_current_mac_address(iface)
print("[+] New MAC address:", new_mac_address)