演示.py
import os
f = os.popen('passwd')
输出:
Enter Existing Password: (This one I want program enter)
我正在运行这个提示我输入现有密码的 python 程序。但我想通过程序发送现有密码。
有什么方法可以通过程序将该值传递给命令提示符。
最佳答案
我们可以通过使用 python 的 expect 模块来实现这一点。我写了示例代码:
#!/usr/bin/python
import pexpect
import sys
child = pexpect.spawn('passwd')
child.logfile=sys.stdout
child.expect("UNIX password:")
child.sendline('guest')
child.expect('Enter new UNIX password:')
child.sendline('guest')
child.expect('Retype new UNIX password:')
child.sendline('guest')
您还可以在 http://www.pythonforbeginners.com/systems-programming/how-to-use-the-pexpect-module-in-python 查看 expect 模块的更多详细信息
关于python - 如何使用 python 脚本将参数传递给 linux 命令提示符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37034585/