使用python从另一个脚本处理交互式脚本
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了使用python从另一个脚本处理交互式脚本,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1545字,纯文字阅读大概需要3分钟。
内容图文
![使用python从另一个脚本处理交互式脚本](/upload/InfoBanner/zyjiaocheng/791/dfe0d11b4e9242b98bf0da8297e74dcc.jpg)
我有一个shell脚本,这个是交互式的,我正在创建一些自动化来为这个shell脚本提供输入.使用Python完成自动化.例,
shell脚本等待输入,例如“域名是什么?”
现在python应该能够提供输入并按ENTER键.
请通过一些示例提供处理此类会话的解决方案.
解决方法:
pexpect:http://pypi.python.org/pypi/pexpect/
它带有一堆例子.例如这一个用于ftp:
import pexpect
import sys
child = pexpect.spawn('ftp ftp.openbsd.org')
child.expect('(?i)name .*: ')
child.sendline('anonymous')
child.expect('(?i)password')
child.sendline('pexpect@sourceforge.net')
child.expect('ftp> ')
child.sendline('cd /pub/OpenBSD/3.7/packages/i386')
child.expect('ftp> ')
child.sendline('bin')
child.expect('ftp> ')
child.sendline('prompt')
child.expect('ftp> ')
child.sendline('pwd')
child.expect('ftp> ')
print("Escape character is '^]'.\n")
sys.stdout.write (child.after)
sys.stdout.flush()
child.interact() # Escape character defaults to ^]
# At this point this script blocks until the user presses the escape character
# or until the child exits. The human user and the child should be talking
# to each other now.
# At this point the script is running again.
print 'Left interactve mode.'
# The rest is not strictly necessary. This just demonstrates a few functions.
# This makes sure the child is dead; although it would be killed when Python exits.
if child.isalive():
child.sendline('bye') # Try to ask ftp child to exit.
child.close()
# Print the final state of the child. Normally isalive() should be FALSE.
if child.isalive():
print 'Child did not exit gracefully.'
else:
print 'Child exited gracefully.'
内容总结
以上是互联网集市为您收集整理的使用python从另一个脚本处理交互式脚本全部内容,希望文章能够帮你解决使用python从另一个脚本处理交互式脚本所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。