Python网络编程:使用pexpect实现快速ssh连接
2015-07-26 11:56
881 查看
简介
本文介绍了使用pexpect模块实现快速ssh登录的功能实现
下面的例子,在Python 2.6.5的开发环境和Ubuntu系统下,实现ssh的快速连接的代码实现:#! /usr/bin/env python import pexpect import sys ip = "ssh root@192.168." + sys.argv[1] try: child = pexpect.spawn(ip) res = child.expect(['Are you sure you want to continue connecting (yes/no)?', 'password:']) if res == 0: child.sendline('yes') child.expect('password:') child.sendline('liuqi80') print child.before child.interact() except pexpect.EOF, pexpect.TIMEOUT: print "ssh 192.168." + sys.argv[1] + " timeout"注1: spawn是Pexpect模块主要的类,用以实现启动子程序,它有丰富的方法与子程序交互从而实现用户对子程序的控制。查看spawn的帮助手册
| __init__(self, command, args=[], timeout=30, maxread=2000, searchwindowsize=None, logfile=None, cwd=None, env=None) | This is the constructor. The command parameter may be a string that | includes a command and any arguments to the command. For example:: | | child = pexpect.spawn ('/usr/bin/ftp') | child = pexpect.spawn ('/usr/bin/ssh user@example.com') | child = pexpect.spawn ('ls -latr /tmp') | | You may also construct it with a list of arguments like so:: | | child = pexpect.spawn ('/usr/bin/ftp', []) | child = pexpect.spawn ('/usr/bin/ssh', ['user@example.com']) | child = pexpect.spawn ('ls', ['-latr', '/tmp']) | After this the child application will be created and will be ready to | talk to. For normal use, see expect() and send() and sendline().注2: 为了控制子程序,等待子程序产生特定输出,做出特定的响应,可以使用 expect 为了控制子程序,等待子程序产生特定输出,做出特定的响应,可以使用 expect 方法。在参数中: pattern 可以是正则表达式, pexpect.EOF , pexpect.TIMEOUT ,或者由这些元素组成的列表。需要注意的是,当 pattern 的类型是一个列表时,且子程序输出结果中不止一个被匹配成功,则匹配返回的结果是缓冲区中最先出现的那个元素,或者是列表中最左边的元素。使用 timeout
可以指定等待结果的超时时间 ,该时间以秒为单位。当超过预订时间时, expect 匹配到pexpect.TIMEOUT。
| expect(self, pattern, timeout=-1, searchwindowsize=None) | This seeks through the stream until a pattern is matched. The | pattern is overloaded and may take several types. The pattern can be a | StringType, EOF, a compiled re, or a list of any of those types. | Strings will be compiled to re types. This returns the index into the | pattern list. If the pattern was not a list this returns index 0 on a | successful match. This may raise exceptions for EOF or TIMEOUT. To | avoid the EOF or TIMEOUT exceptions add EOF or TIMEOUT to the pattern | list. That will cause expect to match an EOF or TIMEOUT condition | instead of raising an exception. | | If you pass a list of patterns and more than one matches, the first match | in the stream is chosen. If more than one pattern matches at that point, | the leftmost in the pattern list is chosen. For example:: | | # the input is 'foobar' | index = p.expect (['bar', 'foo', 'foobar']) | # returns 1 ('foo') even though 'foobar' is a "better" match注3: xpect还可以调用interact() 让出控制权,用户可以继续当前的会话控制子程序。用户可以敲入特定的退出字符跳出.
相关文章推荐
- HttpClient和HttpURLConnection的区别
- TCP/IP详解学习笔记--UDP协议
- Python:爬虫使用代理防封IP:HTTP Error 403: Forbidden
- 关于使用ArcMap10.0为几何网络建立流向
- HTTP Status 500 - java.lang.Long cannot be cast to java.lang.Integer
- HTTP简单的解析协议
- sprintf和printf http://blog.sina.com.cn/s/blog_44f08a12010007pi.html
- lr函数--web_get_int_property(记录HTTP响应信息)
- 【试水CAS-4.0.3】第06节_CAS服务端配置HTTPS
- LINUX C获取文件大小 http://my.oschina.net/mavericsoung/blog/138769
- poj PIGS 网络流EK
- web中的HTTP协议
- TCP编程,Socket通讯
- http代理
- http压缩
- http缓存
- http协议详解
- 虚拟机下centos6的网络IP配置
- HTTP学习要点(一)
- 网络科技协会暑假第一周学习计划报告