Python os.system()将文本输入脚本 [英] Python os.system() input text to script

查看:647
本文介绍了Python os.system()将文本输入脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要自动化的脚本,所以它是一个.sh脚本,我想在python脚本中运行:像这样:

I have a script that I need to automate, so it's a .sh script which I want to run inside a python script: something like this:

import os
os.system('./script.sh -p 1234')

脚本script.sh需要用户输入3个字段,1)sudo密码,2)字符串和3)字符串.

The script script.sh needs the user to input 3 fields, 1) the sudo password, 2) a string and 3) a string.

Enter password for user: xxxx  #typed by me
Enter Auth Username: xxxx      #typed by me
Enter Auth Password: xxx       #typed by me

如何使python脚本将这3个所需的值键入/插入/传递给script.sh.

How can I make the python script to type/insert/pass those 3 needed values to script.sh.

推荐答案

您可以使用subprocess.Popen启动脚本,并使用communicate方法传递输入.像这样:

You can use subprocess.Popen to start the script and the communicate method to pass it input. Something like this:

import subprocess

p = subprocess.Popen(['./script.sh', '-p', '1234'], 
                     stdin=subprocess.PIPE, stdout=subprocess.PIPE)
stdout, stderr = p.communicate(input='password\nauth username\nauth password\n')

这篇关于Python os.system()将文本输入脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆