Contents
19.2. 系统批量运维管理器pexpect¶
pip安装
pip install pexpect
easy_install pexpect
源码安装
#wget https://github.com/pexpect/pexpect/releases/download/3.0/pexpect-3.0.tar.gz -O pexpect-3.0.tar.gz
#tar -zxvf pexpect-3.0.tar.gz
#cd pexpect-3.0
#python setup.py install
19.2.1. pexpect应用示例¶
01.实现SSH自动登录示例.py
#!/usr/bin/env python
# -*- coding:utf8 -*-
# auther; 18793
# Date:2019/8/17 17:33
# filename: 01.实现SSH自动登录示例.py
from pexpect import pxssh
def send_command(s, cmd):
s.sendline(cmd)
s.prompt()
print(s.before)
def connect(host, user, password):
try:
s = pxssh.pxssh()
s.login(host, user, password)
return s
except:
print("error")
exit(0)
def main():
s = connect('192.168.0.100', 'root', 'admin#123')
send_command(s, 'whoami')
if __name__ == '__main__':
main()
02.实现ssh远程02.py
#!/usr/bin/env python
# -*- coding:utf8 -*-
# auther; 18793
# Date:2019/8/17 17:44
# filename: 02.实现ssh远程02.py
import pexpect
import sys
# 通过spawn类启动和控制子应用程序
child = pexpect.spawn('ssh root@192.168.0.100')
# 将pexpect的输入输出信息写到mylog.txt文件中
fout = open('mylog.txt', 'w')
child.logfile = fout
child.expect(['password:'])
# 字符串匹配则使用sendline进行回应-----send:发送命令,不回车、sendline:发送命令,回车、sendcontrol:发送控制符,如:sendctrol('c')等价于‘ctrl+c'、sendeof:发送eof
child.sendline('admin#123')
child.expect("#")
child.sendline('ls /home')
child.expect("#")
print("before:" + child.before)
print("after:" + child.after)
03.simple1.py
#!/usr/bin/env python
#-*- coding:utf8 -*-
# auther; 18793
# Date:2019/8/17 18:04
# filename: 03.simple1.py
from pexpect import pxssh
import getpass
try:
s = pxssh.pxssh()
hostname = raw_input('hostname: ')
username = raw_input('username: ')
password = getpass.getpass('password: ')
s.login (hostname, username, password)
s.sendline ('uptime') # run a command
s.prompt() # match the prompt
print s.before # print everything before the prompt.
s.sendline ('ls -l')
s.prompt()
print s.before
s.sendline ('df')
s.prompt()
print s.before
s.logout()
except pxssh.ExceptionPxssh, e:
print "pxssh failed on login."
print str(e)
实现一个自动化FTP操作
from __future__ import unicode_literals
import pexpect
import sys
child = pexpect.spawnu('ftp ftp.openbsd.org')
child.expect('(?i)name .*: ')
child.sendline('anonymous')
child.expect('(?i)password')
child.sendline('pexpect@sourceforge.net')
child.expect('ftp> ')
child.sendline('bin')
child.expect('ftp> ')
child.sendline('get robots.txt')
child.expect('ftp> ')
sys.stdout.write (child.before)
print("Escape character is '^]'.\n")
sys.stdout.write (child.after)
sys.stdout.flush()
child.interact() # Escape character defaults to ^]
child.sendline('bye')
child.close()
远程文件自动打包并下载
import sys
ip="192.168.1.21"
user="root"
passwd="H6DSY#*$df32"
target_file="/data/logs/nginx_access.log"
child = pexpect.spawn('/usr/bin/ssh', [user+'@'+ip])
fout = file('mylog.txt','w')
child.logfile = fout
try:
child.expect('(?i)password')
child.sendline(passwd)
child.expect('#')
child.sendline('tar -czf /data/nginx_access.tar.gz '+target_file)
child.expect('#')
print child.before
child.sendline('exit')
fout.close()
except EOF:
print "expect EOF"
except TIMEOUT:
print "expect TIMEOUT"
child = pexpect.spawn('/usr/bin/scp', [user+'@'+ip+':/data/nginx_access.tar.gz','/home'])
fout = file('mylog.txt','a')
child.logfile = fout
try:
child.expect('(?i)password')
child.sendline(passwd)
child.expect(pexpect.EOF)
except EOF:
print "expect EOF"
except TIMEOUT:
print "expect TIMEOUT"