#!/usr/bin/python
#-*-coding:utf-8-*-
'''
create on 2014.12.13
@author: feifei
'''

import os
import sys
import paramiko

ips = []
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 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: 使用python实现对多台目标主机的远程操作:可以查看远程主机的主机名和内存等信息。