0

我正在尝试编写一个脚本来检查 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

我在这里错过了什么吗?

4

2 回答 2

4

有大量的先决条件。我只是认为要求用户安装所有这些会让人头疼。所以只是想写一个功能来测试软件,如果没有安装的东西会继续为他安装

有几种方法:

  1. 检查软件是否存在,如果它没有退出程序并显示列出要安装的缺失组件的消息。

  2. 提供需要安装的软件列表README或文档。

  3. 创建一个列出所需依赖项的debian 包;并让用户使用他们喜欢的方法安装它。

如果您的程序必须自己安装软件(它真的必须安装吗?),然后指示用户以提升的权限运行您的脚本。在您的文档中,要求他们将其作为root.

顺便说一句,sudo可能不会安装在所有系统上。

于 2012-06-25T09:01:24.560 回答
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)
于 2020-02-26T10:44:38.877 回答