![](/img/trans.png)
[英]Some issues with python, python2.7 and python3 in Kali Linux
[英]Is there a way to pass root password into functions on kali linux in python3
我構建了一個類來更改我的 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)
我想知道是否有一種方法可以存儲 root 密碼,因此它只提示一次類似於使用界面的方式,而不是對每個命令使用sudo -S 。 老 mac故意留空。 任何清理它以使其更專業的提示也將不勝感激。
#!/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)
弄清楚看起來文檔提供了足夠的信息來回答我的問題以及Stack Overflow上的一些離題的 linux-cli 答案。 很棒的社區!!!
~edit~ 看起來有一種更好的方法可以使用 getpass 庫並在 cli 中傳遞參數來捕獲不會以明文形式顯示的 sudo 密碼
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.