import re import sys,os import psutil import platform
defaddword(index,w,n): index.setdefault(w,[]).append(n) defget_net(port,hosts): dic = {} for host in hosts: for line in os.popen("ssh -p %s %s '/sbin/ifconfig'" % (port,host)): if'Ether'in line: mac = line.split()[4] net = line.split()[0].strip() elif'inet addr'in line: ip = line.split()[1].split(":")[1] addword(dic,host,net+'|'+mac+'|'+ip) #dic[host] = net+'|'+mac+'|'+ip+',' #print "%s|%s|%s," % (net,mac,ip) elif'lo'in line: break return dic defget_host(file): h_list =[] p = re.compile(r".*(\s+\w+\d+).*") with open(file,'r') as fh: content = [] for line in fh.readlines(): host = line.strip().split() if len(line) > 1: if host[1]== 'localhost': continue else: h_list.append(host[1]) return h_list
defmain(): list = get_host('/etc/hosts') net = get_net('22',list) for key in net: print key + '====>' + str(net[key])