Pexpect自动交互系统

简介:

  Pexpect 是 Don Libes 的 Expect 语言的一个 Python 实现,是一个用来启动子程序,并使用正则表达式对程序输出做出特定响应,以此实现与其自动交互的 Python 模块。 Pexpect 的使用范围很广,可以用来实现与 ssh、ftp 、telnet 等程序的自动交互;可以用来自动复制软件安装包并在不同机器自动安装;还可以用来实现软件测试中与命令行交互的自动化。


一、安装Pexpect

1、安装python软件包管理器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@plinuxos ~] # yum install -y python34-setuptools                                      
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
  * base: mirrors.btte.net
  * epel: mirrors.ustc.edu.cn
  * extras: mirrors.aliyun.com
  * updates: mirrors.aliyun.com
......
Installed:
   python34-setuptools.noarch 0:19.2-3.el7
 
Dependency Installed:
   python34.x86_64 0:3.4.5-4.el7           python34-libs.x86_64 0:3.4.5-4.el7
 
Complete!

2、安装pexpect软件

1
2
3
4
5
6
7
8
9
[root@plinuxos ~] # easy_install-3.4 pexpect
Searching  for  pexpect
Reading https: //pypi .python.org /simple/pexpect/
Best match: pexpect 4.2.1
Downloading https: //pypi .python.org /packages/e8/13/d0b0599099d6cd23663043a2a0bb7c61e58c6ba359b2656e6fb000ef5b98/pexpect-4 .2.1. tar .gz #md5=3694410001a99dff83f0b500a1ca1c95
Processing pexpect-4.2.1. tar .gz
......
Installed  /usr/lib/python3 .4 /site-packages/ptyprocess-0 .5.2-py3.4.egg
Finished processing dependencies  for  pexpect

3、验证安装效果

1
2
3
4
5
6
7
8
9
[root@plinuxos ~] # python3.4
Python 3.4.5 (default, May 29 2017, 15:17:55)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type  "help" "copyright" "credits"  or  "license"  for  more  information.
>>>  import  pexpect
>>>  dir (pexpect)
[ 'EOF' 'ExceptionPexpect' 'Expecter' 'PY3' 'TIMEOUT' '__all__' '__builtins__' '__cached__' '__doc__' '__file__' '__loader__' '__name__' '__package__' '__path__' '__revision__' '__spec__' '__version__' 'exceptions' 'expect' 'is_executable_file' 'pty_spawn' 'run' 'runu' 'searcher_re' 'searcher_string' 'spawn' 'spawnbase' 'spawnu' 'split_command_line' 'sys' 'utils' 'which' ]
>>> print(pexpect.run( 'w' ))
b ' 14:27:41 up 67 days,  1:12,  2 users,  load average: 0.00, 0.01, 0.05\r\nUSER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT\r\nroot     tty1                      23Sep17 38days  0.02s  0.02s -bash\r\nroot     pts/0    103.20.249.250   13:55    5.00s  0.14s  0.09s python3.4\r\n'

二、使用pexpect远程管理设备

1、创建脚本

1
2
3
4
5
6
7
8
9
10
[root@plinuxos tmp] # cat pexpectest.py
import  pexpect
import  sys
ssh =pexpect.spawn( '/usr/bin/ssh root@122.112.***.***' )
ssh .expect( 'password:' )
ssh .sendline( "123456" )
ssh .expect( '#' )
ssh .sendline( "echo 'this is a test'>> /tmp/1.txt" )
print( ssh .before)            # Print the result of the ls command. 
ssh .interact()               # Give control of the ssh to the user.

2、运行脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@plinuxos tmp] # python3.4 pexpectest.py
b ' \r\n  '
#####################################################################
   #                              Notice                                #
   #                                                                    #
   #  1. Please DO NOT upgrade the kernel, as the kernel upgrade would  #
   #   damage the original operating system.                            #
   #                                                                    #
   #  2. Please create unique passwords that use a combination of words,#
   #   numbers, symbols, and both upper-case and lower-case letters.    #
   #   Avoid using simple adjacent keyboard combinations such as        #
   #   "Qwert!234","Qaz2wsx",etc.                                       #
   #                                                                    #
   #  3. Unless necessary, please DO NOT open or use high-risk ports,   #
   #   such as Telnet-23, FTP-20/21, NTP-123(UDP), RDP-3389,            #
   #   SSH/SFTP-22, Mysql-3306, SQL-1433,etc.                           #
   #                                                                    #
   #                                                                    #
   #                     Any questions please contact 4000-955-988      #
   ######################################################################
[root@pos ~] # echo 'this is a test'>> /tmp/1.txt
[root@pos ~] #
##以上运行脚本后自动输出
[root@pos ~] # cat /tmp/1.txt
this is a  test
[root@pos ~] # logout
Connection to 122.112.***.*** closed.


参考文章探索Pexpect1探索Pexpect2python pexpect 学习与探索














本文转自Grodd51CTO博客,原文链接:http://blog.51cto.com/juispan/1978101,如需转载请自行联系原作者

相关文章
|
2月前
|
Shell 测试技术
Airtest如何自动连接重启后的设备并继续执行自动化脚本呢?
Airtest如何自动连接重启后的设备并继续执行自动化脚本呢?
|
11月前
|
调度
Magisk模块:Uperf调度模式切换器
Magisk模块:Uperf调度模式切换器
1764 0
|
Shell 测试技术 网络安全
Fabric 自动响应(替代pexpect)
Fabric 自动响应(替代pexpect)
|
JavaScript 前端开发
使用commander和inquirer创建命令行交互项目
使用commander和inquirer创建命令行交互项目
275 0
使用commander和inquirer创建命令行交互项目
|
SQL Java 数据库
用户模块之激活功能完成 | 学习笔记
快速学习用户模块之激活功能完成
127 0
|
资源调度
SAP Spartacus 如何借助env-cmd 实现 B2B 和 B2C 功能启动的无缝切换
在之前的 SAP Spartacus 标准开发中,每次我从 Github 上 clone 了最新的代码后,必须手动修改 environment.ts 里的配置值,将 CX_BASE_URL 和 b2b 修改成实际值:
SAP Spartacus 如何借助env-cmd 实现 B2B 和 B2C 功能启动的无缝切换
|
Java 开发者
用户模块之退出功能|学习笔记
快速学习用户模块之退出功能
|
弹性计算 运维 Ubuntu
效率神器!云助手----发送远程命令:通过发送远程命令窗口,无需登录到实例内部,即可直接执行命令。
发送远程命令可以帮助您在实例内部快速执行命令,即无需远程连接登录实例,即可完成查看硬盘空间、安装软件、启动停止服务等操作。该功能通过 云助手 的命令执行功能实现,如果您的实例还没有安装或激活云助手客户端,请先 安装或者激活 。
12543 0
效率神器!云助手----发送远程命令:通过发送远程命令窗口,无需登录到实例内部,即可直接执行命令。
|
Shell 网络安全 数据安全/隐私保护
expect - 自动交互脚本
1. expect参数 expect教程中文版 expect说明 2. 启用选项 -c :执行脚本前先执行的命令,可多次使用。 -d :debug模式,可以在运行时输出一些诊断信息,与在脚本开始处使用 exp_internal 1 相似。
1390 0