我正在尝试使用Python子进程库运行这个命令sudo nmap -sP -n,我的目标是创建脚本当我运行该命令时,该脚本将从文件中读取密码并将其添加到子进程中,然后执行该命令。问题是我总是要写密码才能执行命令。
我试过了,但对我不起作用。
p = subprocess.Popen(["sudo", "nmap", "-sP", "-n", network], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
p.stdin.write(bytes("Mypassword", "utf-8"))我在这里找到了一些解决方案,Use subprocess to send a password,我尝试了所有的解决方案,但对我来说都不起作用。你知道我该怎么解决这个问题吗?
发布于 2019-04-30 23:30:18
sudo手册页指定:
-S, --stdin
Write the prompt to the standard error and read the password from the stan‐
dard input instead of using the terminal device. The password must be fol‐
lowed by a newline character.运行以下代码应该可以解决您的问题:
p = subprocess.Popen(["sudo", "-S", "cmd"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
p.stdin.write("password\n")显然,请确保您提供了正确的密码,否则这将无法工作。此外,不要忘记在末尾添加\n。
作为另一种选择,您可以运行nmap as an unprivileged user。这将允许您使用不需要密码的nmap --privileged ...。但是,正如链接中所指定的,请确保您了解安全问题,以确保它不会成为您的用例的问题。
https://stackoverflow.com/questions/55923870
复制相似问题