[英]Is there a way to pass root password into functions on kali linux in python3
I built a class to change my mac address (not sure if this is the cleanest way to do so)我构建了一个类来更改我的 mac 地址(不确定这是否是最干净的方法)
#!/usr/bin/env python3
import subprocess
class MacAdress:
global interface
interface = input("enter interface:\t")
def change_Mac(self=interface):
mac = '00:11:22:ff:ff:ff'
subprocess.call(f"sudo -S ifconfig {interface} down", shell=True)
subprocess.call(f'sudo -S ifconfig {interface} hw ether {mac}', shell=True)
subprocess.call(f"sudo -S ifconfig {interface} up", shell=True)
def Restore_Mac(self=interface):
old_mac = ''
subprocess.call(f"sudo -S ifconfig {interface} down", shell=True)
subprocess.call(f'sudo -S ifconfig {interface} hw ether {old_mac}', shell=True)
subprocess.call(f"sudo -S ifconfig {interface} up", shell=True)
I was wondering if there is a way to store the root password so it only prompts one time similar to how interface is used, instead of using sudo -S for each command.我想知道是否有一种方法可以存储 root 密码,因此它只提示一次类似于使用界面的方式,而不是对每个命令使用sudo -S 。 old mac is left blank intentionally.
老 mac故意留空。 Any tips for cleaning this up to be more professional would be greatly appreciated as well.
任何清理它以使其更专业的提示也将不胜感激。
#!/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)
Figured it out looks documentation provided enough information to answer my question along with some off topic linux-cli answers on Stack Overflow .弄清楚看起来文档提供了足够的信息来回答我的问题以及Stack Overflow上的一些离题的 linux-cli 答案。 Great community!!!
很棒的社区!!!
~edit~ It looks like there is a better way to capture the sudo password that wont show in clear text using the getpass library and passing arguments in cli ~edit~ 看起来有一种更好的方法可以使用 getpass 库并在 cli 中传递参数来捕获不会以明文形式显示的 sudo 密码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.