def parse_state(data): state = {} for line in data.splitlines(): parts = line.split(',') if args.debug: debug("=== begin split line\n{0!s}\n=== end split line".format( parts)) if parts[0].startswith('>INFO') or \ parts[0].startswith('END') or \ parts[0].startswith('>CLIENT'): continue else: state['up_since'] = get_date(date_string=parts[0], uts=True) state['connected'] = parts[1] state['success'] = parts[2] if parts[3]: state['local_ip'] = ip_address(parts[3]) else: state['local_ip'] = '' if parts[4]: state['remote_ip'] = ip_address(parts[4]) state['mode'] = 'Client' else: state['remote_ip'] = '' state['mode'] = 'Server' return state
def parse_state(data): state = {} for line in data.splitlines(): parts = line.split(',') if args.debug: debug("=== begin split line\n{0!s}\n=== end split line".format(parts)) if parts[0].startswith('>INFO') or \ parts[0].startswith('END') or \ parts[0].startswith('>CLIENT'): continue else: state['up_since'] = get_date(date_string=parts[0], uts=True) state['connected'] = parts[1] state['success'] = parts[2] if parts[3]: state['local_ip'] = ip_address(parts[3]) else: state['local_ip'] = '' if parts[4]: state['remote_ip'] = ip_address(parts[4]) state['mode'] = 'Client' else: state['remote_ip'] = '' state['mode'] = 'Server' return state
def get_ip_address(self, host): try: return ip_address(host) except Exception: try: return ip_address(self.resolve(host, dirty=True)[0][1]) except Exception: return ip_address(u'0.0.0.0')
def get_ip_address(self, host): try: return ip_address(unicode(host)) except: try: return ip_address(unicode(self.resolve(host)[0][1])) except: return ip_address(u'8.8.8.8')
def get_ip_address(host): try: return ip_address(host) except: try: return ip_address(getaddrinfo(host)[0][4][1]) except: return ip_address('0.0.0.0')
def get_ip_address(self, host): try: return ip_address(host) except Exception: try: return ip_address(self.resolve(host, dirty=True)[0][1]) except Exception: return ip_address(u"0.0.0.0")
def get_ip_address(self, host): try: return ip_address(unicode(host)) except Exception: try: return ip_address(unicode(self.resolve(host, dirty=True)[0][1])) except Exception: return ip_address(u'0.0.0.0')
def findIPs( self, start, end ): start = ip_address(str(start)) end = ip_address(str(end)) result = [] while start <= end: result.append(str(start)) start += 1 return result
def IPduan(ipl): sides = ipl.split('-') startip = sides[0] endip = sides[1] startip = ip_address(startip) endip = ip_address(endip) while startip <= endip: ipList.append(str(startip)) startip += 1
def get_ip_address(self, host): logger.debug('entering %s.get_ip_address(%s)' % (self.__class__.__name__, host)) try: return ip_address(host) except Exception: try: return ip_address(self.resolve(host, dirty=True)[0][1]) except Exception: return ip_address(u'0.0.0.0')
def IPyu(ipl): sides = ipl.split('-') startip = sides[0] endip = sides[1] ipf = IP(startip) ipe = IP(endip) startip = ip_address(ipf[0]) endip = ip_address(ipe[-1]) while startip <= endip: ipList.append(str(startip)) startip += 1
def main(): """Entry point.""" options = parse() setup_logging(options.debug, options.silent, options.name, options.syslog_facility, not options.no_syslog) if options.pid: options.pid.write("{0}\n".format(os.getpid())) options.pid.close() try: # Setup IP to use options.ips = options.ips or loopback_ips(options.label) if not options.ips: logger.error("No IP found") sys.exit(1) if options.ip_setup: setup_ips(options.ips, options.label, options.sudo) drop_privileges(options.user, options.group) # Parse defined networks into a list of IPs for advertisement if options.deaggregate_networks: options.ips = [ip_address(ip) for net in options.ips for ip in net] options.ips = collections.deque(options.ips) options.ips.rotate(-options.start_ip) options.ips = list(options.ips) # Main loop loop(options) except Exception as e: # pylint: disable=W0703 logger.exception("Uncaught exception: %s", e) sys.exit(1)
def loopback_ips(label): """Retrieve loopback IP addresses""" logger.debug("Retrieve loopback IP addresses") addresses = [] if sys.platform.startswith("linux"): # Use "ip" (ifconfig is not able to see all addresses) ipre = re.compile(r"^(?P<index>\d+):\s+(?P<name>\S+)\s+inet6?\s+" r"(?P<ip>[\da-f.:]+)/(?P<netmask>\d+)\s+.*") labelre = re.compile(r".*\s+lo:(?P<label>\S+)\s+.*") cmd = subprocess.Popen("/sbin/ip -o address show dev lo".split(), shell=False, stdout=subprocess.PIPE) else: # Try with ifconfig ipre = re.compile(r"^inet6?\s+(alias\s+)?(?P<ip>[\da-f.:]+)\s+" r"(?:netmask 0x(?P<netmask>[0-9a-f]+)|" r"prefixlen (?P<mask>\d+)).*") cmd = subprocess.Popen("/sbin/ifconfig lo0".split(), shell=False, stdout=subprocess.PIPE) labelre = re.compile(r"") for line in cmd.stdout: line = line.decode("ascii", "ignore").strip() mo = ipre.match(line) if not mo: continue ip = ip_address(mo.group("ip")) if not ip.is_loopback: if label: lmo = labelre.match(line) if not lmo or not lmo.group("label").startswith(label): continue addresses.append(ip) logger.debug("Loopback addresses: %s", addresses) return addresses
def main(): """Entry point.""" options = parse() setup_logging(options.debug, options.silent, options.name, options.syslog_facility, not options.no_syslog) if options.pid: options.pid.write("{0}\n".format(os.getpid())) options.pid.close() try: # Setup IP to use options.ips = options.ips or loopback_ips(options.label) if not options.ips: logger.error("No IP found") sys.exit(1) if options.ip_setup: setup_ips(options.ips, options.label) drop_privileges(options.user, options.group) # Parse defined networks into a list of IPs for advertisement if options.deaggregate_networks: options.ips = [ip_address(ip) for net in options.ips for ip in net] options.ips = collections.deque(options.ips) options.ips.rotate(-options.start_ip) options.ips = list(options.ips) # Main loop loop(options) except Exception as e: # pylint: disable=W0703 logger.exception("Uncaught exception: %s", e) sys.exit(1)
def loopback_ips(label): """Retrieve loopback IP addresses""" logger.debug("Retrieve loopback IP addresses") addresses = [] if sys.platform.startswith("linux"): # Use "ip" (ifconfig is not able to see all addresses) ipre = re.compile(r"^(?P<index>\d+):\s+(?P<name>\S+)\s+inet6?\s+" r"(?P<ip>[\da-f.:]+)/(?P<netmask>\d+)\s+.*") labelre = re.compile(r".*\s+lo:(?P<label>\S+).*") cmd = subprocess.Popen("/sbin/ip -o address show dev lo".split(), shell=False, stdout=subprocess.PIPE) else: # Try with ifconfig ipre = re.compile(r"^inet6?\s+(alias\s+)?(?P<ip>[\da-f.:]+)\s+" r"(?:netmask 0x(?P<netmask>[0-9a-f]+)|" r"prefixlen (?P<mask>\d+)).*") cmd = subprocess.Popen("/sbin/ifconfig lo0".split(), shell=False, stdout=subprocess.PIPE) labelre = re.compile(r"") for line in cmd.stdout: line = line.decode("ascii", "ignore").strip() mo = ipre.match(line) if not mo: continue ip = ip_address(mo.group("ip")) if not ip.is_loopback: if label: lmo = labelre.match(line) if not lmo or not lmo.group("label").startswith(label): continue addresses.append(ip) logger.debug("Loopback addresses: %s", addresses) return addresses
def resolve(self, host, dirty=False): try: ip = ip_address(host) return [ (2 if ip._version == 4 else 10, host), ] except Exception: return _resolver(host)
def geoip(self, value): if not value: value = None try: valid = str(ip_address(value)) except ValueError: valid = None self._geoip = valid
def resolve(self, host, dirty=False): logger.debug('entering %s.resolve(%s)' % (self.__class__.__name__, host)) try: ip = ip_address(host) return [ (2 if ip._version == 4 else 10, host), ] except Exception: return _resolver(host)
def addhost(host, ip): try: ipo = ip_address(ip) if isinstance(ipo, IPv4Address): self.HOSTS[host].append((2, ip)) else: self.HOSTS[host].append((10, ip)) except Exception: self.logger.warning('unsupported host: %s' % ip) sys.stderr.write(traceback.format_exc() + '\n') sys.stderr.flush()
def check_ipv4(ipv4): """Checks for valid ipv4 addresses. """ while True: try: ip = ipaddr.ip_address(ipv4) break except ValueError: print '[!] - IPv4 {0} is not valid!'.format(ipv4) ipv4 = raw_input('Enter a valid IPv4: ') return ipv4
def _response_for(self, path, model_class, ip_address): if ip_address != 'me': ip_address = str(ipaddress.ip_address(ip_address)) uri = '/'.join([self._base_uri, path, ip_address]) response = requests.get(uri, auth=(self._user_id, self._license_key), headers={'Accept': 'application/json', 'User-Agent': self._user_agent()}) if response.status_code == 200: body = self._handle_success(response, uri) return model_class(body, locales=self._locales) else: self._handle_error(response, uri)
def _response_for(self, path, model_class, ip_address): if ip_address != 'me': ip_address = str(ipaddress.ip_address(ip_address)) uri = '/'.join([self._base_uri, path, ip_address]) response = requests.get(uri, auth=(self.user_id, self.license_key), headers={'Accept': 'application/json', 'User-Agent': self._user_agent()}) if response.status_code == 200: # pylint:disable=E1103 body = self._handle_success(response, uri) return model_class(body, locales=self.locales) else: self._handle_error(response, uri)
def log_unique_ip(self, apikey_shortname): try: ip = str(ip_address(self.request.client_addr)) except ValueError: # pragma: no cover ip = None if ip: redis_key = "apiuser:{api_type}:{api_name}:{date}".format( api_type=self.view_type, api_name=apikey_shortname, date=util.utcnow().date().strftime("%Y-%m-%d") ) with self.redis_client.pipeline() as pipe: pipe.pfadd(redis_key, ip) pipe.expire(redis_key, 691200) # 8 days pipe.execute()
def lookup_csv(lookup, field): try: address = ipaddr.ip_address(field) except ValueError: return None for v in lookup: try: network = ipaddr.ip_network(v["c_ip"]) if address in network: return v except ValueError: pass return None
def lookup_csv(lookup, field): try: address = ipaddr.ip_address(field) except ValueError: return None for v in lookup: try: network = ipaddr.ip_network(v['c_ip']) if address in network: return v except ValueError: pass return None
def testIpRange(ipTestedStr, ipRangeStr): ipTested = None if ipTestedStr.find('/') > 0: ipTested = ipaddr.ip_network(ipTestedStr) else: ipTested = ipaddr.ip_address(ipTestedStr) ipRange = ipaddr.ip_network(ipRangeStr) if ipTested in ipRange: NsmUtil.printStatusHeadLine2('Passed: ' + ipTestedStr + ' is in ' + ipRangeStr) return True else: NsmUtil.printStatusHeadLine2('Failed: ' + ipTestedStr + ' is not in ' + ipRangeStr) return False
def log_unique_ip(self, apikey_shortname): try: ip = str(ip_address(self.request.client_addr)) except ValueError: # pragma: no cover ip = None if ip: redis_key = 'apiuser:{api_type}:{api_name}:{date}'.format( api_type=self.view_type, api_name=apikey_shortname, date=util.utcnow().date().strftime('%Y-%m-%d'), ) with self.redis_client.pipeline() as pipe: pipe.pfadd(redis_key, ip) pipe.expire(redis_key, 691200) # 8 days pipe.execute()
def log_unique_ip(self, valid_key): try: ip = str(ip_address(self.request.client_addr)) except ValueError: # pragma: no cover ip = None if ip: redis_key = 'apiuser:{api_type}:{api_key}:{date}'.format( api_type=self.view_type, api_key=valid_key, date=util.utcnow().date().strftime('%Y-%m-%d'), ) with self.redis_client.pipeline() as pipe: pipe.pfadd(redis_key, ip) pipe.expire(redis_key, 691200) # 8 days pipe.execute()
def resolve(self, host): try: ip = ip_address(host) return [(2 if ip._version == 4 else 10, host), ] except: pass try: record = self.record(host, 'ANY') while len(record.rr) == 1 and record.rr[0].rtype == dnslib.QTYPE.CNAME: record = self.record(str(record.rr[0].rdata), 'ANY') return [(2 if x.rtype == 1 else 10, str(x.rdata)) for x in record.rr if x.rtype in (dnslib.QTYPE.A, dnslib.QTYPE.AAAA)] except Exception as e: logging.warning('resolving %s: %r' % (host, e)) traceback.print_exc(file=sys.stderr) return []
def ip(self, value): if not value: value = None try: valid = str(ip_address(value)) except ValueError: valid = None self._ip = valid if valid: region = None geoip = None if self.geoip_db: geoip = self.geoip_db.lookup(valid) if geoip: region = geoip.get('region_code') self._geoip = geoip self._region = region
def setup_ips(ips, label): """Setup missing IP on loopback interface""" existing = set(loopback_ips(label)) toadd = set([ip_address(ip) for net in ips for ip in net]) - existing for ip in toadd: logger.debug("Setup loopback IP address %s", ip) with open(os.devnull, "w") as fnull: cmd = ["ip", "address", "add", str(ip), "dev", "lo"] if label: cmd += ["label", "lo:{0}".format(label)] subprocess.check_call(cmd, stdout=fnull, stderr=fnull) # If we setup IPs we should also remove them on SIGTERM def sigterm_handler(signum, frame): # pylint: disable=W0612,W0613 remove_ips(ips, label) signal.signal(signal.SIGTERM, sigterm_handler)
def setup_ips(ips, label): """Setup missing IP on loopback interface""" existing = set(loopback_ips(label)) toadd = set([ip_address(ip) for net in ips for ip in net]) - existing for ip in toadd: logger.debug("Setup loopback IP address %s", ip) with open(os.devnull, "w") as fnull: cmd = ["ip", "address", "add", str(ip), "dev", "lo"] if label: cmd += ["label", "lo:{0}".format(label)] subprocess.check_call( cmd, stdout=fnull, stderr=fnull) # If we setup IPs we should also remove them on SIGTERM def sigterm_handler(signum, frame): # pylint: disable=W0612,W0613 remove_ips(ips, label) signal.signal(signal.SIGTERM, sigterm_handler)
def remove_ips(ips, label): """Remove added IP on loopback interface""" existing = set(loopback_ips(label)) # Get intersection of IPs (ips setup, and IPs configured by ExaBGP) toremove = set([ip_address(ip) for net in ips for ip in net]) | existing for ip in toremove: logger.debug("Remove loopback IP address %s", ip) with open(os.devnull, "w") as fnull: cmd = ["ip", "address", "delete", str(ip), "dev", "lo"] if label: cmd += ["label", "lo:{0}".format(label)] try: subprocess.check_call( cmd, stdout=fnull, stderr=fnull) except subprocess.CalledProcessError: logger.warn("Unable to remove loopback IP address %s - is \ healthcheck running as root?", str(ip)) sys.exit(0)
def resolve(self, host, dirty=False): try: ip = ip_address(host) return [ (2 if ip._version == 4 else 10, host), ] except Exception: pass if not self.is_poisoned(host): try: result = _resolver(host) if result: return result except Exception as e: logger.info('resolve %s via local failed! %r' % (host, e)) return self.remote.resolve(host) if dirty: return [] return self.remote.resolve(host)
def resolve(self, host, dirty=False): try: ip = ip_address(host) return [(2 if ip._version == 4 else 10, host)] except Exception: pass try: record = self.record(host, "ANY") while len(record.rr) == 1 and record.rr[0].rtype == dnslib.QTYPE.CNAME: record = self.record(str(record.rr[0].rdata), "ANY") return [ (2 if x.rtype == 1 else 10, str(x.rdata)) for x in record.rr if x.rtype in (dnslib.QTYPE.A, dnslib.QTYPE.AAAA) ] except Exception as e: logger.warning("resolving %s failed: %r" % (host, e)) traceback.print_exc(file=sys.stderr) return []
def remove_ips(ips, label): """Remove added IP on loopback interface""" existing = set(loopback_ips(label)) # Get intersection of IPs (ips setup, and IPs configured by ExaBGP) toremove = set([ip_address(ip) for net in ips for ip in net]) | existing for ip in toremove: logger.debug("Remove loopback IP address %s", ip) with open(os.devnull, "w") as fnull: cmd = ["ip", "address", "delete", str(ip), "dev", "lo"] if label: cmd += ["label", "lo:{0}".format(label)] try: subprocess.check_call(cmd, stdout=fnull, stderr=fnull) except subprocess.CalledProcessError: logger.warn( "Unable to remove loopback IP address %s - is \ healthcheck running as root?", str(ip)) sys.exit(0)
def resolve(self, host, dirty=False): try: ip = ip_address(host) return [(2 if ip._version == 4 else 10, host), ] except Exception: pass if not self.is_poisoned(host): return _resolver(host) if dirty: return [] try: record = self.remote.record(host, 'ANY') while len(record.rr) == 1 and record.rr[0].rtype == dnslib.QTYPE.CNAME: record = self.remote.record(str(record.rr[0].rdata), 'ANY') return [(2 if x.rtype == 1 else 10, str(x.rdata)) for x in record.rr if x.rtype in (dnslib.QTYPE.A, dnslib.QTYPE.AAAA)] except Exception as e: logger.warning('resolving %s failed: %r' % (host, e)) traceback.print_exc(file=sys.stderr) return []
def ip(self, value): if not value: value = None try: valid = str(ip_address(value)) except ValueError: valid = None self._ip = valid if valid: country = None geoip = None if self.geoip_db: geoip = self.geoip_db.geoip_lookup(valid) if geoip: country = geoip.get("country_code") if country: country = country.upper() self._geoip = geoip self._country = country
def resolve(self, host, dirty=False): try: ip = ip_address(host) return [ (2 if ip._version == 4 else 10, host), ] except Exception: pass try: record = self.record(host, 'ANY') while len(record.rr ) == 1 and record.rr[0].rtype == dnslib.QTYPE.CNAME: record = self.record(str(record.rr[0].rdata), 'ANY') return [(2 if x.rtype == 1 else 10, str(x.rdata)) for x in record.rr if x.rtype in (dnslib.QTYPE.A, dnslib.QTYPE.AAAA)] except Exception as e: logger.warning('resolving %s failed: %r' % (host, e)) traceback.print_exc(file=sys.stderr) return []
def parse_status(data): client_section = False routes_section = False status_version = 1 sessions = {} client_session = {} gi = GeoIP.open(args.geoip_data, GeoIP.GEOIP_STANDARD) for line in data.splitlines(): if ',' in line: parts = line.split(',') else: parts = line.split('\t') if args.debug: debug("=== begin split line\n{0!s}\n=== end split line".format(parts)) if parts[0].startswith('GLOBAL'): break if parts[0] == 'HEADER': status_version = 3 if parts[1] == 'CLIENT_LIST': client_section = True routes_section = False if parts[1] == 'ROUTING_TABLE': client_section = False routes_section = True continue if parts[0] == 'Updated': continue if parts[0] == 'Common Name': status_version = 1 client_section = True routes_section = False continue if parts[0] == 'ROUTING TABLE' or parts[0] == 'Virtual Address': status_version = 1 client_section = False routes_section = True continue if parts[0].startswith('>CLIENT'): continue session = {} if parts[0] == 'TUN/TAP read bytes': client_session['tuntap_read'] = int(parts[1]) continue if parts[0] == 'TUN/TAP write bytes': client_session['tuntap_write'] = int(parts[1]) continue if parts[0] == 'TCP/UDP read bytes': client_session['tcpudp_read'] = int(parts[1]) continue if parts[0] == 'TCP/UDP write bytes': client_session['tcpudp_write'] = int(parts[1]) continue if parts[0] == 'Auth read bytes': client_session['auth_read'] = int(parts[1]) sessions['Client'] = client_session continue if client_section and not routes_section: if status_version == 1: ident = parts[1] sessions[ident] = session session['username'] = parts[0] remote_ip, port = parts[1].split(':') session['bytes_recv'] = int(parts[2]) session['bytes_sent'] = int(parts[3]) session['connected_since'] = get_date(parts[4]) if status_version == 3: ident = parts[2] sessions[ident] = session session['username'] = parts[1] if parts[2].count(':') == 1: remote_ip, port = parts[2].split(':') else: remote_ip = parts[2] port = None remote_ip_address = ip_address(remote_ip) local_ip = parts[3] if local_ip == '': session['local_ip'] = '' else: session['local_ip'] = ip_address(parts[3]) session['bytes_recv'] = int(parts[4]) session['bytes_sent'] = int(parts[5]) session['connected_since'] = get_date(parts[7], uts=True) session['location'] = 'Unknown' if isinstance(remote_ip_address, IPv6Address) and \ remote_ip_address.ipv4_mapped is not None: session['remote_ip'] = remote_ip_address.ipv4_mapped else: session['remote_ip'] = remote_ip_address if port: session['port'] = int(port) else: session['port'] = '' if session['remote_ip'].is_private: session['location'] = 'RFC1918' else: try: gir = gi.record_by_addr(str(session['remote_ip'])) except SystemError: gir = None if gir is not None: session['location'] = gir['country_code'] session['city'] = get_str(gir['city']) session['country_name'] = gir['country_name'] session['longitude'] = gir['longitude'] session['latitude'] = gir['latitude'] if routes_section and not client_section: if status_version == 1: ident = parts[2] sessions[ident]['local_ip'] = ip_address(parts[0]) sessions[ident]['last_seen'] = get_date(parts[3]) if status_version == 3: ident = parts[3] sessions[ident]['last_seen'] = get_date(parts[5], uts=True) if args.debug: if sessions: pretty_sessions = pformat(sessions) debug("=== begin sessions\n{0!s}\n=== end sessions".format(pretty_sessions)) else: debug("no sessions") return sessions
def parse_status(self, data, version): gi = self.gi geoip_version = self.geoip_version client_section = False routes_section = False sessions = {} client_session = {} for line in data.splitlines(): parts = deque(line.split('\t')) if args.debug: debug("=== begin split line\n{0!s}\n=== end split line".format(parts)) if parts[0].startswith('END'): break if parts[0].startswith('TITLE') or \ parts[0].startswith('GLOBAL') or \ parts[0].startswith('TIME'): continue if parts[0] == 'HEADER': if parts[1] == 'CLIENT_LIST': client_section = True routes_section = False if parts[1] == 'ROUTING_TABLE': client_section = False routes_section = True continue if parts[0].startswith('TUN') or \ parts[0].startswith('TCP') or \ parts[0].startswith('Auth'): parts = parts[0].split(',') if parts[0] == 'TUN/TAP read bytes': client_session['tuntap_read'] = int(parts[1]) continue if parts[0] == 'TUN/TAP write bytes': client_session['tuntap_write'] = int(parts[1]) continue if parts[0] == 'TCP/UDP read bytes': client_session['tcpudp_read'] = int(parts[1]) continue if parts[0] == 'TCP/UDP write bytes': client_session['tcpudp_write'] = int(parts[1]) continue if parts[0] == 'Auth read bytes': client_session['auth_read'] = int(parts[1]) sessions['Client'] = client_session continue if client_section: session = {} parts.popleft() common_name = parts.popleft() remote_str = parts.popleft() if remote_str.count(':') == 1: remote, port = remote_str.split(':') elif '(' in remote_str: remote, port = remote_str.split('(') port = port[:-1] else: remote = remote_str port = None remote_ip = ip_address(remote) if isinstance(remote_ip, IPv6Address) and \ remote_ip.ipv4_mapped is not None: session['remote_ip'] = remote_ip.ipv4_mapped else: session['remote_ip'] = remote_ip if port: session['port'] = int(port) else: session['port'] = '' if session['remote_ip'].is_private: session['location'] = 'RFC1918' else: try: if geoip_version == 1: gir = gi.record_by_addr(str(session['remote_ip'])) session['location'] = gir['country_code'] session['region'] = get_str(gir['region']) session['city'] = get_str(gir['city']) session['country'] = gir['country_name'] session['longitude'] = gir['longitude'] session['latitude'] = gir['latitude'] elif geoip_version == 2: gir = gi.city(str(session['remote_ip'])) session['location'] = gir.country.iso_code session['region'] = gir.subdivisions.most_specific.iso_code session['city'] = gir.city.name session['country'] = gir.country.name session['longitude'] = gir.location.longitude session['latitude'] = gir.location.latitude except AddressNotFoundError: pass except SystemError: pass local_ipv4 = parts.popleft() if local_ipv4: session['local_ip'] = ip_address(local_ipv4) else: session['local_ip'] = '' if version.major == 2 and version.minor >= 4: local_ipv6 = parts.popleft() if local_ipv6: session['local_ip'] = ip_address(local_ipv6) session['bytes_recv'] = int(parts.popleft()) session['bytes_sent'] = int(parts.popleft()) parts.popleft() session['connected_since'] = get_date(parts.popleft(), uts=True) username = parts.popleft() if username != 'UNDEF': session['username'] = username else: session['username'] = common_name if version.major == 2 and version.minor >= 4: session['client_id'] = parts.popleft() session['peer_id'] = parts.popleft() sessions[str(session['local_ip'])] = session if routes_section: local_ip = parts[1] last_seen = parts[5] if local_ip in sessions: sessions[local_ip]['last_seen'] = get_date(last_seen, uts=True) if args.debug: if sessions: pretty_sessions = pformat(sessions) debug("=== begin sessions\n{0!s}\n=== end sessions".format(pretty_sessions)) else: debug("no sessions") return sessions
def parse_status(data, gi, version): client_section = False routes_section = False sessions = {} client_session = {} for line in data.splitlines(): parts = deque(line.split('\t')) if args.debug: debug("=== begin split line\n{0!s}\n=== end split line".format( parts)) if parts[0].startswith('END'): break if parts[0].startswith('TITLE') or \ parts[0].startswith('GLOBAL') or \ parts[0].startswith('TIME'): continue if parts[0] == 'HEADER': if parts[1] == 'CLIENT_LIST': client_section = True routes_section = False if parts[1] == 'ROUTING_TABLE': client_section = False routes_section = True continue if parts[0].startswith('TUN') or \ parts[0].startswith('TCP') or \ parts[0].startswith('Auth'): parts = parts[0].split(',') if parts[0] == 'TUN/TAP read bytes': client_session['tuntap_read'] = int(parts[1]) continue if parts[0] == 'TUN/TAP write bytes': client_session['tuntap_write'] = int(parts[1]) continue if parts[0] == 'TCP/UDP read bytes': client_session['tcpudp_read'] = int(parts[1]) continue if parts[0] == 'TCP/UDP write bytes': client_session['tcpudp_write'] = int(parts[1]) continue if parts[0] == 'Auth read bytes': client_session['auth_read'] = int(parts[1]) sessions['Client'] = client_session continue if client_section: session = {} parts.popleft() common_name = parts.popleft() remote_str = parts.popleft() if remote_str.count(':') == 1: remote, port = remote_str.split(':') elif '(' in remote_str: remote, port = remote_str.split('(') port = port[:-1] else: remote = remote_str port = None remote_ip = ip_address(remote) if isinstance(remote_ip, IPv6Address) and \ remote_ip.ipv4_mapped is not None: session['remote_ip'] = remote_ip.ipv4_mapped else: session['remote_ip'] = remote_ip if port: session['port'] = int(port) else: session['port'] = '' if session['remote_ip'].is_private: session['location'] = 'RFC1918' else: try: gir = gi.record_by_addr(str(session['remote_ip'])) except SystemError: gir = None if gir is not None: session['location'] = gir['country_code'] session['city'] = get_str(gir['city']) session['country_name'] = gir['country_name'] session['longitude'] = gir['longitude'] session['latitude'] = gir['latitude'] local_ipv4 = parts.popleft() if local_ipv4: session['local_ip'] = ip_address(local_ipv4) else: session['local_ip'] = '' if version.minor == 4: local_ipv6 = parts.popleft() if local_ipv6: session['local_ip'] = ip_address(local_ipv6) session['bytes_recv'] = int(parts.popleft()) session['bytes_sent'] = int(parts.popleft()) parts.popleft() session['connected_since'] = get_date(parts.popleft(), uts=True) username = parts.popleft() if username != 'UNDEF': session['username'] = username else: session['username'] = common_name if version.minor == 4: session['client_id'] = parts.popleft() session['peer_id'] = parts.popleft() sessions[str(session['local_ip'])] = session if routes_section: local_ip = parts[1] last_seen = parts[5] if local_ip in sessions: sessions[local_ip]['last_seen'] = get_date(last_seen, uts=True) if args.debug: if sessions: pretty_sessions = pformat(sessions) debug("=== begin sessions\n{0!s}\n=== end sessions".format( pretty_sessions)) else: debug("no sessions") return sessions
#!/usr/bin/python3 # -*- coding: utf-8 -*- from ipaddr import ip_address from ipaddr import ip_network base = lambda net, i=0: ip_address( ip_network(net)[0] + i*ip_network(net).numhosts ) bits = lambda ip: 32 if ip.version == 4 else 128 prefix = lambda net: int( str(net).split("/")[1] ) network = lambda ip,p: ip_network("{}/{}".format(str(ip),p)) parent = lambda net: merge(sibling(net)) def net_bits(ip): n = int(ip) if n == 0: return bits(ip) for p in range(bits(ip),0,-1): if n & 1 == 1 : return p n = n >> 1 # ein Netz kann entweder mit seinem (gleichgrossen) Vorgänger oder Nachfolger (sibling) zu einem doppelt so grossen # (prefix_neu = prefix-1) Netz "gemerged" werden # mit dem Vorgänger wird gemerged wenn dieser weniger net_bits hat als das aktuelle Netz # mit dem Nachfolger wird gemerged wenn das aktuelle Netz weniger net_bits hat als dieser def sibling(net): p = prefix(net) ip_list = [ base(net,-1), base(net), base(net,+1) ] prefix_list = [ net_bits(ip) for ip in ip_list ] partner_list = [ ip_network("{}/{}".format(str(ip),p)) for ip in ip_list ] if prefix_list[0] < prefix_list[1]: return partner_list[:-1] if prefix_list[1] < prefix_list[2]: return partner_list[1:]
def parse_status(data): client_section = False routes_section = False status_version = 1 sessions = {} client_session = {} gi = GeoIP.open(args.geoip_data, GeoIP.GEOIP_STANDARD) for line in data.splitlines(): if ',' in line: parts = line.split(',') else: parts = line.split('\t') if args.debug: debug("=== begin split line\n{0!s}\n=== end split line".format( parts)) if parts[0].startswith('GLOBAL'): break if parts[0] == 'HEADER': status_version = 3 if parts[1] == 'CLIENT_LIST': client_section = True routes_section = False if parts[1] == 'ROUTING_TABLE': client_section = False routes_section = True continue if parts[0] == 'Updated': continue if parts[0] == 'Common Name': status_version = 1 client_section = True routes_section = False continue if parts[0] == 'ROUTING TABLE' or parts[0] == 'Virtual Address': status_version = 1 client_section = False routes_section = True continue if parts[0].startswith('>CLIENT'): continue session = {} if parts[0] == 'TUN/TAP read bytes': client_session['tuntap_read'] = int(parts[1]) continue if parts[0] == 'TUN/TAP write bytes': client_session['tuntap_write'] = int(parts[1]) continue if parts[0] == 'TCP/UDP read bytes': client_session['tcpudp_read'] = int(parts[1]) continue if parts[0] == 'TCP/UDP write bytes': client_session['tcpudp_write'] = int(parts[1]) continue if parts[0] == 'Auth read bytes': client_session['auth_read'] = int(parts[1]) sessions['Client'] = client_session continue if client_section and not routes_section: if status_version == 1: ident = parts[1] sessions[ident] = session session['username'] = parts[0] remote_ip, port = parts[1].split(':') session['bytes_recv'] = int(parts[2]) session['bytes_sent'] = int(parts[3]) session['connected_since'] = get_date(parts[4]) if status_version == 3: ident = parts[2] sessions[ident] = session session['username'] = parts[1] if parts[2].count(':') == 1: remote_ip, port = parts[2].split(':') else: remote_ip = parts[2] port = None remote_ip_address = ip_address(remote_ip) local_ip = parts[3] if local_ip == '': session['local_ip'] = '' else: session['local_ip'] = ip_address(parts[3]) session['bytes_recv'] = int(parts[4]) session['bytes_sent'] = int(parts[5]) session['connected_since'] = get_date(parts[7], uts=True) session['location'] = 'Unknown' if isinstance(remote_ip_address, IPv6Address) and \ remote_ip_address.ipv4_mapped is not None: session['remote_ip'] = remote_ip_address.ipv4_mapped else: session['remote_ip'] = remote_ip_address if port: session['port'] = int(port) else: session['port'] = '' if session['remote_ip'].is_private: session['location'] = 'RFC1918' else: try: gir = gi.record_by_addr(str(session['remote_ip'])) except SystemError: gir = None if gir is not None: session['location'] = gir['country_code'] session['city'] = get_str(gir['city']) session['country_name'] = gir['country_name'] session['longitude'] = gir['longitude'] session['latitude'] = gir['latitude'] if routes_section and not client_section: if status_version == 1: ident = parts[2] sessions[ident]['local_ip'] = ip_address(parts[0]) sessions[ident]['last_seen'] = get_date(parts[3]) if status_version == 3: ident = parts[3] sessions[ident]['last_seen'] = get_date(parts[5], uts=True) if args.debug: if sessions: pretty_sessions = pformat(sessions) debug("=== begin sessions\n{0!s}\n=== end sessions".format( pretty_sessions)) else: debug("no sessions") return sessions
def parse_status(self, data, version): client_section = False routes_section = False sessions = {} client_session = {} for line in data.splitlines(): parts = deque(line.split('\t')) if 1: debug("=== begin split line\n{0!s}\n=== end split line".format( parts)) if parts[0].startswith('END'): break if parts[0].startswith('TITLE') or \ parts[0].startswith('GLOBAL') or \ parts[0].startswith('TIME'): continue if parts[0] == 'HEADER': if parts[1] == 'CLIENT_LIST': client_section = True routes_section = False if parts[1] == 'ROUTING_TABLE': client_section = False routes_section = True continue if parts[0].startswith('TUN') or \ parts[0].startswith('TCP') or \ parts[0].startswith('Auth'): parts = parts[0].split(',') if parts[0] == 'TUN/TAP read bytes': client_session['tuntap_read'] = int(parts[1]) continue if parts[0] == 'TUN/TAP write bytes': client_session['tuntap_write'] = int(parts[1]) continue if parts[0] == 'TCP/UDP read bytes': client_session['tcpudp_read'] = int(parts[1]) continue if parts[0] == 'TCP/UDP write bytes': client_session['tcpudp_write'] = int(parts[1]) continue if parts[0] == 'Auth read bytes': client_session['auth_read'] = int(parts[1]) sessions['Client'] = client_session continue if client_section: session = {} parts.popleft() common_name = parts.popleft() remote_str = parts.popleft() if remote_str.count(':') == 1: remote, port = remote_str.split(':') elif '(' in remote_str: remote, port = remote_str.split('(') port = port[:-1] else: remote = remote_str port = None remote_ip = ip_address(remote) session['remote_ip'] = remote_ip if port: session['port'] = int(port) else: session['port'] = '' local_ipv4 = parts.popleft() if local_ipv4: session['local_ip'] = ip_address(local_ipv4) else: session['local_ip'] = '' if version.major >= 2 and version.minor >= 4: local_ipv6 = parts.popleft() if local_ipv6: session['local_ip'] = ip_address(local_ipv6) session['bytes_recv'] = int(parts.popleft()) session['bytes_sent'] = int(parts.popleft()) parts.popleft() session['connected_since'] = get_date(parts.popleft(), uts=True) username = parts.popleft() if username != 'UNDEF': session['username'] = username else: session['username'] = common_name if version.major == 2 and version.minor >= 4: session['client_id'] = parts.popleft() session['peer_id'] = parts.popleft() sessions[str(session['local_ip'])] = session if routes_section: local_ip = parts[1] remote_ip = parts[3] last_seen = get_date(parts[5], uts=True) if sessions.get(local_ip): sessions[local_ip]['last_seen'] = last_seen elif self.is_mac_address(local_ip): matching_local_ips = [ sessions[s]['local_ip'] for s in sessions if remote_ip == self.get_remote_address( sessions[s]['remote_ip'], sessions[s]['port']) ] if len(matching_local_ips) == 1: local_ip = '{0!s}'.format(matching_local_ips[0]) if sessions[local_ip].get('last_seen'): prev_last_seen = sessions[local_ip]['last_seen'] if prev_last_seen < last_seen: sessions[local_ip]['last_seen'] = last_seen else: sessions[local_ip]['last_seen'] = last_seen if 1: if sessions: pretty_sessions = pformat(sessions) debug("=== begin sessions\n{0!s}\n=== end sessions".format( pretty_sessions)) else: debug("no sessions") return sessions
def parse_status(self, data, version): gi = self.gi geoip_version = self.geoip_version client_section = False routes_section = False sessions = {} client_session = {} for line in data.splitlines(): parts = deque(line.split('\t')) if args.debug: debug("=== begin split line\n{0!s}\n=== end split line".format(parts)) if parts[0].startswith('END'): break if parts[0].startswith('TITLE') or \ parts[0].startswith('GLOBAL') or \ parts[0].startswith('TIME'): continue if parts[0] == 'HEADER': if parts[1] == 'CLIENT_LIST': client_section = True routes_section = False if parts[1] == 'ROUTING_TABLE': client_section = False routes_section = True continue if parts[0].startswith('TUN') or \ parts[0].startswith('TCP') or \ parts[0].startswith('Auth'): parts = parts[0].split(',') if parts[0] == 'TUN/TAP read bytes': client_session['tuntap_read'] = int(parts[1]) continue if parts[0] == 'TUN/TAP write bytes': client_session['tuntap_write'] = int(parts[1]) continue if parts[0] == 'TCP/UDP read bytes': client_session['tcpudp_read'] = int(parts[1]) continue if parts[0] == 'TCP/UDP write bytes': client_session['tcpudp_write'] = int(parts[1]) continue if parts[0] == 'Auth read bytes': client_session['auth_read'] = int(parts[1]) sessions['Client'] = client_session continue if client_section: session = {} parts.popleft() common_name = parts.popleft() remote_str = parts.popleft() if remote_str.count(':') == 1: remote, port = remote_str.split(':') elif '(' in remote_str: remote, port = remote_str.split('(') port = port[:-1] else: remote = remote_str port = None remote_ip = ip_address(remote) session['remote_ip'] = remote_ip if port: session['port'] = int(port) else: session['port'] = '' if session['remote_ip'].is_private: session['location'] = 'RFC1918' elif session['remote_ip'].is_loopback: session['location'] = 'loopback' else: try: if geoip_version == 1: gir = gi.record_by_addr(str(session['remote_ip'])) if gir is not None: session['location'] = gir['country_code'] session['region'] = get_str(gir['region']) session['city'] = get_str(gir['city']) session['country'] = gir['country_name'] session['longitude'] = gir['longitude'] session['latitude'] = gir['latitude'] elif geoip_version == 2: gir = gi.city(str(session['remote_ip'])) session['location'] = gir.country.iso_code session['region'] = gir.subdivisions.most_specific.iso_code session['city'] = gir.city.name session['country'] = gir.country.name session['longitude'] = gir.location.longitude session['latitude'] = gir.location.latitude except AddressNotFoundError: pass except SystemError: pass local_ipv4 = parts.popleft() if local_ipv4: session['local_ip'] = ip_address(local_ipv4) else: session['local_ip'] = '' if version.major >= 2 and version.minor >= 4: local_ipv6 = parts.popleft() if local_ipv6: session['local_ip'] = ip_address(local_ipv6) session['bytes_recv'] = int(parts.popleft()) session['bytes_sent'] = int(parts.popleft()) parts.popleft() session['connected_since'] = get_date(parts.popleft(), uts=True) username = parts.popleft() if username != 'UNDEF': session['username'] = username else: session['username'] = common_name if version.major == 2 and version.minor >= 4: session['client_id'] = parts.popleft() session['peer_id'] = parts.popleft() sessions[str(session['local_ip'])] = session if routes_section: local_ip = parts[1] remote_ip = parts[3] last_seen = get_date(parts[5], uts=True) if local_ip in sessions: sessions[local_ip]['last_seen'] = last_seen elif self.is_mac_address(local_ip): matching_local_ips = [sessions[s]['local_ip'] for s in sessions if remote_ip == self.get_remote_address(sessions[s]['remote_ip'], sessions[s]['port'])] if len(matching_local_ips) == 1: local_ip = '{0!s}'.format(matching_local_ips[0]) if 'last_seen' in sessions[local_ip]: prev_last_seen = sessions[local_ip]['last_seen'] if prev_last_seen < last_seen: sessions[local_ip]['last_seen'] = last_seen else: sessions[local_ip]['last_seen'] = last_seen if args.debug: if sessions: pretty_sessions = pformat(sessions) debug("=== begin sessions\n{0!s}\n=== end sessions".format(pretty_sessions)) else: debug("no sessions") return sessions