Jusene's Blog

Python运维自动化利器pexpect

字数统计: 514阅读时长: 2 min
2018/02/07 Share

pexcept

pexcept是一个纯python实现的模块,pexpect可以某些交互式子进程进行交互而不需要手工操作,比如使用passwd命令时需要人工输入密码,而pexcept可以使用sendline进行自动化。

安装

1
pip install -U pexpect

or

1
easy_install pexpect

使用

1
import pexpect
2
3
child=pexpect.spawn('ssh root@10.211.55.18')
4
'''
5
The authenticity of host '10.211.55.18 (10.211.55.18)' can't be established.
6
ECDSA key fingerprint is SHA256:FUUxzLdqAZQtRp5e0LeuwrQ698D3sDs3PoX9Fq3+uYk.
7
Are you sure you want to continue connecting (yes/no)?
8
'''
9
child.expect('.*(yes/no)? ')
10
child.sendline('yes')
11
'''
12
root@10.211.55.18's password:
13
'''
14
child.expect('password: ')
15
child.sendline('123456')
16
17
print(child.before)
18
b"root@10.211.55.18's "  #before属性将包含所有的文本到预期的字符串模式
19
print(child.after)
20
b'password: '            #after属性将包含与预期模式匹配的文本
21
22
child.interact()         #给出子进程的控制权
23
Last login: Wed Feb  7 00:20:45 2018 from 10.211.55.2
24
[root@node3 ~]#

except() 可以是正则表达式,也可能是正则表达式的列表。这允许您匹配多个可选响应。

1
import pexpect
2
3
secret='123456'
4
child=pexpect.spawn('ssh root@10.211.55.18')
5
child.expect('password: ')
6
child.sendline(secret)
7
8
i = child.expect(['password: ','\[.*\]# '])
9
if i==0:
10
    print('密码错误')
11
    child.kill(0)
12
elif i==1:
13
    print('登陆成功')
14
    child.interact()

except()超时等待时间,默认是30秒,可以通过参数的形式更改:

1
child.expect('password: ',timeout=120)

debug

1
import pexpect
2
3
secret='12345'
4
child=pexpect.spawn('ssh root@10.211.55.18')
5
child.expect('password: ')
6
child.sendline(secret)
7
print(str(child))
8
9
<pexpect.pty_spawn.spawn object at 0x105ee30b8>
10
command: /usr/bin/ssh
11
args: ['/usr/bin/ssh', 'root@10.211.55.18']
12
buffer (last 100 chars): b''
13
before (last 100 chars): b"root@10.211.55.18's "
14
after: b'password: '
15
match: <_sre.SRE_Match object; span=(20, 30), match=b'password: '>
16
match_index: 0
17
exitstatus: None
18
flag_eof: False
19
pid: 7922
20
child_fd: 5
21
closed: False
22
timeout: 30
23
delimiter: <class 'pexpect.exceptions.EOF'>
24
logfile: None
25
logfile_read: None
26
logfile_send: None
27
maxread: 2000
28
ignorecase: False
29
searchwindowsize: None
30
delaybeforesend: 0.05
31
delayafterclose: 0.1
32
delayafterterminate: 0.1
33
34
child.expect('password: ')
35
print(str(child))
36
<pexpect.pty_spawn.spawn object at 0x105ee3400>
37
command: /usr/bin/ssh
38
args: ['/usr/bin/ssh', 'root@10.211.55.18']
39
buffer (last 100 chars): b''
40
before (last 100 chars): b"\r\nPermission denied, please try again.\r\r\nroot@10.211.55.18's "
41
after: b'password: '
42
match: <_sre.SRE_Match object; span=(61, 71), match=b'password: '>
43
match_index: 0
44
exitstatus: None
45
flag_eof: False
46
pid: 7936
47
child_fd: 5
48
closed: False
49
timeout: 30
50
delimiter: <class 'pexpect.exceptions.EOF'>
51
logfile: None
52
logfile_read: None
53
logfile_send: None
54
maxread: 2000
55
ignorecase: False
56
searchwindowsize: None
57
delaybeforesend: 0.05
58
delayafterclose: 0.1
59
delayafterterminate: 0.1
CATALOG
  1. 1. pexcept
  2. 2. 安装
  3. 3. 使用
  4. 4. debug