Pexpect is a Python module used to start subroutines and automatically control them, which can be used to interact with command-line programs like SSH, FTP, passwd, Telnet, and so on.
Shell command expect using http://blog.51cto.com/superleedo/1931418
Installing Pexpect
Open https://pypi.org/project/pexpect/#files
Download wget https://files.pythonhosted.org/packages/09/0e/ 75f0c093654988b8f17416afb80f7621bcf7d36bbd6afb4f823acdb4bcdc/pexpect-4.5.0.tar.gz
Tar zxf pexpect-4.5.0.tar.gz
CD pexpect-4.5.0/
Python setup.py Install
SSH telnet, execute command ' LS-LH ' example after successful login
#!/usr/bin/env python#-*-coding:utf-8-*-import pexpectimport sys# Start and control child applications via spawn class children = pexpect.spawn (' SSH [email Protected] ') #将pexpect的输入输出信息写到mylog. txt files fout = file (' Mylog.txt ', ' w ') Child.logfile = fout# outputs input and output information of pexpect to standard output # The Child.logfile = Sys.stdout#expect method is used to determine the output generated by the subroutine and to determine if the corresponding string Child.expect (' Password: ') is matched. String matching uses Sendline to respond-----send: Send command, do not enter, Sendline: Send command, enter, Sendcontrol: Send control, such as: Sendctrol (' C ') equivalent to ' CTRL + C ', Sendeof: Send Eofchild.sendline (' 123456 ') child.expect (' # ') child.sendline (' Ls-lh ') child.expect (' # ')
SSH login can also be implemented using Pexpect's Run function
Pexpect.run (' ssh [email protected] ', events={' password: ', ' 123456 '})
For SSH remote login, Pexpect also derived the Pxssh class, in the SSH session operation on another layer of encapsulation
from pexpect import pxsshimport getpasstry: s = Pxssh.pxssh () #创建pxssh对象 hostname = raw_input (' hostname: ') username = raw_input (' username: ') password = getpass.getpass (' Password: ') #接收密码输入 s.login (Server=hostname, Username=username,password=password) #建立ssh连接 s.sendline (' uptime ') #运行uptime命令 s.prompt () #匹配系统提示符 print s.before #打印出现系统提示符前的命令输出 s.sendline (' Ls -lh ') #运行命令 s.prompt () #匹配系统提示符 print s.before #打印出现系统提示符前的命令输出 s.sendline (' df -h ') #运行命令 s.prompt () #匹配系统提示符 &Nbsp; print s.before #打印出现系统提示符前的命令输出 s.logout () #断开ssh连接except pxssh. exceptionpxssh as e: print ' Pxssh failed on login ' print str (e)
Automated FTP Example
#!/usr/bin/env python#-*-coding:utf-8-*-form __future__ Import unicode_literals# Use Unicode encoding import Pexpectimport Syschild=pexpect.spawnu (' ftp ftp.openbsd.org ') child.expect (' (? i) name. *: ') # (? i) Ignore case child.sendline (' anonymous ') Child.expect (' (? i) password ') child.sendline (' mima123456 ') child.expect (' ftp> ') child.sendline (' bin ') # Open binary transfer child.expect (' ftp> ') child.sendline (' Get Test.txt ') child.expect (' ftp> ') sys.stdout.write ( Child.before) Print ("Escape character is ' ^] '. \ n") sys.stdout.write (Child.after) Sys.stdout.flush () child.interact () Child.sendline (' Bye ') Child.close ()
Remote file package and download sample
#!/usr/bin/env python# -*- coding: utf-8 -*- import pexpectimport sysip= "192.168.1.124" user= "root" passwd= "kkl123456" target_file= "/data/logs/ Nginx.log "Child=pexpect.spawn ('/usr/bin/ssh ', [user+ ' @ ' +ip]) fout=file (' Mylog.txt ', ' W ') child.logfile= Fouttry:child.expect (' (? i) password ') child.sendline (passwd) child.expect (' # ') child.sendline (' tar -zcf /data/logs/nginx.tar.gz ' +target_file) child.expect (' # ') print child.beforechild.sendline (' exit ') ) Fout.close () except eof:print "expect eof" except timeout:print "Expect TIMEOUT" Child=pexpect.spawn ('/usr/bin/scp ', [user+ ' @ ' +ip+ ':/data/logs/nginx.tar.gz ', ' Home ']) fout=file (' Mylog.txt ' A ') child.logfile=fouttry:child.expect (' (? i) password ') child.sendline (passwd) child.expect (pexpect. EOF) except eof:print "expect eof" except timeout:print "Expect timeout"
Python uses pexpect for automated interaction examples