在python3中,有没有一种方法可以将根密码传递到kali-linux上的函数中
我构建了一个类来更改我的mac地址(不确定这是否是最干净的方式) 我想知道是否有一种方法可以存储根密码,这样它只会提示一次类似于使用接口的方式,而不是对每个命令使用sudo-S。旧mac故意留空。如果您能给我一些建议,让自己更专业一些,我们将不胜感激在python3中,有没有一种方法可以将根密码传递到kali-linux上的函数中,python,python-3.x,kali-linux,Python,Python 3.x,Kali Linux,我构建了一个类来更改我的mac地址(不确定这是否是最干净的方式) 我想知道是否有一种方法可以存储根密码,这样它只会提示一次类似于使用接口的方式,而不是对每个命令使用sudo-S。旧mac故意留空。如果您能给我一些建议,让自己更专业一些,我们将不胜感激 #!/usr/bin/env python3 import subprocess, optparse, getpass def get_arguments(): parser = optparse.OptionParser()
#!/usr/bin/env python3
import subprocess, optparse, getpass
def get_arguments():
parser = optparse.OptionParser()
parser.add_option("-i", "--interface", dest="interface", help="Interface to change MAC address eg. eth0 wlan0")
parser.add_option("-m", "--mac", dest="new_mac", help="New Mac Address")
(options, arguments) = parser.parse_args()
if not options.interface:
parser.error("Please specify an interface with -i or --interface use --help for more info")
elif not options.new_mac:
parser.error("Please specify an new mac with -m or --mac use --help for more info")
return options
def change_mac(interface, new_mac):
cmd1 = "ifconfig " + interface + " down"
cmd2 = "ifconfig " + interface + " hw" + " ether " + new_mac
cmd3 = "ifconfig " + interface + " up"
root_password = getpass.getpass("Enter Sudo password : ")
print(f"[+] Changing MAC address for " + interface + " to " + new_mac)
subprocess.call('echo {} | sudo -S {}'.format(root_password, cmd1), shell=True)
subprocess.call('echo {} | sudo -S {}'.format(root_password, cmd2), shell=True)
subprocess.call('echo {} | sudo -S {}'.format(root_password, cmd3), shell=True)
options = get_arguments()
change_mac(options.interface, options.new_mac)
解决了它看起来文档提供了足够的信息来回答我的问题,以及关于堆栈溢出的一些离题linux cli回答。伟大的社区
~z~编辑~
似乎有更好的方法可以使用getpass库捕获不会以明文显示的sudo密码,并在cli中传递参数您不希望或不需要在脚本中运行sudo。只需在不使用sudo的情况下运行命令,并使用sudo运行脚本。
#!/usr/bin/env python3
import subprocess, optparse, getpass
def get_arguments():
parser = optparse.OptionParser()
parser.add_option("-i", "--interface", dest="interface", help="Interface to change MAC address eg. eth0 wlan0")
parser.add_option("-m", "--mac", dest="new_mac", help="New Mac Address")
(options, arguments) = parser.parse_args()
if not options.interface:
parser.error("Please specify an interface with -i or --interface use --help for more info")
elif not options.new_mac:
parser.error("Please specify an new mac with -m or --mac use --help for more info")
return options
def change_mac(interface, new_mac):
cmd1 = "ifconfig " + interface + " down"
cmd2 = "ifconfig " + interface + " hw" + " ether " + new_mac
cmd3 = "ifconfig " + interface + " up"
root_password = getpass.getpass("Enter Sudo password : ")
print(f"[+] Changing MAC address for " + interface + " to " + new_mac)
subprocess.call('echo {} | sudo -S {}'.format(root_password, cmd1), shell=True)
subprocess.call('echo {} | sudo -S {}'.format(root_password, cmd2), shell=True)
subprocess.call('echo {} | sudo -S {}'.format(root_password, cmd3), shell=True)
options = get_arguments()
change_mac(options.interface, options.new_mac)