我正在编写一个python3.x程序,该程序每隔几分钟运行一次在Linux中更改MAC的命令(作为其各种功能之一)。我仔细检查过了,而且我已经安装了ifconfig,所以这不是问题所在。下面是让我感到悲哀的部分:
import sys
import subprocess
import argparse
import random
import time
import re
def macfunc():
def get_args():
#Get interface stuff
prsr = argparse.ArgumentParser()
prsr.add_argument("-i","--interface",dest="interface",help="Name of interface.")
options = prsr.parse_args()
if options.interface:
return options.interface
else:
prsr.error("syntax error")
def changer(interface, new_mac_address):
#Does the terminal commands for changing the MAC
subprocess.call(["sudo","ifconfig",interface,"down"])
subprocess.call(["sudo","ifconfig",interface,"hw","ether",new_mac_address])
subprocess.call(["sudo","ifconfig",interface,"up"])
def get_random_mac():
#Randomizes MAC
charset="0123456789abcdef"
random_mac="00"
for i in range(5):
random_mac += ":" + \
random.choice(charset) \
+ random.choice(charset)
return random_mac
def get_original(interface):
#Holds the current MAC for restoration purposes
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)
#Now let's do the magic and change the mac every 2 minutes
if __name__ == "__main__":
print("Initializing MAC scrambler. Generating new MAC every 2 minutes.")
sleeper=120
interface=get_args()
current_mac=get_original(interface)
try:
while True:
random_mac=get_random_mac()
change_mac(interface,random_mac)
new_mac_info=subprocess.check_output(["ifconfig",interface])
if random_mac in str(new_mac_info):
print("New MAC:",random_mac,end=" ")
sys.stdout.flush()
time.sleep(sleeper)
except KeyboardInterrupt:
change_mac(interface,current_mac)
print("Original MAC restored. Terminating scrambling.")
macfunc()每当我运行它时,我都会收到语法错误消息。我无论如何也找不出我错过了什么。这可能是我做了一些非常愚蠢的事情。帮助将是巨大的。
发布于 2020-11-08 01:43:53
我用了一种不同的方式,并达到了同样的预期结果!
import time
import subprocess
import random
import time
from getmac import get_mac_address as gma
#Look after the original MAC
original=(gma())
#Randomize a new address
charset="0123456789abcdef"
randommac="00"
for i in range(5):
randommac += ":" +\
random.choice(charset)\
+ random.choice(charset)
#do the terminal commands
def subproc():
subprocess.call(["sudo","ifconfig","wlp3s0","down"])
subprocess.call(["sudo","ifconfig","wlp3s0","hw","ether",randommac])
subprocess.call(["sudo","ifconfig","wlp3s0","up"])
subproc()
print("Your MAC has been cheesed. New MAC:" + randommac)
print("The new MAC will expire in 60 seconds and be reverted.")
print("KEEP THIS PROGRAM OPEN.")
time.sleep(60)
subprocess.call(["sudo","ifconfig","wlp3s0","down"])
subprocess.call(["sudo","ifconfig","wlp3s0","hw","ether",original])
subprocess.call(["sudo","ifconfig","wlp3s0","up"])
print("Old MAC restored:" + original)发布于 2020-11-08 00:10:44
首先,您没有使用适当的缩进。我建议您使用自己的函数,而不是在这类简单程序中使用argparse。而且,要获得当前的MAC地址,您可以简单地运行此命令cat /sys/class/net/{interface}/address,而不是使用re。
import subprocess
import random
import time
import sys
def elementAfter(lst,element):
try:
elementIndex = lst.index(element)
elementAfter = lst[elementIndex + 1]
return elementAfter
except ValueError:
return False
def change_mac(interface, new_mac_address):
#Does the terminal commands for changing the MAC
subprocess.call(["sudo","ifconfig",interface,"down"])
subprocess.call(["sudo","ifconfig",interface,"hw","ether",new_mac_address])
subprocess.call(["sudo","ifconfig",interface,"up"])
def get_random_mac():
#Randomizes MAC
charset="0123456789abcdef"
random_mac="00"
for i in range(5):
random_mac += ":" + \
random.choice(charset) \
+ random.choice(charset)
return random_mac
def get_original(interface):
#Holds the current MAC for restoration purposes
currentMac = subprocess.run(f'cat /sys/class/net/{interface}/address', shell=True, capture_output=True)
return currentMac.stdout.decode("utf-8").rstrip()
#Now let's do the magic and change the mac every 2 minutes
if __name__ == "__main__":
print("Initializing MAC scrambler. Generating new MAC every 2 minutes.")
sleeper=120
interface=elementAfter(sys.argv,"-i") or elementAfter(sys.argv,"--interface")
current_mac=get_original(interface)
try:
while True:
random_mac=get_random_mac()
change_mac(interface,random_mac)
new_mac_info=subprocess.check_output(["ifconfig",interface])
if random_mac in str(new_mac_info):
print("New MAC:",random_mac,end=" ")
sys.stdout.flush()
time.sleep(sleeper)
except KeyboardInterrupt:
change_mac(interface,current_mac)
print("Original MAC restored. Terminating scrambling.") https://stackoverflow.com/questions/64729507
复制相似问题