[英]how to issue sudo command using python
I need to issue "sudo service nginx status" to check for service status. 我需要发出“ sudo服务nginx状态”来检查服务状态。 I have the following:
我有以下几点:
import commands
service output = commands.getoutput("sudo service nginx status")
but I am getting "no tty present and no askpass program specified" 但我收到“没有tty存在且未指定askpass程序”
Does someone understand this? 有人明白吗?
using commands.getoutput
makes impossible to provide the user input that is required by sudo command. 使用
commands.getoutput
使得无法提供sudo命令所需的用户输入。 The name is self explainable, you are interested only in the command output. 名称是不言自明的,您只对命令输出感兴趣。
stdin
is closed. stdin
已关闭。
There are several solutions for this: 有几种解决方案:
Turn off the password verification for the sudo user that is launching this python script. 关闭正在启动此python脚本的sudo用户的密码验证。 (read about /etc/sudoers)
(了解/ etc / sudoers)
pipe your password: (unsafe/bad solution but easy) "echo YOURPASS | sudo ..." 用管道传输您的密码:(不安全/错误的解决方案,但很容易)“ echo YOURPASS | sudo ...”
check out subprocess.popen
allowing you to provide input either from console or from file https://docs.python.org/2/library/subprocess.html#popen-constructor 检出
subprocess.popen
使您可以从控制台或文件https://docs.python.org/2/library/subprocess.html#popen-constructor提供输入
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.