![](/img/trans.png)
[英]how to execute bash script from python (bash file contains user input)
[英]How to send variable from php to a bash script that is asking for user input and need to click enter?
#!/usr/bin/python
import os
import sys
client=sys.argv[1]
homeDir="/home/vpnUsers"
os.system("cd /etc/openvpn/easy-rsa/")
os.system("./easyrsa build-client-full %s" %(client))
os.system("cp /etc/openvpn/client-template.txt %s/%s.ovpn" %(homeDir,client))
在os.system("./easyrsa build-client-full %s" %(client))如果我在终端上手动运行它会要求输入密码 2 次,所以如果我手动执行我可以输入密码,单击输入并完成。 我怎么能做到这一点,但使用python?
让我们假设 sys.argv[2] 是它的密码,当它要求输入密码时,它应该写在这个命令中。
Ps:参数来自php。
解决了!
该库将与脚本对话。 便于使用。
pip 安装 pexpect
然后在你的脚本 python 上导入它
进口预期
我的情况的示例代码
localcmd='/path/to/script/easyrsa build-client-full "arg"'
def localOutput(localcmd):
child = pexpect.spawn (localcmd)
child.expect ('Enter PEM pass phrase:')
child.sendline ('password')
child.expect ('Verifying - Enter PEM pass phrase:')
child.sendline ('password')
child.expect(pexpect.EOF)
return child.before
localout=localOutput(localcmd)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.