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,探索Pexpect2,python pexpect 学习与探索
本文转自Grodd51CTO博客,原文链接:http://blog.51cto.com/juispan/1978101,如需转载请自行联系原作者