diff --git a/src/linux_auto_dics_multi.py b/src/linux_auto_dics_multi.py index d94fc2f..582c18d 100644 --- a/src/linux_auto_dics_multi.py +++ b/src/linux_auto_dics_multi.py @@ -36,14 +36,14 @@ ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) -D42_API_URL = 'https://D42_IP_or_FQDN' #make sure to NOT to end in / -D42_USERNAME = 'D42USER' -D42_PASSWORD = 'D42PASS' +D42_API_URL = 'https://192.168.3.30' #make sure to NOT to end in / +D42_USERNAME = 'admin' +D42_PASSWORD = 'adm!nd42' USE_IP_RANGE = True -IP_RANGE = ['192.168.11.10', '192.168.11.202'] #Start and End IP. There is no validation in the script. Please make sure these are in same subnet. Valid if USE_IP_RANGE = True +IP_RANGE = ['192.168.3.103', '192.168.3.107'] #Start and End IP. There is no validation in the script. Please make sure these are in same subnet. Valid if USE_IP_RANGE = True NETWORKS = ['10.10.0.0/23', '10.11.8.0/23',] #End with , if a single network. always use / notation for the network. Only valid if USE_IP_RANGE = False -LINUX_USER = 'USER' -LINUX_PASSWORD = 'PASS' #Change USE_KEY_FILE to False if using password. password for linux servers. not required if using key file. +LINUX_USER = 'root' +LINUX_PASSWORD = 'P@ssw0rd' #Change USE_KEY_FILE to False if using password. password for linux servers. not required if using key file. USE_KEY_FILE = False #change this to true, if not using password. KEY_FILE = '/path/.ssh/id_rsa.pub' #key file name (with full path if not in same directory as the script) PORT = 22 #ssh port to use @@ -123,11 +123,11 @@ def grab_and_post_inventory_data(machine_name): print str(machine_name) + ': authentication failed' return None except Exception as err: - print str(machine_name) + ': ' + str(err) + print '\n[!] ' + str(machine_name) + ': ' + str(err) return None devargs = {} - print '[+] Connecting to: %s' % machine_name + print '\n[+] Connecting to: %s' % machine_name stdin, stdout, stderr = ssh.exec_command("/bin/hostname") data_err = stderr.readlines() data_out = stdout.readlines() @@ -183,7 +183,7 @@ def grab_and_post_inventory_data(machine_name): break if manufacturer != 'virtual' and GET_HARDWARE_INFO: devargs.update({'manufacturer': to_ascii(manufacturer).replace("# SMBIOS implementations newer than version 2.6 are not\n# fully supported by this version of dmidecode.\n", "").strip()}) - stdin, stdout, stderr = ssh.exec_command("sudo dmidecode -s system-product- name") + stdin, stdout, stderr = ssh.exec_command("sudo dmidecode -s system-product-name") data_err = stderr.readlines() data_out = stdout.readlines() if not data_err: @@ -279,63 +279,102 @@ def grab_and_post_inventory_data(machine_name): ADDED, msg = post(devargs, 'device') if ADDED: - print str(machine_name) + ': ' + msg['msg'][0] device_name_in_d42 = msg['msg'][2] stdin, stdout, stderr = ssh.exec_command("/sbin/ifconfig -a") #TODO add just macs without IPs data_err = stderr.readlines() data_out = stdout.readlines() if not data_err: - ipinfo = stdout.readlines() - + ipinfo = data_out + # ======= MAC only=========# - for rec in ipinfo: - if 'hwaddr' in rec.lower(): - mac = re.search(r'([0-9A-F]{2}[:-]){5}([0-9A-F]{2})', rec, re.I).group() - print 'MAC: %s' % mac - print rec.split("\n")[0].split()[0] - mac_address = { - 'macaddress' : mac, - 'port_name': rec.split("\n")[0].split()[0], - 'device' : device_name_in_d42, - 'override': 'smart' - } - ADDED, msg_mac = post(mac_address, 'mac') - if ADDED: - print mac + ': ' + str(msg_mac) - else: - print mac + ': failed with message = ' + str(msg_mac) - print '\n\n' + if ipinfo: + ipinfo_mac = ''.join(ipinfo).split('\n\n') + for nic in ipinfo_mac: + if nic not in ('', '\n'): + splitted = nic.split('\n') + tag = (splitted[0]).split()[0].rstrip(':') + for row in splitted: + if 'ether' in row.lower().split() or 'hwaddr' in row.lower().split(): + mac = re.search(r'([0-9A-F]{2}[:-]){5}([0-9A-F]{2})', row, re.I).group() + mac_address = { + 'macaddress' : mac, + 'port_name': tag, + 'device' : device_name_in_d42, + 'override': 'smart' + } + ADDED, msg_mac = post(mac_address, 'mac') + if ADDED: + print '\n\t[!] MAC Response: '+mac + ': ' + str(msg_mac).strip() + else: + print '\n\t[!] MAC Response: '+mac + ': failed with message = ' + str(msg_mac).strip() # ======= / MAC only =========# - - for i, item in enumerate(ipinfo): - if 'Ethernet' in item: - if 'inet addr' in ipinfo[i+1]: - ipv4_address = ipinfo[i+1].split()[1].replace('addr:', '') + + + if ipinfo: + ipinfo_ip = ''.join(ipinfo).split('\n\n') + for nic in ipinfo_ip: + #print nic + if nic not in ('', '\n'): + splitted = nic.split('\n') + tag = (splitted[0]).split()[0].rstrip(':') + + ipv4_address = None + ipv6_address = None + + for row in splitted: + rowdata= ' '.join(row.lower().split(':')).split() + + try: + if 'hwaddr' in rowdata: + mac = row.split()[-1] + if rowdata[0] == 'ether': + mac = row.split()[1] + if rowdata[0] == 'inet' and rowdata[1] == 'addr': + ipv4_address = rowdata[2] + if rowdata[0] == 'inet' and not rowdata[1] == 'addr': + ipv4_address = rowdata[1] + if 'inet6 addr:' in row: + ipv6_address =row.split()[2].split('/')[0] + if rowdata[0] == 'inet6' and not rowdata[1] == 'addr': + ipv6_address = row.split()[1].split('/')[0] + except IndexError: + pass + except Exception, e: + print '\n[!] Exception! Message was: %s ' % str(e) + print '\tData: %s' % nic + pass + + if not tag == 'lo' and ipv4_address: ip = { 'ipaddress': ipv4_address, - 'tag': item.split("\n")[0].split()[0], - 'macaddress' : item.split("\n")[0].split()[4], - 'device' : device_name_in_d42, - } - ADDED, msg_ip = post(ip, 'ip') - if ADDED: - print ipv4_address + ': ' + str(msg_ip) - else: - print ipv4_address + ': failed with message = ' + str(msg_ip) - if uploadipv6 and ('inet6 addr' in ipinfo[i+1] or 'inet6 addr' in ipinfo [i+2]): - if 'inet6 addr' in ipinfo[i+1]: ipv6_address = ipinfo[i+1].split()[2 ].split('/')[0] - else: ipv6_address = ipinfo[i+2].split()[2].split('/')[0] - ip = { - 'ipaddress': ipv6_address, - 'tag': item.split("\n")[0].split()[0], - 'macaddress' : item.split("\n")[0].split()[4], + 'tag': tag, + 'macaddress' : mac, 'device' : device_name_in_d42, } ADDED, msg_ip = post(ip, 'ip') if ADDED: - print ipv6_address + ' : ' + str(msg_ip) + print '\n\t[!] Response: '+ ipv4_address + ': ' + str(msg_ip) else: - print ipv6_address + ': failed with message = ' + str(msg_ip) + print '\n\t[!] Response: '+ ipv4_address + ': failed with message = ' + str(msg_ip) + + if uploadipv6 and ipv6_address: + ip = { + 'ipaddress': ipv6_address, + 'tag': tag, + 'macaddress' : mac, + 'device' : device_name_in_d42, + } + ADDED, msg_ip = post(ip, 'ip') + if ADDED: + print '\n\t[!] Response: '+ipv6_address + ' : ' + str(msg_ip) + else: + print '\n\t[!] Response: '+ipv6_address + ': failed with message = ' + str(msg_ip) + elif not ipv6_address: + if DEBUG: + print '\n[!] Cannot find/parse IPv6 address. Data was:\n\t%s' % nic + elif not ipv4_address: + if DEBUG: + print '\n[!] Cannot find/parse IPv4 address. Data was:\n\t%s' % nic else: if DEBUG: print data_err