亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 7268 | 回復(fù): 1
打印 上一主題 下一主題

pexpect通過ssh執(zhí)行遠(yuǎn)程命令 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2009-08-17 15:50 |只看該作者 |倒序瀏覽
原文參見:http://hi.baidu.com/smallfish_xy ... 5a86202cf53447.html

pexpect是python一個模塊,可以通過:easy_install pexpect 來安裝。

這里主要是用pexpect執(zhí)行ssh,查看遠(yuǎn)程uptime和df -h看硬盤狀況。

  1. #ssh_cmd.py
  2. #coding:utf-8

  3. import pexpect

  4. def ssh_cmd(ip, user, passwd, cmd):
  5.     ssh = pexpect.spawn('ssh %s@%s "%s"' % (user, ip, cmd))
  6.     r = ''
  7.     try:
  8.         i = ssh.expect(['password: ', 'continue connecting (yes/no)?'])
  9.         if i == 0 :
  10.             ssh.sendline(passwd)
  11.         elif i == 1:
  12.             ssh.sendline('yes')
  13.     except pexpect.EOF:
  14.         ssh.close()
  15.     else:
  16.         r = ssh.read()
  17.         ssh.expect(pexpect.EOF)
  18.         ssh.close()
  19.     return r

  20. hosts = '''
  21. 192.168.0.12:smallfish:1234:df -h,uptime
  22. 192.168.0.13:smallfish:1234:df -h,uptime
  23. '''

  24. for host in hosts.split("\n"):
  25.     if host:
  26.         ip, user, passwd, cmds = host.split(":")
  27.         for cmd in cmds.split(","):
  28.             print "-- %s run:%s --" % (ip, cmd)
  29.             print ssh_cmd(ip, user, passwd, cmd)
復(fù)制代碼


hosts數(shù)組格式是:主機IP:用戶名:密碼:命令 (多個命令用逗號, 隔開)
可以看出打印出相應(yīng)的結(jié)果了,可以拼成html發(fā)送mail看起來比較美觀些咯!

論壇徽章:
0
2 [報告]
發(fā)表于 2009-08-18 14:12 |只看該作者
借鑒了
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP