国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python安裝使用命令行交互模塊pexpect的基礎教程

2020-01-04 17:28:28
字體:
來源:轉載
供稿:網友
Pexpect是一個純Python模塊,可以用來和ssh、ftp、passwd、telnet等命令行命令進行交互使用,在Linux系統下尤其好用,下面我們就來具體來看一下Python安裝使用命令行交互模塊pexpect的基礎教程:
 

一、安裝
1、安裝easy_install工具

wget http://peak.telecommunity.com/dist/ez_setup.py

python ez_setup.py 安裝easy_install工具(這個腳本會自動去官網搜索下載并安裝)

python ez_setup.py -U setuptools

升級easy_install工具

2、安裝pexpect

easy_install Pexpect

測試一下:

[root@OMS python]# pythonPython 2.7.3rc1 (default, Nov 7 2012, 15:03:45)[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import pexpect>>> import pxssh>>>

ok已經安裝完成。


二、基本用法
1.run()函數

run功能相對簡單,只能實現簡單交互

 

復制代碼代碼如下:

 

run(command,timeout=-1,withexitstatus=False,events=None,extra_args=None, logfile=None, cwd=None, env=None)

 


run運行命令,然后返回結果,與os.system類似.
示例:

 

pexpect.run('ls -la')# 返回值(輸出,退出狀態)(command_output, exitstatus) = pexpect.run('ls -l /bin', withexitstatus=1)
2.spawn類
spawn功能比run強大,可以實現更復雜交互
class spawn   __init__(self, command, args=[], timeout=30, maxread=2000, searchwindowsize=None, logfile=None, cwd=None, env=None)
timeout指定交互是等待的超時值;
maxread設置read buffer大小. 每次pexpect嘗試從TTY(Teletype終端)從讀取的最大字節數;
searchwindowsize 指定了從輸入緩沖區中進行模式匹配的位置,默認從開始匹配;
logfile 參數指定日志的記錄位置。
工作過程:
# 第一步與終端建立連接child = pexpect.spawn('scp foo user@example.com:.')# 第二步等待終端返回特定內容child.expect('Password:')# 第三步根據返回內容發送命令進行交互child.sendline(mypassword)

3.pxssh類
pxssh是pexpect的派生類,用于建立ssh連接,比pexpect好用。

login()  建立到目標機器的ssh連接;
logout() 釋放該連接;
prompt() 等待提示符,通常用于等待命令執行結束。


三、實例
寫一個腳本給遠程服務器發送命令,并返回結果。
腳本內容:

#!/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文件內容如下:

192.168.0.21:22999:root:123456:cat /etc/redhat-release,df -Th,whoami192.168.0.21:22999:root:123456:cat /etc/redhat-release,df -Th,whoami

返回結果:

+++++++++++++++ 192.168.0.21 run:cat /etc/redhat-release ++++++++++++Red Hat Enterprise Linux Server release 4+++++++++++++++ 192.168.0.21 run:df -Th ++++++++++++文件系統   類型  容量 已用 可用 已用% 掛載點/dev/cciss/c0d0p6ext3  5.9G 4.4G 1.2G 80% //dev/cciss/c0d0p7ext3  426G 362G  43G 90% /opt/dev/cciss/c0d0p5ext3  5.9G 540M 5.0G 10% /var/dev/cciss/c0d0p3ext3  5.9G 4.1G 1.5G 74% /usr/dev/cciss/c0d0p1ext3  487M  17M 445M  4% /boottmpfs    tmpfs  4.0G   0 4.0G  0% /dev/shm+++++++++++++++ 192.168.0.21 run:whoami ++++++++++++root

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阿克陶县| 沧州市| 福建省| 修武县| 淮滨县| 日土县| 噶尔县| 乌审旗| 富锦市| 高台县| 灵武市| 杭锦后旗| 观塘区| 丰镇市| 扶沟县| 安陆市| 水城县| 富川| 邛崃市| 搜索| 乌审旗| 湘阴县| 东港市| 博兴县| 辽阳县| 昌吉市| 拉萨市| 红河县| 凯里市| 彭泽县| 吉木乃县| 临湘市| 丽江市| 达拉特旗| 天台县| 福安市| 巴林左旗| 固阳县| 宕昌县| 中山市| 泗洪县|