-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchangemac.py
85 lines (76 loc) · 2.29 KB
/
changemac.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# Python Script to change MAC Address
# Import Useful modules
import sys
import subprocess
import argparse
import random
import time
import re
# Function to get the interface name
def get_arguments():
# This will give user a neat CLI
parser = argparse.ArgumentParser()
# We need the interface name
parser.add_argument("-i", "--interface",
dest="interface",
help="Name of the interface. "
"Type ifconfig for more details.")
options = parser.parse_args()
# Check if interface was given
if options.interface:
return options.interface
else:
parser.error("[!] Invalid Syntax. "
"Use --help for more details.")
# Function to change the MAC Address
def change_mac(interface, new_mac_address):
# As explained above, these lines will
# execute these commands for us
subprocess.call(["sudo", "ifconfig", interface,
"down"])
subprocess.call(["sudo", "ifconfig", interface,
"hw", "ether", new_mac_address])
subprocess.call(["sudo", "ifconfig", interface,
"up"])
# Function to generate a random MAC Address
def get_random_mac_address():
characters = "0123456789abcdef"
random_mac_address = "00"
for i in range(5):
random_mac_address += ":" + \
random.choice(characters) \
+ random.choice(characters)
return random_mac_address
# Function to get the current MAC Address
# We will use it restore MAC address
# in case something goes wrong.
def get_current_mac(interface):
output = subprocess.check_output(["ifconfig",
interface])
return re.search("\w\w:\w\w:\w\w:\w\w:\w\w:\w\w",
str(output)).group(0)
# Driver Program
if __name__ == "__main__":
print("[* Welcome to MAC ADDRESS Changer *]")
print("[*] Press CTRL-C to QUIT")
# Change it to required value(in sec)
TIME_TO_WAIT = 60
interface = get_arguments()
current_mac = get_current_mac(interface)
try:
while True:
random_mac = get_random_mac_address()
change_mac(interface, random_mac)
new_mac_summary = subprocess.check_output(
["ifconfig", interface])
if random_mac in str(new_mac_summary):
print("\r[*] MAC Address Changed to",
random_mac,
end=" ")
sys.stdout.flush()
# Wait for a constant period of time
time.sleep(TIME_TO_WAIT)
except KeyboardInterrupt:
# Restore the MAC before quitting.
change_mac(interface, current_mac)
print("\n[+] Quitting Program...")