繁体   English   中英

有没有办法在python3中将root密码传递给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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM