Python安裝使用命令行交互模塊pexpect的基礎(chǔ)教程
一、安裝
1、安裝easy_install工具
wget http://peak.telecommunity.com/dist/ez_setup.py
python ez_setup.py 安裝easy_install工具(這個(gè)腳本會自動去官網(wǎng)搜索下載并安裝)
python ez_setup.py -U setuptools
升級easy_install工具
2、安裝pexpect
easy_install Pexpect
測試一下:
[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功能相對簡單,只能實(shí)現(xiàn)簡單交互
run(command,timeout=-1,withexitstatus=False,events=None,extra_args=None, logfile=None, cwd=None, env=None)
run運(yùn)行命令,然后返回結(jié)果,與os.system類似.
示例:
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ù);
工作過程:
# 第一步與終端建立連接
child = pexpect.spawn('scp foo user@example.com:.')
# 第二步等待終端返回特定內(nèi)容
child.expect('Password:')
# 第三步根據(jù)返回內(nèi)容發(fā)送命令進(jìn)行交互
child.sendline(mypassword)
3.pxssh類
pxssh是pexpect的派生類,用于建立ssh連接,比pexpect好用。
login() 建立到目標(biāo)機(jī)器的ssh連接;
logout() 釋放該連接;
prompt() 等待提示符,通常用于等待命令執(zhí)行結(jié)束。
三、實(shí)例
寫一個(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) 類型 容量 已用 可用 已用% 掛載點(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)文章
python爬蟲 使用真實(shí)瀏覽器打開網(wǎng)頁的兩種方法總結(jié)
下面小編就為大家分享一篇python爬蟲 使用真實(shí)瀏覽器打開網(wǎng)頁的兩種方法總結(jié),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04
Python中的正則表達(dá)式與JSON數(shù)據(jù)交換格式
正則表達(dá)式 是一個(gè)特殊的字符序列,一個(gè)字符串是否與我們所設(shè)定的這樣的字符序列,相匹配快速檢索文本、實(shí)現(xiàn)替換文本的操作。這篇文章主要介紹了Python中的正則表達(dá)式與JSON ,需要的朋友可以參考下2019-07-07
Python實(shí)現(xiàn)跨平臺表格數(shù)據(jù)分頁打印預(yù)覽處理詳解
這篇文章主要為大家詳細(xì)介紹了如何使用PySide6/PyQt6實(shí)現(xiàn)Python跨平臺表格數(shù)據(jù)分頁打印預(yù)覽處理,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-03-03
使用Python代碼實(shí)現(xiàn)PDF文檔與SVG文件之間的轉(zhuǎn)換
PDF作為普遍采用的文件格式,確保了文檔的一致性和可靠性,而SVG(可縮放矢量圖形)則因其矢量性質(zhì),在網(wǎng)頁設(shè)計(jì)、高分辨率打印及動態(tài)交互內(nèi)容中展現(xiàn)出無與倫比的優(yōu)勢,本文將介紹如何使用Python將PDF文件轉(zhuǎn)換為SVG文件以及將SVG文件轉(zhuǎn)換為PDF文件,需要的朋友可以參考下2024-05-05
利用Python實(shí)現(xiàn)QQ實(shí)時(shí)到賬免簽支付功能
這篇文章主要介紹了利用Python實(shí)現(xiàn)QQ實(shí)時(shí)到賬免簽支付功能,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
python中requests使用代理proxies方法介紹
這篇文章主要介紹了python中requests使用代理proxies方法介紹,具有一定參考價(jià)值,需要的朋友可以了解下。2017-10-10
使用Termux在手機(jī)上運(yùn)行Python的詳細(xì)過程
這篇文章主要介紹了使用Termux在手機(jī)上運(yùn)行Python的詳細(xì)過程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-10-10

