pexpect模块

  pexpect用来启动子程序,使用正则表达式对程序输出做出特定响应,以此实现与其自动交互的python模块,当然我们可以使用他来做ssh登陆,ssh模块登陆还有一个基于python实现远程连接,用于ssh远程执行命令,文件传输等功能的ssh客户端模块paramiko。

  在开发时也经常会使用pexpect模块,所以这里简单介绍一下pexpect。

  run():直接执行命令返回结果和状态

  run里有一个command参数,可以直接写命令然后拿到命令返回的状态和结果。

In [2]: import pexpect

In [3]: pexpect.run("ls /tmp/")

  我的/tmp下的东西有点多,已经删除了部分,此时只拿到了执行的结果但是没有拿到执行的状态。

In [4]: pexpect.run("ls /tmp", withexitstatus=1)

  withexitstatus为1则表示需要返回执行状态,默认不返回。

  执行状态为:

  返回的结果是一个元组,下标1的值为0即执行成功,非0则为执行失败。

  命令不存在时会报错,

  命令存在但是执行失败则打印错误信息,得到非0状态码。

  spawn

  启动子程序,有丰富的方法实现对子程序的控制。

  比如我们使用spawn进行ssh连接:

In [7]: ssh_aliyun = pexpect.spawn('ssh 120.79.254.225')

In [8]: ssh_aliyun.expect("password:")

  生成一个实例对象,使用在expect期望的结果中我们可以使用完全匹配也可以使用正则匹配。

  当匹配成功以后会返回状态码0,匹配字段不存在,则程序阻塞,知道timeout程序会报错。

  注:expect中的$就是$不是正则表达式里的以某某结尾的意思。

  当然我们不希望匹配不成功而导致程序崩溃,expect中也可以填入错误状态,或者多个期待值,放在一个列表中。

In [18]: ssh_aliyun = pexpect.spawn('ssh 120.79.254.225')

In [19]: ssh_aliyun.expect([pexpect.TIMEOUT, pexpect.EOF, "password:"])
Out[19]: 2

  返回的结果为列表下标索引值。timeout则结果为0.

  对于刚才的实例,我对阿里云服务器发起ssh远程连接,需要进行密码校验,那么我们如何进行命令键入呢。

  我们可以使用send(),sendline(),sendcontrol(char)。向子程序发送指令。前两种就是发送指令,后一种发送控制符(比如发送ctrl+c中断命令)。

  密码登陆的ssh脚本

import pexpect

def login_ssh_passwd(port="",user="",host="",passwd=""):
    '''函数:用于实现pexepect实现ssh的自动化用户密码登录'''

    # print 'ssh -p %s %s@%s' % (port,user, host)
    if  port and user and host and passwd:
        ssh = pexpect.spawn('ssh -p %s %s@%s' % (port,user, host))
        i = ssh.expect(['password:', 'continue connecting (yes/no)?'], timeout=5)
        if i == 0 :
            ssh.sendline(passwd)
        elif i == 1:
            ssh.sendline('yes\n')
            ssh.expect('password: ')
            ssh.sendline(passwd)
        index = ssh.expect (["#", pexpect.EOF, pexpect.TIMEOUT])

        if index == 0:
            print("logging in as root!")
            #终端直接接管子程序会话,在终端直接操控ssh
            # ssh.interact()
        elif index == 1:
            print("logging process exit!")
        elif index == 2:
            print("logging timeout exit")
    else:
        print("Parameter error!")

  通过密钥方式登陆

def login_ssh_key(keyfile="",user="",host="",port=""):
    '''函数:用于实现pexepect实现ssh的自动化密钥登录'''

    if  port and user and host and keyfile:
        ssh = pexpect.spawn('ssh -i %s -p %s %s@%s' % (keyfile,port,user, host))
        i = ssh.expect( [pexpect.TIMEOUT,'continue connecting (yes/no)?'], timeout=2)
        # print '...................................',0
        if i == 1:
            ssh.sendline('yes\n')
            index = ssh.expect (["#", pexpect.EOF, pexpect.TIMEOUT])
        else:
            index = ssh.expect (["#", pexpect.EOF, pexpect.TIMEOUT])
        if index == 0:
            print("logging in as root!")
            ssh.interact()
        elif index == 1:
            print("logging process exit!")
        elif index == 2:
            print("logging timeout exit")
    else:
        print("Parameter error!")

  使用pexpect实现ssh远程登陆存在一些问题,依赖终端来完成登陆。在不同的登陆环境(系统不一致,端口不一致等)兼容性比较差。

  在资产管理的登陆扫描探测的具体环境中不适合直接使用pexpect来操作。

posted @ 2018-03-29 00:01  JeffD  阅读(875)  评论(2编辑  收藏  举报