「pythonの学習」pythonでパッチ処理/並行処理を実現サンプルプログラム

pythonコード:
#-*- coding: utf-8 -*-
#!/usr/bin/python
import paramiko
import threading
def ssh2(ip,username,passwd,cmd):
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip,22,username,passwd,timeout=5)
for m in cmd:
stdin,stdout,stderr = ssh.exec_command(m)
stdin.write(“Y")
out = stdout.readlines()
for o in out:
print o,
print '[OK]%s’ %(ip),
print '=========================================================================’
ssh.close()
except:
print '[Error]%s’ %(ip),
print '=========================================================================’
def main():
cmd = ['uptime’] #コマンドを実行
username = “root"
passwd = “startnews24_pwd"
threads = [4]
f = file('list.txt’) #IPリスト
while True:
ip = f.readline()
if len(ip) == 0:
break
a = threading.Thread(target=ssh2,args=(ip,username,passwd,cmd))
a.start()
f.close()
if __name__ == '__main__’:
main()
実行結果:
[root@bw-vm-soft ~]# python ssh2.py
13:31:28 up 514 days, 7:28, 1 user, load average: 10.27, 9.44, 9.03
[OK]192.168.13.116
=========================================================================
13:31:28 up 514 days, 8:04, 1 user, load average: 5.99, 6.05, 6.46
[OK]192.168.13.117

Development

Posted by arkgame