开发手册 欢迎您!
软件开发者资料库

Python pexpect模块的使用及示例代码

Pexpect 是 Expect 语言的一个 Python 实现,用于生成子应用程序并自动控制它们,并使用正则表达式对程序输出做出特定响应,以此实现与其自动交互的 Python 模块。本文主要介绍Python pexpect模块的使用及示例代码。

1、Expect工具语言

expect是一个简单的工具语言,它的作者对Expect的定义:是一个实现自动交互功能的软件套件(a software suite for automating interactive tools),使用expect,它能帮助我们在合适的情景下进行合适的交互。

expect 的核心功能,对于设定好的特定匹配形式,以相匹配的动作以应对。每一个expect后所跟的字符串(或者正则表达式)就是脚本所等待的匹配模式,每一个send 所做的工作就是对于各种的模式串,实施相应的动作。

2、pexpect模块的安装

pip install pexpect

3、pexpect模块的使用

Pexpect 系统有两个主要接口;这些是函数run() 和类 s​​pawnspawn 类更强大。run()函数比 spawn 简单,适合快速调用程序。当调用 run() 函数时,它会执行给定的程序,然后返回输出。很容易替代 os.system()

1)spawn()

spawn() 方法用来执行一个程序,它返回这个程序的操作句柄,以后可以通过操作这个句柄来对这个程序进行操作。

注意:spawn() 使用时,需要使用特殊符号的正确含义就必须加上 shell 来运行。

例如,

process = pexpect.spawn('/bin/bash –c "ls –l | grep error > log.txt"')

2)expect()

expect()用来关键字匹配,spawn() 启动了一个程序并返回程序控制句柄后,则可以用 expect() 方法来等待指定的关键字。它最后会返回 0 表示匹配到了所需的关键字,如果后面的匹配关键字是一个列表的话,就会返回一个数字表示匹配到列表中第几个关键字,从 0 开始计算。

3)send()

send()方法用来发送关键字,向程序发送指定的字符串。

4)sendline()

sendline()方法用来发送带回车符的字符串,sendline()send()区别只是在发送的字符串后面加上了回车换行符。

5)sendcontrol()

sendcontrol()方法用于发送控制信号,向子程序发送控制字符,如 Ctrl + C 或者 Ctrl + D 等,需要向子程序发送 Ctrl + C,则可以使用process.sendcontrol('c')

4、使用pexpect执行scp命令

使用pexpect执行scp命令,可以代码中指定密码,不用手动输入,可以批量自动处理。

import pexpectusername='root' aim_ip='192.168.31.11' password='Aa123456'source_file_path='/home/root/wonhero'aim_file_path='/home/'port=22password_key = '.*assword.*'command = f'scp {source_file_path}  {username}@{aim_ip}:{aim_file_path}'print("开始在本地服务器上执行指令: ", command)try:    execute = pexpect.spawn(command)    execute.expect(password_key)    execute.sendline(password)    execute.expect(pexpect.EOF)    print("拷贝成功")except:    print("拷贝失败")

5、使用pexpect实现自动登陆

可以使用pexpect实现通过密码或密钥文件ssh自动登陆Linux服务器,代码如下,

# -*- coding: utf-8 -*-#!/usr/bin/pythonimport pexpectdef login_ssh_password(port,user,host,passwd):    '''实现pexpect实现ssh的自动化用户密码登录'''    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.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):    '''实现pexpect实现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)        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!")def main():    '''实现两种方式分别的登录'''    login_ssh_password('22','root','192.163.31.11','Aa123456')    # login_ssh_key(keyfile="/tmp/id_rsa",port='22',user='root',host='192.168.31.21')if __name__ == "__main__":    main()