#!/usr/bin/python#-*-coding:utf-8-*-' Create on 2014.12.13@author: feifei ' import Osimport sysimport paramikoips = []def read_ip (ipfile): try: if os.path.isfile (ipfile): allip = open (ipfile, ' R ') for ip in allip: ips.append (Ip.strip ()) allip.close () else: sys.exit () except:print ' Open ip.txt is failure. ' Sys.exit () def remote_action (): username= ' root ' pkey= '/root/ . Ssh/id_rsa ' port=22 fail_host = [] for hostname in ips: try: paramiko.util.log_to_file (' Paramiko.log ') key=paramiko. Rsakey.from_private_key_file (Pkey) s=paramiko. Sshclient () s.load_system_host_keys () s.connect (Hostname,port, Username,pkey=key) print '%s: ' % hostname stdin, Stdout,stderr=s.exec_command (' hostname;free m ') print stdout.read () s.close () Except: fail_host.append (hostname) if not fail_host:print ' \033[1;31;40mall host is succeed.\033[0m ' else: print "\033[1;31;40mplease check fail host:\033[0m ",         FOR&NBSp;index, value in enumerate (fail_host): print "\033[1;31;40m%s\033[0m" % value,if __name__ == "__main__": read_ip (' Ip.txt ') remote_action ()
PS: Use Python to implement remote operations on multiple target hosts.
Python multi-Master remote operation