python 使用pexpect实现自动交互示例
Pexpect?是一個(gè)用來啟動(dòng)子程序并對(duì)其進(jìn)行自動(dòng)控制的 Python 模塊,它可以用來和像 ssh、ftp、passwd、telnet 等命令行程序進(jìn)行自動(dòng)交互。
shell 命令expect使用? ??https://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遠(yuǎn)程登錄,登錄成功后執(zhí)行命令‘ls -lh’示例
#!/usr/bin/env?python #?-*-?coding:?utf-8?-*- import?pexpect import?sys #通過spawn類啟動(dòng)和控制子應(yīng)用程序 child?=?pexpect.spawn('ssh?root@192.168.1.124') #將pexpect的輸入輸出信息寫到mylog.txt文件中 fout?=?file('mylog.txt','w') child.logfile?=?fout #將pexpect的輸入輸出信息輸出到標(biāo)準(zhǔn)輸出 #child.logfile?=?sys.stdout #expect方法用來判斷子程序產(chǎn)生的輸出,判斷是否匹配相應(yīng)字符串 child.expect('password:') #字符串匹配則使用sendline進(jìn)行回應(yīng)-----send:發(fā)送命令,不回車、sendline:發(fā)送命令,回車、sendcontrol:發(fā)送控制符,如:sendctrol('c')等價(jià)于‘ctrl+c'、sendeof:發(fā)送eof child.sendline('123456') child.expect('#') child.sendline('ls?-lh') child.expect('#')ssh登錄還可以使用pexpect的run函數(shù)實(shí)現(xiàn)
pexpect.run('ssh?root@192.168.1.124',events={'password:','123456'})針對(duì)ssh遠(yuǎn)程登錄,pexpect又派生出了pxssh類,在ssh會(huì)話操作上再做一層封裝
from?pexpect?import?pxssh import?getpasstry:s?=?pxssh.pxssh()???#創(chuàng)建pxssh對(duì)象hostname?=?raw_input('hostname:')username?=?raw_input('username:')password?=?getpass.getpass('password:')???#接收密碼輸入s.login(server=hostname,username=username,password=password)??#建立ssh連接s.sendline('uptime')??#運(yùn)行uptime命令s.prompt()???#匹配系統(tǒng)提示符print?s.before??#打印出現(xiàn)系統(tǒng)提示符前的命令輸出s.sendline('ls?-lh')??#運(yùn)行命令s.prompt()???#匹配系統(tǒng)提示符print?s.before??#打印出現(xiàn)系統(tǒng)提示符前的命令輸出s.sendline('df?-h')??#運(yùn)行命令s.prompt()???#匹配系統(tǒng)提示符print?s.before??#打印出現(xiàn)系統(tǒng)提示符前的命令輸出s.logout()??#斷開ssh連接except?pxssh.ExceptionPxssh?as?e:print?'pxssh?failed?on?login'print?str(e)自動(dòng)化FTP示例
#!/usr/bin/env?python #?-*-?coding:?utf-8?-*-form?__future__?import?unicode_literals #使用unicode編碼 import?pexpect import?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') #開啟二進(jìn)制傳輸 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()遠(yuǎn)程文件打包并下載示例
#!/usr/bin/env?python #?-*-?coding:?utf-8?-*-import?pexpect import?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"轉(zhuǎn)載于:https://blog.51cto.com/superleedo/2119076
總結(jié)
以上是生活随笔為你收集整理的python 使用pexpect实现自动交互示例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Mysql启动自己主动设置max_con
- 下一篇: 前端工程师和数据科学的快乐