简体   繁体   English

使用python脚本升级linux

[英]Upgrade linux with a python script

I want to upgrade my linux system with a python script. 我想用python脚本升级我的linux系统。 But I don't know how to send Y or Enter to the console. 但是我不知道如何将Y或Enter输入到控制台。

This is my code: 这是我的代码:

sudo_password = 'password'
command = 'apt-get update'
os.system('echo %s|sudo -S %s' % (sudo_password, command))
command = 'apt-get upgrade'
os.system('echo %s|sudo -S %s' % (sudo_password, command))

EDIT: 编辑:

I want to run the script automated, therefore I don't want to use raw_input() or input(). 我想自动运行脚本,因此我不想使用raw_input()或input()。

Use raw_input function 使用raw_input函数

eg 例如

user_input = raw_input('Enter Y ')

Alternatively you could add the -y parameter to your apt-get command. 或者,您可以将-y参数添加到apt-get命令中。 Like this: 像这样:

...
command = 'apt-get upgrade -y'
...

If you want to just answer 'y' to all the apt-get prompts, consider piping the command through the yes bash command. 如果您只想对所有apt-get提示回答'y',请考虑通过yes bash命令来传递命令。

Untested (and you should test): 未经测试(您应该测试):

command = 'apt-get update|yes'
os.system('echo %s|sudo -S %s' % (sudo_password, command))
command = 'apt-get upgrade|yes'
os.system('echo %s|sudo -S %s' % (sudo_password, command))

The yes man page description says: yes手册页说明说:

Repeatedly output a line with all specified STRING(s), or 'y'. 重复输出所有指定的STRING或“ y”的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM