python 使用pexpect实现自动交互示例
生活随笔
收集整理的這篇文章主要介紹了
python 使用pexpect实现自动交互示例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Pexpect?是一個用來啟動子程序并對其進行自動控制的 Python 模塊,它可以用來和像 ssh、ftp、passwd、telnet 等命令行程序進行自動交互。
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遠程登錄,登錄成功后執行命令‘ls -lh’示例
#!/usr/bin/env?python #?-*-?coding:?utf-8?-*- import?pexpect import?sys #通過spawn類啟動和控制子應用程序 child?=?pexpect.spawn('ssh?root@192.168.1.124') #將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:發送eof child.sendline('123456') child.expect('#') child.sendline('ls?-lh') child.expect('#')ssh登錄還可以使用pexpect的run函數實現
pexpect.run('ssh?root@192.168.1.124',events={'password:','123456'})針對ssh遠程登錄,pexpect又派生出了pxssh類,在ssh會話操作上再做一層封裝
from?pexpect?import?pxssh import?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?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') #開啟二進制傳輸 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?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"轉載于:https://blog.51cto.com/superleedo/2119076
總結
以上是生活随笔為你收集整理的python 使用pexpect实现自动交互示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Mysql启动自己主动设置max_con
- 下一篇: python selenium系列(六)