def load_services(filename): spaces = re.compile(b"[ \t]+|\n") tdct = DADict(_name="%s-tcp" % filename) udct = DADict(_name="%s-udp" % filename) try: with open(filename, "rb") as fdesc: for line in fdesc: try: shrp = line.find(b"#") if shrp >= 0: line = line[:shrp] line = line.strip() if not line: continue lt = tuple(re.split(spaces, line)) if len(lt) < 2 or not lt[0]: continue if lt[1].endswith(b"/tcp"): tdct[lt[0]] = int(lt[1].split(b'/')[0]) elif lt[1].endswith(b"/udp"): udct[lt[0]] = int(lt[1].split(b'/')[0]) except Exception as e: log_loading.warning( "Couldn't parse file [%s]: line [%r] (%s)", filename, line, e, ) except IOError: log_loading.info("Can't open /etc/services file") return tdct, udct
def load_services(filename): spaces = re.compile("[ \t]+|\n") tdct=DADict(_name="%s-tcp"%filename) udct=DADict(_name="%s-udp"%filename) try: f=open(filename) for l in f: try: shrp = l.find("#") if shrp >= 0: l = l[:shrp] l = l.strip() if not l: continue lt = tuple(re.split(spaces, l)) if len(lt) < 2 or not lt[0]: continue if lt[1].endswith("/tcp"): tdct[lt[0]] = int(lt[1].split('/')[0]) elif lt[1].endswith("/udp"): udct[lt[0]] = int(lt[1].split('/')[0]) except Exception as e: log_loading.warning("Couldn't file [%s]: line [%r] (%s)" % (filename,l,e)) f.close() except IOError: log_loading.info("Can't open /etc/services file") return tdct,udct
def load_protocols(filename, _integer_base=10): """"Parse /etc/protocols and return values as a dictionary.""" spaces = re.compile(b"[ \t]+|\n") dct = DADict(_name=filename) try: with open(filename, "rb") as fdesc: for line in fdesc: try: shrp = line.find(b"#") if shrp >= 0: line = line[:shrp] line = line.strip() if not line: continue lt = tuple(re.split(spaces, line)) if len(lt) < 2 or not lt[0]: continue dct[lt[0]] = int(lt[1], _integer_base) except Exception as e: log_loading.info( "Couldn't parse file [%s]: line [%r] (%s)", filename, line, e, ) except IOError: log_loading.info("Can't open %s file", filename) return dct
def load_services(filename): # type: (str) -> Tuple[DADict[int, str], DADict[int, str]] spaces = re.compile(b"[ \t]+|\n") tdct = DADict(_name="%s-tcp" % filename) # type: DADict[int, str] udct = DADict(_name="%s-udp" % filename) # type: DADict[int, str] try: with open(filename, "rb") as fdesc: for line in fdesc: try: shrp = line.find(b"#") if shrp >= 0: line = line[:shrp] line = line.strip() if not line: continue lt = tuple(re.split(spaces, line)) if len(lt) < 2 or not lt[0]: continue dtct = None if lt[1].endswith(b"/tcp"): dtct = tdct elif lt[1].endswith(b"/udp"): dtct = udct else: continue port = lt[1].split(b'/')[0] name = fixname(lt[0]) if b"-" in port: sport, eport = port.split(b"-") for i in range(int(sport), int(eport) + 1): dtct[i] = name else: dtct[int(port)] = name except Exception as e: log_loading.warning( "Couldn't parse file [%s]: line [%r] (%s)", filename, line, e, ) except IOError: log_loading.info("Can't open /etc/services file") return tdct, udct
def load_ethertypes(filename): spaces = re.compile("[ \t]+|\n") dct = DADict(_name=filename) try: f=open(filename) for l in f: try: shrp = l.find("#") if shrp >= 0: l = l[:shrp] l = l.strip() if not l: continue lt = tuple(re.split(spaces, l)) if len(lt) < 2 or not lt[0]: continue dct[lt[0]] = int(lt[1], 16) except Exception,e: log_loading.info("Couldn't parse file [%s]: line [%r] (%s)" % (filename,l,e)) f.close()
def load_protocols(filename): spaces = re.compile("[ \t]+|\n") dct = DADict(_name=filename) try: for l in open(filename): try: shrp = l.find("#") if shrp >= 0: l = l[:shrp] l = l.strip() if not l: continue lt = tuple(re.split(spaces, l)) if len(lt) < 2 or not lt[0]: continue dct[lt[0]] = int(lt[1]) except Exception as e: log_loading.info("Couldn't parse file [%s]: line [%r] (%s)" % (filename,l,e)) except IOError: log_loading.info("Can't open %s file" % filename) return dct