您的位置:首页 > 理论基础 > 计算机网络

Python网络编程:使用pexpect实现快速ssh连接

jinguangliu 2015-07-26 11:56 98 查看

简介

本文介绍了使用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() 让出控制权,用户可以继续当前的会话控制子程序。用户可以敲入特定的退出字符跳出.
标签: