標籤:pexpect
Pexpect 是一個用來啟動子程式並對其進行自動控制的 Python 模組,它可以用來和像 ssh、ftp、passwd、telnet 等命令列程式進行自動互動。
shell 命令expect使用 http://blog.51cto.com/superleedo/1931418
安裝pexpect
開啟 https://pypi.org/project/pexpect/#files
下載 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遠程登入,登入成功後執行命令‘ls -lh’樣本
#!/usr/bin/env python# -*- coding: utf-8 -*-import pexpectimport sys#通過spawn類啟動和控制子應用程式child = pexpect.spawn('ssh [email protected]')#將pexpect的輸入輸出資訊寫到mylog.txt檔案中fout = file('mylog.txt','w')child.logfile = fout#將pexpect的輸入輸出資訊輸出到標準輸出#child.logfile = sys.stdout#expect方法用來判斷子程式產生的輸出,判斷是否匹配相應字串child.expect('password:')#字串匹配則使用sendline進行回應-----send:發送命令,不斷行符號、sendline:發送命令,斷行符號、sendcontrol:發送控制符,如:sendctrol('c')等價於‘ctrl+c'、sendeof:發送eofchild.sendline('123456')child.expect('#')child.sendline('ls -lh')child.expect('#')
ssh登入還可以使用pexpect的run函數實現
pexpect.run('ssh [email protected]',events={'password:','123456'})
針對ssh遠程登入,pexpect又派生出了pxssh類,在ssh會話操作上再做一層封裝
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() #匹配系統提示符 print s.before #列印出現系統提示符前的命令輸出 s.logout() #斷開ssh串連except pxssh.ExceptionPxssh as e: print 'pxssh failed on login' print str(e)
自動化FTP樣本
#!/usr/bin/env python# -*- coding: utf-8 -*-form __future__ import unicode_literals#使用unicode編碼import pexpectimport syschild=pexpect.spawnu('ftp ftp.openbsd.org')child.expect('(?i)name .*: ')#(?i)忽略大小寫child.sendline('anonymous')child.expect('(?i)password')child.sendline('mima123456')child.expect('ftp> ')child.sendline('bin')#開啟二進位傳輸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()
遠程檔案打包並下載樣本
#!/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 使用pexpect實現自動互動樣本