【python】python 模組學習之--pexpect
基礎例子:
複製資料
-
>>> import pexpect
-
>>> child=pexpect.spawn('scp /home/chenliang/oradba.perl root@218.78.186.162:/home/chenliang/') -----啟動程式
-
>>> child.expect('(?i)password:') -----匹配密碼,expect正規表示式,表示匹配的意思
-
0
-
>>> child.sendline('XXXXXXXXX') ------傳送密碼,sendline 傳送命令
- 9
檢查:複製成功
[root@localhost /home/chenliang ]# ls
1.txt 2.txt oradba.perl test.txt
[root@localhost /home/chenliang ]# ls
1.txt 2.txt oradba.perl test.txt
ssh目標機器並建立一個檔案
-
>>> import pexpect
-
>>> tar=pexpect.spawn('ssh root@218.78.186.162')
-
>>> tar.expect('(?i)password:')
-
0
-
>>> tar.sendline('ESBecs00') ####sendline 傳送密碼
-
9
-
>>> tar.sendline('touch /home/chenliang/tar.txt') ####sendline 傳送命令
- 30
檢查:成功
[root@localhost /home/chenliang ]# ls
1.txt 2.txt oradba.perl tar.txt test.txt
[root@localhost /home/chenliang ]# ls
1.txt 2.txt oradba.perl tar.txt test.txt
run命令
- 和 os.system一樣呼叫系統命令
-
>>> run('touch /home/chenliang/run.txt')
- ''
練習一
-
ssh並執行命令
-
-
[root@hostnfsd :/soft/python]$ more test2.py
-
#ssh_cmd.py
-
#coding:utf-8
-
-
import pexpect
-
import sys
-
ip='218.78.186.162'
-
user='root'
-
passwd='XXXXX'
-
cmd='df -h'
-
def ssh_cmd(vip,vuser,vpassword,vcmd):
-
child=pexpect.spawn('ssh %s@%s %s' % (vuser, vip,vcmd))
-
r = ''
-
child.expect('(?i)password:')
-
child.sendline(vpassword)
-
r = child.read()
-
return r
-
-
print ssh_cmd(ip, user, passwd, cmd)
[root@hostnfsd :/soft/python]$ python test2.py
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
18G 15G 1.7G 90% /
tmpfs 1.9G 8.0K 1.9G 1% /dev/shm
/dev/xvda1 485M 119M 341M 26% /boot
/dev/mapper/vgu01-lvu01
20G 3.2G 16G 17% /home
/dev/mapper/vgu01-lvu02
30G 33M 30G 1% /back
-
向多臺機器ssh並執行多個命令
-
稍微修改一下就好了
-
#!/usr/bin/python
import pexpect
def ssh_cmd(ip, user, passwd, cmd):
ssh = pexpect.spawn('ssh %s@%s "%s"' % (user, ip, cmd))
r = ''
ssh.expect('(?i)password:')
ssh.sendline(passwd)
r = ssh.read()
return r
hosts = '''
218.78.186.162:root:xxxx:df -h,uptime,tar -zcf /home/chenliang/tar.txt.tar.gz /home/chenliang/tar.txt;
-
218.78.186.163:root:xxxx:df -h,uptime,tar -zcf /home/chenliang/tar.txt.tar.gz /home/chenliang/tar.txt;
'''
for host in hosts.split("\n"):
if host:
ip, user, passwd, cmds = host.split(":")
for cmd in cmds.split(","):
print "-- %s run:%s --" % (ip, cmd)
print ssh_cmd(ip, user, passwd, cmd)
-
-
執行結果
-
[root@hostnfsd :/soft/python]$ python test1.py
-
-- 218.78.186.162 run:df -h --
-
-
Filesystem Size Used Avail Use% Mounted on
-
/dev/mapper/VolGroup-lv_root
-
18G 15G 1.7G 90% /
-
tmpfs 1.9G 8.0K 1.9G 1% /dev/shm
-
/dev/xvda1 485M 119M 341M 26% /boot
-
/dev/mapper/vgu01-lvu01
-
20G 3.2G 16G 17% /home
-
/dev/mapper/vgu01-lvu02
-
30G 33M 30G 1% /back
-
-
-- 218.78.186.162 run:uptime --
-
-
13:28:27 up 57 days, 1 min, 1 user, load average: 0.10, 0.11, 0.03
-
-
-- 218.78.186.162 run:tar -zcf /home/chenliang/tar.txt.tar.gz /home/chenliang/tar.txt; --
-
-
更完整版
-
#ssh_cmd.py
-
#coding:utf-8
-
-
import pexpect
-
-
def ssh_cmd(ip, user, passwd, cmd):
-
ssh = pexpect.spawn('ssh %s@%s "%s"' % (user, ip, cmd))
-
r = ''
-
try:
-
i = ssh.expect(['password: ', 'continue connecting (yes/no)?'])
-
if i == 0 :
-
ssh.sendline(passwd)
-
elif i == 1:
-
ssh.sendline('yes')
-
except pexpect.EOF:
-
ssh.close()
-
else:
-
r = ssh.read()
-
ssh.expect(pexpect.EOF)
-
ssh.close()
-
return r
-
-
hosts = '''
-
218.78.186.162:root:XXXXXX:df -h,uptime,tar -zcf /home/chenliang/tar.txt.tar.gz /home/chenliang/tar.txt;
-
'''
-
-
for host in hosts.split("\n"):
-
if host:
-
ip, user, passwd, cmds = host.split(":")
-
for cmd in cmds.split(","):
-
print "-- %s run:%s --" % (ip, cmd)
-
print ssh_cmd(ip, user, passwd, cmd)
- ssh並執行命令
-
-
[root@hostnfsd :/soft/python]$ more test2.py
-
#ssh_cmd.py
-
#coding:utf-8
-
-
import pexpect
-
import sys
-
ip='218.78.186.162'
-
user='root'
-
passwd='XXXXX'
-
cmd='df -h'
-
def ssh_cmd(vip,vuser,vpassword,vcmd):
-
child=pexpect.spawn('ssh %s@%s %s' % (vuser, vip,vcmd))
-
r = ''
-
child.expect('(?i)password:')
-
child.sendline(vpassword)
-
r = child.read()
-
return r
-
- print ssh_cmd(ip, user, passwd, cmd)
[root@hostnfsd :/soft/python]$ python test2.py
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
18G 15G 1.7G 90% /
tmpfs 1.9G 8.0K 1.9G 1% /dev/shm
/dev/xvda1 485M 119M 341M 26% /boot
/dev/mapper/vgu01-lvu01
20G 3.2G 16G 17% /home
/dev/mapper/vgu01-lvu02
30G 33M 30G 1% /back
-
[root@hostnfsd :/soft/python]$ more test2.py
-
向多臺機器ssh並執行多個命令
- 稍微修改一下就好了
-
#!/usr/bin/python
import pexpect
def ssh_cmd(ip, user, passwd, cmd):
ssh = pexpect.spawn('ssh %s@%s "%s"' % (user, ip, cmd))
r = ''
ssh.expect('(?i)password:')
ssh.sendline(passwd)
r = ssh.read()
return r
hosts = '''
218.78.186.162:root:xxxx:df -h,uptime,tar -zcf /home/chenliang/tar.txt.tar.gz /home/chenliang/tar.txt; -
218.78.186.163:root:xxxx:df -h,uptime,tar -zcf /home/chenliang/tar.txt.tar.gz /home/chenliang/tar.txt;
'''
for host in hosts.split("\n"):
if host:
ip, user, passwd, cmds = host.split(":")
for cmd in cmds.split(","):
print "-- %s run:%s --" % (ip, cmd)
print ssh_cmd(ip, user, passwd, cmd)
-
-
執行結果
-
[root@hostnfsd :/soft/python]$ python test1.py
-
-- 218.78.186.162 run:df -h --
-
-
Filesystem Size Used Avail Use% Mounted on
-
/dev/mapper/VolGroup-lv_root
-
18G 15G 1.7G 90% /
-
tmpfs 1.9G 8.0K 1.9G 1% /dev/shm
-
/dev/xvda1 485M 119M 341M 26% /boot
-
/dev/mapper/vgu01-lvu01
-
20G 3.2G 16G 17% /home
-
/dev/mapper/vgu01-lvu02
-
30G 33M 30G 1% /back
-
-
-- 218.78.186.162 run:uptime --
-
-
13:28:27 up 57 days, 1 min, 1 user, load average: 0.10, 0.11, 0.03
-
-
-- 218.78.186.162 run:tar -zcf /home/chenliang/tar.txt.tar.gz /home/chenliang/tar.txt; --
-
-
[root@hostnfsd :/soft/python]$ python test1.py
-
更完整版
-
#ssh_cmd.py
-
#coding:utf-8
-
-
import pexpect
-
-
def ssh_cmd(ip, user, passwd, cmd):
-
ssh = pexpect.spawn('ssh %s@%s "%s"' % (user, ip, cmd))
-
r = ''
-
try:
-
i = ssh.expect(['password: ', 'continue connecting (yes/no)?'])
-
if i == 0 :
-
ssh.sendline(passwd)
-
elif i == 1:
-
ssh.sendline('yes')
-
except pexpect.EOF:
-
ssh.close()
-
else:
-
r = ssh.read()
-
ssh.expect(pexpect.EOF)
-
ssh.close()
-
return r
-
-
hosts = '''
-
218.78.186.162:root:XXXXXX:df -h,uptime,tar -zcf /home/chenliang/tar.txt.tar.gz /home/chenliang/tar.txt;
-
'''
-
-
for host in hosts.split("\n"):
-
if host:
-
ip, user, passwd, cmds = host.split(":")
-
for cmd in cmds.split(","):
-
print "-- %s run:%s --" % (ip, cmd)
- print ssh_cmd(ip, user, passwd, cmd)
-
#ssh_cmd.py
練習二:
-
ssh遠端機器 並tar檔案再傳回本地
-
#!/usr/bin/python
-
import pexpect
-
import sys
-
ip='218.78.186.162'
-
passwd='XXXXXXX'
-
user='root'
-
cmd='gzip /home/chenliang/tar.txt'
-
source_dir='/home/chenliang/'
-
def ssh_m(vip,vuser,vpassword,vcmd):
-
child=pexpect.spawn('ssh %s@%s %s' % (vuser, vip,vcmd))
-
r = ''
-
child.expect('(?i)password:')
-
child.sendline(vpassword)
-
r = child.read()
-
child.sendline('exit')
-
return r
-
-
def scp_m(vip,vuser,vpassword,source_dir):
-
child=pexpect.spawn('scp %s@%s:/home/chenliang/tar.txt.gz %s' % (vuser, vip,source_dir))
-
r = ''
-
child.expect('(?i)password:')
-
child.sendline(vpassword)
-
r = child.read()
-
child.sendline('exit')
-
return r
-
-
print ssh_m(ip, user, passwd, cmd)
- print scp_m(ip, user, passwd,source_dir)
-
#!/usr/bin/python
疑問:
-
[root@hostnfsd :/soft/python]$ more test3.py
-
#!/usr/bin/python
-
import pexpect
-
import sys
-
ip='218.78.186.162'
-
user='root'
-
passwd='ESBecs00'
-
cmd='touch /home/chenliang/111t'
-
def ssh_cmd(vip,vuser,vpassword,vcmd):
-
child=pexpect.spawn('ssh %s@%s ' % (vuser, vip)) ----####如果將cmd移到下面
-
r = ''
-
child.expect('(?i)password:')
-
child.sendline(vpassword)
-
child.sendline('%s'%vcmd) ------#####命令移到這個部分,會tm的執行失敗,還不知道為什麼
-
r = child.read()
-
return r
-
- print ssh_cmd(ip, user, passwd, cmd)
。。。待續
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29096438/viewspace-2123669/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Python學習之模組Python
- 【python】python 模組學習之--FabricPython
- Python學習之常用模組Python
- python學習之argparse模組Python
- pexpect 學習
- Python學習之 datetime模組Python
- Python學習之模組與包Python
- Python學習之如何引用Python自定義模組?Python
- [Python模組學習] glob模組Python
- python開發學習之如何更好的引用Python模組?Python
- Python學習——xml模組PythonXML
- Python time模組學習Python
- python模組學習:CookiePythonCookie
- Python模組學習:atexitPython
- Python模組學習:fileinputPython
- Python學習——hashlib模組Python
- Python學習——shelve模組Python
- python 各種模組學習Python
- python optparse模組學習薦Python
- Python模組學習:urllibPython
- Python模組學習:datetimePython
- 小豬的Python學習之旅 —— 12.Python併發之queue模組Python
- python基礎學習16—-模組Python
- 學習Python的urllib模組Python
- Python學習——configparser模組Python
- python模組學習:anydbm, shelvePython
- python模組之collections模組Python
- python之模組Python
- Python之numpy學習Python
- python之pandas學習Python
- Python之Series 學習Python
- Python的shutil zipfile tarfile模組學習Python
- Python學習——logging模組Python
- Python模組學習:hashlib hash加密Python加密
- Python模組學習:copy 物件拷貝Python物件
- Python模組學習:subprocess 建立子程式Python
- Python學習筆記|Python之程式Python筆記
- Python模組之urllib模組Python