python 利用pexpect进行多机远程命令执行
生活随笔
收集整理的這篇文章主要介紹了
python 利用pexpect进行多机远程命令执行
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在安裝之前,確認(rèn)你的機(jī)器安裝了python,和easy_install.通常python是自動(dòng)安裝的,如果沒(méi)有安裝easy_install,那么wget -q?http://peak.telecommunity.com/dist/ez_setup.py?獲取一下 python ez_setup.py pexpect是python一個(gè)模塊,可以通過(guò):easy_install pexpect?來(lái)安裝。
這里主要是用pexpect執(zhí)行ssh,查看遠(yuǎn)程uptime和df -h看硬盤狀況。 #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 = '''
192.168.0.12:smallfish:1234:df -h,uptime
192.168.0.13:smallfish:1234:df -h,uptime
'''
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)
本文轉(zhuǎn)自elbertchen 51CTO博客,原文鏈接:http://blog.51cto.com/linkyou/751020,如需轉(zhuǎn)載請(qǐng)自行聯(lián)系原作者
這里主要是用pexpect執(zhí)行ssh,查看遠(yuǎn)程uptime和df -h看硬盤狀況。 #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 = '''
192.168.0.12:smallfish:1234:df -h,uptime
192.168.0.13:smallfish:1234:df -h,uptime
'''
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)
本文轉(zhuǎn)自elbertchen 51CTO博客,原文鏈接:http://blog.51cto.com/linkyou/751020,如需轉(zhuǎn)載請(qǐng)自行聯(lián)系原作者
總結(jié)
以上是生活随笔為你收集整理的python 利用pexpect进行多机远程命令执行的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 笨人可以学计算机吗,为什么有的笨人一旦开
- 下一篇: html dom反选,HTML DOM系