Python安裝使用命令行交互模塊pexpect的基礎(chǔ)教程
一、安裝
1、安裝easy_install工具
wget http://peak.telecommunity.com/dist/ez_setup.py
python ez_setup.py 安裝easy_install工具(這個(gè)腳本會(huì)自動(dòng)去官網(wǎng)搜索下載并安裝)
python ez_setup.py -U setuptools
升級(jí)easy_install工具
2、安裝pexpect
easy_install Pexpect
測(cè)試一下:
[root@OMS python]# python Python 2.7.3rc1 (default, Nov 7 2012, 15:03:45) [GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import pexpect >>> import pxssh >>>
ok已經(jīng)安裝完成。
二、基本用法
1.run()函數(shù)
run功能相對(duì)簡(jiǎn)單,只能實(shí)現(xiàn)簡(jiǎn)單交互
run(command,timeout=-1,withexitstatus=False,events=None,extra_args=None, logfile=None, cwd=None, env=None)
run運(yùn)行命令,然后返回結(jié)果,與os.system類(lèi)似.
示例:
pexpect.run('ls -la') # 返回值(輸出,退出狀態(tài)) (command_output, exitstatus) = pexpect.run('ls -l /bin', withexitstatus=1)
spawn功能比run強(qiáng)大,可以實(shí)現(xiàn)更復(fù)雜交互
class spawn __init__(self, command, args=[], timeout=30, maxread=2000, searchwindowsize=None, logfile=None, cwd=None, env=None)
maxread設(shè)置read buffer大小. 每次pexpect嘗試從TTY(Teletype終端)從讀取的最大字節(jié)數(shù);
工作過(guò)程:
# 第一步與終端建立連接 child = pexpect.spawn('scp foo user@example.com:.') # 第二步等待終端返回特定內(nèi)容 child.expect('Password:') # 第三步根據(jù)返回內(nèi)容發(fā)送命令進(jìn)行交互 child.sendline(mypassword)
3.pxssh類(lèi)
pxssh是pexpect的派生類(lèi),用于建立ssh連接,比pexpect好用。
login() 建立到目標(biāo)機(jī)器的ssh連接;
logout() 釋放該連接;
prompt() 等待提示符,通常用于等待命令執(zhí)行結(jié)束。
三、實(shí)例
寫(xiě)一個(gè)腳本給遠(yuǎn)程服務(wù)器發(fā)送命令,并返回結(jié)果。
腳本內(nèi)容:
#!/usr/bin/python #2013-01-16 by larry import pexpect def login(port,user,passwd,ip,command): child=pexpect.spawn('ssh -p%s %s@%s "%s"' %(port,user,ip,command)) o='' try: i=child.expect(['[Pp]assword:','continue connecting (yes/no)?']) if i == 0: child.sendline(passwd) elif i == 1: child.sendline('yes') else: pass except pexpect.EOF: child.close() else: o=child.read() child.expect(pexpect.EOF) child.close() return o hosts=file('hosts.list','r') for line in hosts.readlines(): host=line.strip("\n") if host: ip,port,user,passwd,commands= host.split(":") for command in commands.split(","): print "+++++++++++++++ %s run:%s ++++++++++++" % (ip,command), print login(port,user,passwd,ip,command) hosts.close()
使用方法:
python scripts.py
host.list文件內(nèi)容如下:
192.168.0.21:22999:root:123456:cat /etc/redhat-release,df -Th,whoami 192.168.0.21:22999:root:123456:cat /etc/redhat-release,df -Th,whoami
返回結(jié)果:
+++++++++++++++ 192.168.0.21 run:cat /etc/redhat-release ++++++++++++ Red Hat Enterprise Linux Server release 4 +++++++++++++++ 192.168.0.21 run:df -Th ++++++++++++ 文件系統(tǒng) 類(lèi)型 容量 已用 可用 已用% 掛載點(diǎn) /dev/cciss/c0d0p6 ext3 5.9G 4.4G 1.2G 80% / /dev/cciss/c0d0p7 ext3 426G 362G 43G 90% /opt /dev/cciss/c0d0p5 ext3 5.9G 540M 5.0G 10% /var /dev/cciss/c0d0p3 ext3 5.9G 4.1G 1.5G 74% /usr /dev/cciss/c0d0p1 ext3 487M 17M 445M 4% /boot tmpfs tmpfs 4.0G 0 4.0G 0% /dev/shm +++++++++++++++ 192.168.0.21 run:whoami ++++++++++++ root
相關(guān)文章
haskell實(shí)現(xiàn)多線程服務(wù)器實(shí)例代碼
這篇文章主要介紹了haskell實(shí)現(xiàn)的多線程服務(wù)器,大家參考使用吧2013-11-11解決Pycharm在Debug的時(shí)候一直“Connected”沒(méi)有下一步動(dòng)作問(wèn)題
這篇文章主要介紹了解決Pycharm在Debug的時(shí)候一直“Connected”沒(méi)有下一步動(dòng)作問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08Python判斷兩個(gè)文件是否相同與兩個(gè)文本進(jìn)行相同項(xiàng)篩選的方法
今天小編就為大家分享一篇關(guān)于Python判斷兩個(gè)文件是否相同與兩個(gè)文本進(jìn)行相同項(xiàng)篩選的方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03python 讀取txt,json和hdf5文件的實(shí)例
今天小編就為大家分享一篇python 讀取txt,json和hdf5文件的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06django框架模板中定義變量(set variable in django template)的方法分析
這篇文章主要介紹了django框架模板中定義變量(set variable in django template)的方法,結(jié)合實(shí)例形式分析了Django框架實(shí)現(xiàn)模板中定義變量與變量賦值相關(guān)操作技巧,需要的朋友可以參考下2019-06-06