import re import argparse import paramiko from netaddr import IPAddress def scanparams(): parser = argparse.ArgumentParser(description='scan hardware at remote host using /proc /sys data via ssh connection.') parser.add_argument("scanedhost", help="IP or hostname for detecting hardware", type = str, default = '127.0.0.1') parser.add_argument("-s", "--sshkey", help="path to the ssh private file ", default = './.ssh/id_rsa') args = parser.parse_args() hostlist = args.scanedhost.split(",") print("args: {0}; {1}".format(args.scanedhost, args.sshkey)) return args.sshkey, hostlist def detecthostname(client): """ """ stdin,stdout,stderr = client.exec_command('hostname') return stdout.readlines()[0] def detectcpu(client): """ detecting cpu models and count it""" stdin,stdout,stderr = client.exec_command('cat /proc/cpuinfo') cpuinfotext = stdout.readlines() cpucount = 0 cpuname = 'not detected' for mystr in cpuinfotext: m = re.search('(^model\ name\t)(?P.*)', mystr) if m is not None: cpucount +=1 cpuname = m.group('cpuname') return cpuname, cpucount def detectmemory(client): """ """ stdin,stdout,stderr = client.exec_command('cat /proc/meminfo') return stdout.readlines()[0].split()[1] def detectdrives(client): """ """ stdin,stdout,stderr = client.exec_command('ls /sys/block') devicelist = stdout.readlines() localdrivelist = [] drives = {} for localdrive in devicelist: m = re.search('(?Ph|sd.)',localdrive) if m is not None: localdrivelist.append(m.group('drivename')) for selecteddrive in localdrivelist: drives[selecteddrive] = {} stdin,stdout,stderr = client.exec_command('cat /sys/class/block/{0}/device/model'.format(selecteddrive)) drives[selecteddrive]['model'] = stdout.readlines()[0].rstrip() stdin,stdout,stderr = client.exec_command('cat /sys/block/{0}/size'.format(selecteddrive)) drivesize = int(stdout.readlines()[0])/2028 drives[selecteddrive]['size'] = drivesize return drives def detectnetworkcards(client): """ detect netcar procedure""" busvendordict = {} namesettingdict = {} netcardlist = [] stdin,stdout,stderr = client.exec_command("lspci") etherlist= stdout.readlines() for mystr in etherlist: m = re.search('(?P^[0-9a-fA-F]{2}:[0-9a-fA-F]{2}\.[0-9a-fA-F]) Ethernet controller: (?P.*)', mystr) if m is not None: busvendordict[m.group('hwid')] = m.group('vendorname') netcardlist.append({'hardwareid':m.group('hwid'),'vendorname':m.group('vendorname')}) for hwid in busvendordict: stdin,stdout,stderr = client.exec_command('ls /sys/bus/pci/devices/0000\:{0}/net/'.format(hwid)) systemname = stdout.readlines()[0].rstrip() for card in netcardlist: if card['hardwareid'] == hwid: card.update({'systemname':systemname}) namesettingdict[systemname] = {} namesettingdict[systemname]['systemname'] = hwid namesettingdict[systemname]['vendorname'] = busvendordict[hwid] netcard = {'systemname': hwid, 'vendorname':busvendordict[hwid]} stdin,stdout,stderr = client.exec_command('cat /proc/net/route') routestings = stdout.readlines() for mystr in routestings: st = mystr.split('\t') if ((st[2] == '00000000') and (st[7] != 'FFFFFFFF') and (st[1] != '0000FEA9')): IPnetwork = '.'.join([str(int(st[1][i-2:i], 16)) for i in reversed(range(2,10,2))]) IPnetmask = '.'.join([str(int(st[7][i-2:i], 16)) for i in reversed(range(2,10,2))]) IPnetmaskhex = ''.join([st[7][i-2:i] for i in reversed(range(2,10,2))]) IPnetmaskshortform = bin(int(IPnetmaskhex,16)).count("1") logicalinterfacename = st[0] for netcard in netcardlist: if re.match('{0}.*'.format(netcard['systemname']), logicalinterfacename): if not netcard.has_key('logicalname'): netcard.update({'logicalname': list()}) netcard['logicalname'].append({'name':logicalinterfacename, 'IPv4':'0.0.0.0', 'IPnetwork':IPnetwork, 'IPnetmask':IPnetmask, 'IPnetmaskshortform': IPnetmaskshortform}) stdin,stdout,stderr = client.exec_command('cat /proc/net/fib_trie') fib_trie_result = stdout.readlines() if (len(fib_trie_result) == 0): print( "No information about IPs !") return netcardlist for pos, mystr in enumerate(fib_trie_result): if re.match( r'(.*)host(.*)', mystr): localIP = re.search('(?P\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3})', fib_trie_result[pos-1]) if localIP is None: pass Network = re.search('(?P\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3})/(?P\d{1,2})', fib_trie_result[pos-5]) if Network is not None: for netcard in netcardlist: try: if not 'logicalname' in netcard: continue for linterface in netcard['logicalname']: logicalinterface = linterface['name'] if (int(Network.group('Netmask')) == linterface['IPnetmaskshortform']) and (Network.group('Network')==linterface['IPnetwork']): linterface['IPv4'] = localIP.group('IP') except: print ("Exception netcard structure : {0} 1: {1}".format(netcard, netcard[1])) return netcardlist def main(): """ main function for scanning hardware using ssh connection""" sshkeyfile, hostlist = scanparams() for hostname in hostlist: print("="*50) print("trying to get information from host: {0}".format(hostname)) print("="*50) client = paramiko.SSHClient() client.load_system_host_keys() client.load_host_keys(sshkeyfile) client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect(hostname, username='root') print("*"*50) print("Hostname:{0}".format(detecthostname(client))) cpuname, cpucount = detectcpu(client) print ("*"*20+"CPU information"+"*"*20) print("CPUCount: {0}\n CPU model: {1}".format(cpucount, cpuname)) print("*"*30+"Memory information"+"*"*20) print(("Momory size in kb: {0}\n"+"="*30).format(detectmemory(client))) print("*"*20+"Netcard's information'"+"*"*20) netcardlist = detectnetworkcards(client) drives = detectdrives(client) print("{0}\n {1}\n {0}".format("#"*50, netcardlist)) print("*"*20+"Drives information"+"*"*20) for drive in drives: print("Drive system name:/dev/{0}\nDrive vendor:{1}\nDrive size: {2} MB".format(drive, drives[drive]['model'], drives[drive]['size'])) client.close() if __name__=="__main__": main()