用pexpect做交互式的程序
来源:互联网 发布:网络管理小结 编辑:程序博客网 时间:2024/06/01 10:14
http://www.ibm.com/developerworks/cn/linux/l-cn-pexpect1/
其中一个ftp的例子是
import os
import sys
(command_output, exitstatus) = pexpect.run('ls -la', withexitstatus=1)
#print "%s, %d" %(command_output, exitstatus)
child = pexpect.spawn('ftp ftp.openbsd.org')
child.logfile = sys.stdout
child.expect('Name .*: ')
child.sendline('anonymous')
child.expect('Password:')
child.sendline('ex@example.com')
child.expect('ftp> ')
child.sendline('cd pub/OpenBSD')
child.expect('ftp> ')
child.sendline('get README')
child.expect('ftp> ')
child.sendline('bye')
1。 需要注意的是中间这个 child.logfile = sys.stdout 用来表明在标准输出中显示log。
如果不加这个,那么交互的输出将不会在stdout中显示。
2。 还有就是用pexpect.run()执行一个命令的输出在 返回值中,晕阿,开始我还一直以为会直接在stdout中显示的。。。
下面是个telnet的例子,本来想加密的,后来发现不好做。 先留着吧。
#!/usr/bin/pythonimport os
import sys
import pexpect
import base64
def main():
host_name = raw_input("Enter the host name: ")
if host_name == "haven":
host_name = "haven.au.ibm.com"
elif host_name == "ford11":
host_name = "ford11.ltc.austin.ibm.com"
elif host_name == "zhangli":
host_name = "9.3.190.254"
else:
print "Unknown host name"
exit()
telnet_cmd = "telnet %s" %(host_name)
try:
child = pexpect.spawn(telnet_cmd)
child.logfile = sys.stdout
i = child.expect(['Username: ', 'Unable to connect to remote host'], 30)
if i == 0:
child.sendline('ywywyang@cn.ibm.com')
child.expect('Password:')
child.sendline('144025_shyw_')
i == child.expect(['BSO Authentication Successful', 'BSO Authentication Error'])
if i == 0:
print "Passed"
else:
print "Try Again"
else:
print "You passed the BSO already"
child.close()
except pexpect.EOF:
child.close()
except pexpect.TIMEOUT:
print "Connection timeout"
clile.close()
else:
child.close()
if __name__ == "__main__":
main()
- 用pexpect做交互式的程序
- linux下交互式任务的自动化实现(expect&Pexpect)
- 用pexpect实现的任务分发工具,可用于pexpect学习
- 应用SoRayPickAction类的交互式程序
- 交互式程序的字符读取技巧
- matlab实现简单的交互式程序
- pexpect模块的学习
- pexpect模块的学习
- Python的Pexpect详解
- python下的Pexpect
- Python的Pexpect详解
- python 的 pexpect模块
- Pexpect 的实例分析
- Pexpect 模块的学习
- Python的Pexpect详解
- Python的Pexpect库
- pexpect
- pexpect
- wireshark 抓包的问题
- Android学习笔记<一> 简介
- Java Oracle JDBC 超出打开游标的最大数异常
- ARM寄存器
- win7安装Oracle错误:要求的结果: 5.0,5.1,5.2,6.0 之一 实际结果: 6.1
- 用pexpect做交互式的程序
- 【WordPress】定制后台管理界面的修改方法
- vmware ubuntu二三事
- NSF方式挂载Android分区
- Common Beginner Problems in JSF
- Java 泛型高级编程 ? 类型占位符的使用
- B/S开发用到的javascript技术
- 中介者模式(python)
- 五大内存分区