我正在尝试编写一个脚本来检查Ubuntu中的必备软件,如果没有安装,请继续并安装它。
我是Python的新手,我知道Python的subprocess
模块可以用来实现这个目标。
我在下面的示例代码中写了这个,但不确定它为什么失败。
import subprocess
c = subprocess.Popen(['sudo','apt-get','install','git'], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
grep_stdout = c.communicate(input='root')[0]
print(grep_stdout)
当我运行此程序时,我收到以下错误...
sudo: no tty present and no askpass program specified
Sorry, try again.
sudo: no tty present and no askpass program specified
Sorry, try again.
sudo: no tty present and no askpass program specified
Sorry, try again.
sudo: 3 incorrect password attempts
我在这里错过了什么吗?
答案 0 :(得分:4)
有大量的先决条件。我只是想问 用户安装所有这些将是一个痛苦的脖子。所以就 想到编写一个将测试软件的功能,以及是否 未安装的东西将继续为他安装
有几种方法:
检查软件是否存在,如果没有退出程序,并显示一条消息,列出要安装的缺失组件。
提供需要在README
或文档中安装的软件列表。
创建列出所需依赖项的debian package;并让用户使用他们的首选方法安装它。
如果你的程序必须安装软件本身(它真的必须吗?),那么指示用户使用提升的权限运行你的脚本。在您的文档中,要求他们将其作为root
运行。
顺便说一下sudo
可能不会安装在所有系统上。
答案 1 :(得分:1)
import subprocess
c = subprocess.Popen(['sudo','yum','install','git','-y'],
stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
grep_stdout = c.communicate(input='root')[0]
print(grep_stdout)