Skip to content

Commit f77660e

Browse files
Merge pull request geekcomputers#491 from rizwansoaib/master
Create changemac.py
2 parents b061e69 + 3f16f0a commit f77660e

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

changemac.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Author- RIZWAN AHMAD
2+
3+
# Simple python Script to change mac address of linux generate random or enter mac address
4+
5+
6+
7+
8+
9+
10+
import random,os,base64
11+
from subprocess import PIPE, Popen
12+
#function for returning terminal command
13+
def cret(command):
14+
process = Popen(
15+
args=command,
16+
stdout=PIPE,
17+
shell=True
18+
)
19+
return process.communicate()[0]
20+
21+
#function for genrate mac address random
22+
def randmac():
23+
return [ 0x00, 0x16, 0x3e,
24+
random.randint(0x00, 0x7f),
25+
random.randint(0x00, 0xff),
26+
random.randint(0x00, 0xff) ]
27+
28+
def retrandmac(mac):
29+
return ':'.join(map(lambda x: "%02x" % x, mac))
30+
31+
32+
33+
34+
35+
print(" +-+-+-+ +-+-+-+-+-+-+-+")
36+
print(" |M|A|C| |c|h|a|n|g|e|r|")
37+
print(" +-+-+-+ +-+-+-+-+-+-+-+")
38+
#finding wireless interface name that should start with wl e.g.-wlan0,wlp3s0
39+
infname=cret('ifconfig -a | egrep "^[wl-wl]+" | sed "s/: .*//" | grep -v "lo"')
40+
#INTERFACE NAME 6 character so return 6 last character
41+
infname=infname[:6]
42+
infname=infname.decode('utf-8')
43+
#GETTING MAC Address from /sys/class/net/wlan0/address directory
44+
cmdgetmac=('cat /sys/class/net/'+infname+'/address')
45+
crrntmac=cret("cat /sys/class/net/"+infname+"/address")
46+
crrntmac=crrntmac.decode('utf-8')
47+
print("Your Current mac address = "+crrntmac+"\nEnter Option to change Your MAC:\n1. Enter MAC address manually \n2. Automatic Random MAC address")
48+
opt=int(input())
49+
50+
51+
52+
if opt==1:
53+
print("Please Enter Your New MAC address: \nExmple: 46:d2:f4:0c:2a:50")
54+
55+
newmac=input()
56+
print("Please wait changing mac address..................")
57+
58+
59+
#first turn off wifi
60+
cret('nmcli radio wifi off')
61+
62+
changemaccmd="sudo ip link set dev "+infname+" address "+newmac
63+
#executing command with new mac address
64+
cret(changemaccmd)
65+
#turning on wifi
66+
cret('nmcli radio wifi on')
67+
#GETTING MAC Address from /sys/class/net/wlan0/address directory
68+
cr=cret("cat /sys/class/net/"+infname+"/address")
69+
cr=cr.decode('utf-8')
70+
71+
72+
print("\nNow Your Current mac address = "+cr)
73+
74+
75+
elif opt==2:
76+
genmac=retrandmac(randmac())
77+
print("Please wait generating new mac address.....................")
78+
cret('nmcli radio wifi off')
79+
changemaccmd="sudo ip link set dev "+infname+" address "+genmac
80+
cret(changemaccmd)
81+
cret('nmcli radio wifi on')
82+
cr=cret("cat /sys/class/net/"+infname+"/address")
83+
cr=cr.decode('utf-8')
84+
print("Now Your Current mac address = "+cr)
85+
86+
else:
87+
print("You Have Selected wrong Option")
88+
89+
90+
91+
92+

0 commit comments

Comments
 (0)