问题描述
我定义了一个不时切换代理设置的函数,问题是我希望它在没有人工干预的情况下循环运行.但是当我在 sudo 中执行程序时,它第一次被调用 en 运行顺利,第二次它要求我输入我的 sudo 密码.这是一段代码:
i defined a function that switch my proxy settings every now and then, problem is that i want it to run in a loop without manual intervention. But when i execute the program in sudo it gets called the first time en runs smoothly, second time it asks me for my sudo password. Here is the bit of code:
def ProxySetting(Proxy):
print "ProxyStetting(Proxy)"
call("networksetup -setwebproxy 'Wi-Fi' %s" "on" % Proxy, shell = True)
call("networksetup -setsecurewebproxy 'Wi-Fi' %s" "on" % Proxy, shell = True)
call("networksetup -setftpproxy 'Wi-Fi' %s" "on" %Proxy , shell=True)
我可以使用线程,但我确信有一种方法不会导致问题.如何对我的 sudo 密码进行硬编码,使其在函数开头运行?
I could use threading but am sure there is a way of doing it that wont cause problems. How can i hard code my sudo password so that it runs at the beginning of the function?
推荐答案
在这里你可以执行一个命令 sudo 没有交互式提示要求你输入密码:
Here you can execute a command sudo without interactive prompt asking you to type your password :
from subprocess import call
pwd='my password'
cmd='ls'
call('echo {} | sudo -S {}'.format(pwd, cmd), shell=True)
这篇关于使用 subprocess.call 时如何输入 sudo 密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!