-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMacAddressChanger.py
More file actions
56 lines (47 loc) · 1.98 KB
/
MacAddressChanger.py
File metadata and controls
56 lines (47 loc) · 1.98 KB
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
#!/usr/bin/env python
import subprocess
import argparse
import re
def get_arg():
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--interface", dest="interface", help="Interface to change its MAC address")
parser.add_argument("-m", "--mac", dest="new_mac", help="New MAC address")
option = parser.parse_args()
mac_format = re.search(r"\w\w:\w\w:\w\w:\w\w:\w\w:\w\w", str(option.new_mac))
ifconfig_result = subprocess.check_output(["ifconfig"])
interfaces = re.findall(r"\w+: ", str(ifconfig_result))
if not option.interface:
print("[-]Please specify interface, use --help for more info")
exit()
elif not option.new_mac:
print("[-]Please specify new mac, use --help for more info")
exit()
elif not mac_format:
print("[-]Please use correct mac format, use --help for more info")
exit()
elif ((str(option.interface) + ": ") and ("n" + str(option.interface) + ": ")) not in interfaces:
print("[-]Please use correct interface format, use --help for more info")
exit()
return option
def change_mac(interface, new_mac):
print("[+]Changing Mac address of " + interface + " to " + new_mac)
subprocess.call(["ifconfig", interface, "down"])
subprocess.call(["ifconfig", interface, "hw", "ether", new_mac])
subprocess.call(["ifconfig", interface, "up"])
def get_mac(interface):
ifconfig_result = subprocess.check_output(["ifconfig", interface])
mac_search = re.search(r"\w\w:\w\w:\w\w:\w\w:\w\w:\w\w", str(ifconfig_result))
if mac_search:
return mac_search.group(0)
else:
print("[-]Could not read Mac address")
exit()
options = get_arg()
current_mac = get_mac(options.interface)
print("Current MAC: " + str(current_mac))
change_mac(options.interface, options.new_mac)
current_mac = get_mac(options.interface)
if current_mac == options.new_mac:
print("[+]Mac Address changed to: " + current_mac)
else:
print("[-]Mac address did not changed")