def run(self): hosts = defaultdict() ipl_begin = ip2long(self.ip_begin) ipl_end = ip2long(self.ip_end) try: c = Controller(self.controller, self.username, self.password) for client in c.get_clients(): ip = client.get('ip', False) if not ip: continue ipl_cur = ip2long(ip) if ipl_cur >= ipl_begin and ipl_cur <= ipl_end: hosts[ip] = client['mac'].lower() self.hosts = hosts except: # somethimes a weird SSL Handshake errors appears in combination # with the ubiquiti tomcat server. # # see: http://stackoverflow.com/questions/14167508/intermittent-sslv3-alert-handshake-failure-under-python return False
def parse(self, output): hosts = defaultdict() ipl_begin = ip2long(self.ip_begin) ipl_end = ip2long(self.ip_end) # parse all mac and ip combinations from string matches = re.findall(r"\(((?:\d{1,3}\.){3}\d{1,3})\) at ((?:[0-9A-F]{2}\:){5}[0-9A-F]{2})", output, re.I) for match in matches: ip = match[0] ipl_cur = ip2long(ip) if ipl_cur >= ipl_begin and ipl_cur <= ipl_end: hosts[ip] = match[1].lower() return hosts
def main(): collectors = [] # ====== add ubuquiti ====== ubnt = settings.ubiquiti x = ubiquiti.Collector(settings.network, ubnt['host'], ubnt['user'], ubnt['pass']) x.start() collectors.append(x) # ====== add arp ======= x = arp.Collector(settings.network) x.start() collectors.append(x) # ====== add nmap ====== x = nmap.Collector(settings.network) x.start() collectors.append(x) # waiting for collectors... for collector in collectors: collector.join() hosts = {} for collector in collectors: hosts = dict(hosts.items() + collector.get_hosts().items()) # create database connection dbcron = MySQLdb.connect(host=settings.mysql_host, user=settings.mysql_user, passwd=settings.mysql_pass, db=settings.mysql_name) # write hosts to database dbcursor = dbcron.cursor() # add ALL THE HOSTS to the database for ip,mac in hosts.iteritems(): longip = ip2long(ip) dbcursor.execute("INSERT INTO alive_hosts (macaddr, iplong, erfda) VALUES ('%s', %d, NOW())" % (mac.lower() , longip)) dbcursor.close() # delete everything with higher privacy than 3 dbcursor = dbcron.cursor() dbcursor.execute("DELETE FROM alive_hosts WHERE EXISTS(SELECT 1 FROM mac_to_nick as t2 WHERE t2.privacy > 3 AND t2.macaddr = alive_hosts.macaddr LIMIT 1)") dbcursor.close() # close database connection dbcron.commit() dbcron.close()
def get_next_ip(s): """ip=ip+1, include x.x.x.0 and x.x.x.255, :param s: string type of ip . :returns: ip+1 :raises: """ return str(long2ip(ip2long(s) + 1))
def main(): collectors = [] # ====== add ubuquiti ====== ubnt = settings.ubiquiti x = ubiquiti.Collector(settings.network, ubnt['host'], ubnt['user'], ubnt['pass']) x.start() collectors.append(x) # ====== add arp ======= x = arp.Collector(settings.network) x.start() collectors.append(x) # ====== add nmap ====== x = nmap.Collector(settings.network) x.start() collectors.append(x) # waiting for collectors... for collector in collectors: collector.join() # results print "The results are in!" hosts = {} for collector in collectors: print collector.get_hosts() hosts = dict(hosts.items() + collector.get_hosts().items()) # create database connection dbcron = MySQLdb.connect(host=settings.mysql_host, user=settings.mysql_user, passwd=settings.mysql_pass, db=settings.mysql_name) # write hosts to database dbcursor = dbcron.cursor() # add ALL THE HOSTS to the database for ip, mac in hosts.iteritems(): longip = ip2long(ip) dbcursor.execute( "INSERT INTO alive_hosts (macaddr, iplong, erfda) VALUES ('%s', %d, NOW())" % (mac.lower(), longip)) dbcursor.close() # close database connection dbcron.commit() dbcron.close()
def parse(self, output): hosts = defaultdict() ipl_begin = ip2long(self.ip_begin) ipl_end = ip2long(self.ip_end) # parse all mac and ip combinations from string matches = re.findall( r'\(((?:\d{1,3}\.){3}\d{1,3})\) at ((?:[0-9A-F]{2}\:){5}[0-9A-F]{2})', output, re.I) for match in matches: ip = match[0] ipl_cur = ip2long(ip) if ipl_cur >= ipl_begin and ipl_cur <= ipl_end: hosts[ip] = match[1].lower() return hosts
def main(): collectors = [] # ====== add ubuquiti ====== ubnt = settings.ubiquiti x = ubiquiti.Collector(settings.network, ubnt['host'], ubnt['user'], ubnt['pass']) x.start() collectors.append(x) # ====== add arp ======= x = arp.Collector(settings.network) x.start() collectors.append(x) # ====== add nmap ====== x = nmap.Collector(settings.network) x.start() collectors.append(x) # waiting for collectors... for collector in collectors: collector.join() # results print "The results are in!" hosts = {} for collector in collectors: print collector.get_hosts() hosts = dict(hosts.items() + collector.get_hosts().items()) # create database connection dbcron = MySQLdb.connect(host=settings.mysql_host, user=settings.mysql_user, passwd=settings.mysql_pass, db=settings.mysql_name) # write hosts to database dbcursor = dbcron.cursor() # add ALL THE HOSTS to the database for ip,mac in hosts.iteritems(): longip = ip2long(ip) dbcursor.execute("INSERT INTO alive_hosts (macaddr, iplong, erfda) VALUES ('%s', %d, NOW())" % (mac.lower() , longip)) dbcursor.close() # close database connection dbcron.commit() dbcron.close()
def get_next_valid_ip(s): """ip = ip + 1, omit x.x.x.0 and x.x.x.255, :param s: string type of ip . :returns: string type of ip+1 :raises: """ b = str(long2ip(ip2long(s) + 1)) if b.split('.')[3] == "0" or b.split('.')[3] == "255": b = get_next_valid_ip(b) return b
def get_next_ip(ports): max_ip = -1 for port in ports: fixed_ip = port["fixed_ips"] if not fixed_ip: continue fixed_ip = fixed_ip[0]["ip_address"] max_ip = max(max_ip, ip2long(fixed_ip)) if max_ip <= 0: raise Exception("Next IP address could not be determined" " (You have no existing Fixed IPs!)") new_fixed_ip = long2ip(max_ip + 1) return new_fixed_ip
def get_next_ip(ports): max_ip = -1 for port in ports: fixed_ip = port['fixed_ips'] if not fixed_ip: continue fixed_ip = fixed_ip[0]['ip_address'] max_ip = max(max_ip, ip2long(fixed_ip)) if max_ip <= 0: raise Exception("Next IP address could not be determined" " (You have no existing Fixed IPs!)") new_fixed_ip = long2ip(max_ip + 1) return new_fixed_ip
def _get_next_fixed_ip(ports): """ Expects the output from user-specific neutron port-list. will determine the next available fixed IP by 'counting' the highest allocated IP address and adding one to it. """ try: from iptools.ipv4 import ip2long, long2ip except ImportError: raise Exception("For this script, we need iptools. pip install iptools") max_ip = -1 for port in ports: fixed_ip = port["fixed_ips"] if not fixed_ip: continue fixed_ip = fixed_ip[0]["ip_address"] max_ip = max(max_ip, ip2long(fixed_ip)) if max_ip <= 0: raise Exception("Next IP address could not be determined" " (You have no existing Fixed IPs!)") new_fixed_ip = long2ip(max_ip + 1) return new_fixed_ip
def _get_next_fixed_ip(ports): """ Expects the output from user-specific neutron port-list. will determine the next available fixed IP by 'counting' the highest allocated IP address and adding one to it. """ try: from iptools.ipv4 import ip2long, long2ip except ImportError: raise Exception("For this script, we need iptools. pip install iptools") max_ip = -1 for port in ports: fixed_ip = port['fixed_ips'] if not fixed_ip: continue fixed_ip = fixed_ip[0]['ip_address'] max_ip = max(max_ip, ip2long(fixed_ip)) if max_ip <= 0: raise Exception("Next IP address could not be determined" " (You have no existing Fixed IPs!)") new_fixed_ip = long2ip(max_ip + 1) return new_fixed_ip
def get_prep_value(self, value): if ':' in value: return 'ipv6' + str(ipv6.ip2long(value.strip())) elif '.' in value: return 'ipv4' + str(ipv4.ip2long(value.strip())) return ''