def main(): parser = argparse.ArgumentParser( description='Tool for ip addresses manipulation.', ) parser.add_argument( 'ips', nargs='*', help='Display results for specified IP addresses or ranges.', ) args = parser.parse_args() while '-' in args.ips: idx = args.ips.index('-') args.ips = ( args.ips[:idx - 1] + ['%s-%s' % (args.ips[idx - 1], args.ips[idx + 1])] + args.ips[idx + 2:] ) for a in args.ips: if '/' in a: a = utils.net2range(a) print("%s-%s" % (a[0], a[1])) elif '-' in a: a = a.split('-', 1) if a[0].isdigit(): a[0] = int(a[0]) if a[1].isdigit(): a[1] = int(a[1]) for n in utils.range2nets((a[0], a[1])): print(n) else: if a.isdigit(): a = utils.force_int2ip(int(a)) else: a = utils.force_ip2int(a) print(a)
def list_ips_by_data(datafile, condition, listall=True, listcidrs=False, skip=0, maxnbr=None, multiple=False): if ((not listall) or listcidrs) and ((skip != 0) or (maxnbr is not None)): utils.LOGGER.warning('Skip and maxnbr parameters have no effect ' 'when listall == False or listcidrs == True.') if listcidrs: listall = False for start, stop in _get_by_data(datafile, condition): if listall: curaddrs = [utils.int2ip(addr) for addr in range(start, stop + 1)] if skip > 0: skip -= len(curaddrs) if skip <= 0: curaddrs = curaddrs[skip:] else: curaddrs = [] if maxnbr is not None: maxnbr -= len(curaddrs) if maxnbr < 0: curaddrs = curaddrs[:maxnbr] for addr in curaddrs: print(addr) if maxnbr is not None and maxnbr <= 0: return elif listcidrs: for net in utils.range2nets((start, stop)): print(net) else: print("%s - %s" % (utils.int2ip(start), utils.int2ip(stop)))
def list_ips_by_data(datafile, parseline, data, listall=True, listcidrs=False, skip=0, maxnbr=None, multiple=False): if ((not listall) or listcidrs) and ((skip != 0) or (maxnbr is not None)): utils.LOGGING.warning('Skip and maxnbr parameters have no effect ' 'when listall == False or listcidrs == True.') if listcidrs: listall = False with open(datafile) as fdesc: for line in fdesc: start, stop, curdata = parseline(line) if (multiple and curdata in data) or curdata == data: if listall: curaddrs = map(utils.int2ip, xrange(start, stop + 1)) if skip > 0: skip -= len(curaddrs) if skip <= 0: curaddrs = curaddrs[skip:] else: curaddrs = [] if maxnbr is not None: maxnbr -= len(curaddrs) if maxnbr < 0: curaddrs = curaddrs[:maxnbr] for addr in curaddrs: print addr if maxnbr is not None and maxnbr <= 0: return elif listcidrs: for net in utils.range2nets((start, stop)): print net else: print "%s - %s" % (utils.int2ip(start), utils.int2ip(stop))
def list_ips_by_data(datafile, parseline, data, listall=True, listcidrs=False, skip=0, maxnbr=None, multiple=False): if ((not listall) or listcidrs) and ((skip != 0) or (maxnbr is not None)): sys.stderr.write('WARNING: skip and maxnbr parameters have no effect ' 'when listall == False or listcidrs == True.\n') if listcidrs: listall = False with open(datafile) as fdesc: for line in fdesc: start, stop, curdata = parseline(line) if (multiple and curdata in data) or curdata == data: if listall: curaddrs = map(utils.int2ip, xrange(start, stop + 1)) if skip > 0: skip -= len(curaddrs) if skip <= 0: curaddrs = curaddrs[skip:] else: curaddrs = [] if maxnbr is not None: maxnbr -= len(curaddrs) if maxnbr < 0: curaddrs = curaddrs[:maxnbr] for addr in curaddrs: print addr if maxnbr is not None and maxnbr <= 0: return elif listcidrs: for net in utils.range2nets((start, stop)): print net else: print "%s - %s" % (utils.int2ip(start), utils.int2ip(stop))
def main() -> None: parser = argparse.ArgumentParser( description="Tool for ip addresses manipulation.", ) parser.add_argument( "ips", nargs="*", help="Display results for specified IP addresses or ranges.", ) args = parser.parse_args() while "-" in args.ips: idx = args.ips.index("-") args.ips = (args.ips[:idx - 1] + ["%s-%s" % (args.ips[idx - 1], args.ips[idx + 1])] + args.ips[idx + 2:]) for a in args.ips: if "/" in a: a = utils.net2range(a) print("%s-%s" % (a[0], a[1])) elif "-" in a: a = a.split("-", 1) if a[0].isdigit(): a[0] = int(a[0]) if a[1].isdigit(): a[1] = int(a[1]) for n in utils.range2nets((a[0], a[1])): print(n) else: if a.isdigit(): a = utils.force_int2ip(int(a)) else: a = utils.force_ip2int(a) print(a)
def list_ips_by_data(datafile, parseline, data, listall=True, listcidrs=False, skip=0, maxnbr=None, multiple=False): if ((not listall) or listcidrs) and ((skip != 0) or (maxnbr is not None)): utils.LOGGER.warning('Skip and maxnbr parameters have no effect ' 'when listall == False or listcidrs == True.') if listcidrs: listall = False with open(datafile, 'rb') as fdesc: for line in fdesc: start, stop, curdata = parseline(line) if (multiple and curdata in data) or curdata == data: if listall: curaddrs = [utils.int2ip(addr) for addr in range(start, stop + 1)] if skip > 0: skip -= len(curaddrs) if skip <= 0: curaddrs = curaddrs[skip:] else: curaddrs = [] if maxnbr is not None: maxnbr -= len(curaddrs) if maxnbr < 0: curaddrs = curaddrs[:maxnbr] for addr in curaddrs: print(addr) if maxnbr is not None and maxnbr <= 0: return elif listcidrs: for net in utils.range2nets((start, stop)): print(net) else: print("%s - %s" % (utils.int2ip(start), utils.int2ip(stop)))
def __init__(self, target, zmap='zmap', port=80, zmap_opts=None): self.srctarget = target self.infos = target.infos if zmap_opts is None: zmap_opts = [] zmap_opts += ['-p', str(port)] self.infos['zmap_pre_scan'] = zmap_opts[:] zmap_opts = [zmap] + zmap_opts + ['-o', '-'] self.tmpfile = tempfile.NamedTemporaryFile(delete=False) for start, count in viewvalues(target.targets.ranges): for net in utils.range2nets((start, start + count - 1)): self.tmpfile.write("%s\n" % net) self.tmpfile.close() zmap_opts += ['-w', self.tmpfile.name] self.proc = subprocess.Popen(zmap_opts, stdout=subprocess.PIPE) self.targetsfd = self.proc.stdout
def __init__(self, target, zmap='zmap', port=80, zmap_opts=None): self.srctarget = target self.infos = target.infos if zmap_opts is None: zmap_opts = [] zmap_opts += ['-p', str(port)] self.infos['zmap_pre_scan'] = zmap_opts[:] zmap_opts = [zmap] + zmap_opts + ['-o', '-'] self.tmpfile = tempfile.NamedTemporaryFile(delete=False) for start, count in target.targets.ranges.itervalues(): for net in utils.range2nets((start, start + count - 1)): self.tmpfile.write("%s\n" % net) self.tmpfile.close() zmap_opts += ['-w', self.tmpfile.name] self.proc = subprocess.Popen(zmap_opts, stdout=subprocess.PIPE) self.targetsfd = self.proc.stdout
def __init__(self, target, zmap="zmap", port=80, zmap_opts=None): self.srctarget = target self.infos = target.infos if zmap_opts is None: zmap_opts = [] zmap_opts += ["-p", str(port)] self.infos["zmap_pre_scan"] = zmap_opts[:] zmap_opts = [zmap] + zmap_opts + ["-o", "-"] self.tmpfile = tempfile.NamedTemporaryFile(delete=False, mode="w") for start, count in target.targets.ranges.values(): for net in utils.range2nets((start, start + count - 1)): self.tmpfile.write("%s\n" % net) self.tmpfile.close() zmap_opts += ["-w", self.tmpfile.name] self.proc = subprocess.Popen(zmap_opts, stdout=subprocess.PIPE) self.targetsfd = self.proc.stdout
def main(): if USING_ARGPARSE: parser = argparse.ArgumentParser( description='Tool for ip addresses manipulation.', ) else: parser = optparse.OptionParser( description='Tool for ip addresses manipulation.', ) parser.parse_args_orig = parser.parse_args def my_parse_args(): res = parser.parse_args_orig() res[0].ensure_value('ips', res[1]) return res[0] parser.parse_args = my_parse_args parser.add_argument = parser.add_option if USING_ARGPARSE: parser.add_argument('ips', nargs='*', help='Display results for specified IP addresses' ' or ranges.') args = parser.parse_args() for a in args.ips: if '/' in a: a = utils.net2range(a) print("%s-%s" % (a[0], a[1])) elif '-' in a: a = a.split('-', 1) if a[0].isdigit(): a[0] = int(a[0]) if a[1].isdigit(): a[1] = int(a[1]) for n in utils.range2nets((a[0], a[1])): print(n) else: if a.isdigit(): a = utils.force_int2ip(int(a)) else: a = utils.force_ip2int(a) print(a)
def __init__(self, target, nmap='nmap', ports=None, nmap_opts=None): self.srctarget = target self.infos = target.infos if ports is None: ports = [80, 443] ports = ','.join(str(p) for p in ports) if nmap_opts is None: nmap_opts = [] nmap_opts += ['-n', '-PS%s' % ports, '-sS', '--open', '-p', ports] self.infos['nmap_pre_scan'] = nmap_opts[:] # TODO: use -iL and feed target randomly when needed, w/o # using a temporary file self.tmpfile = tempfile.NamedTemporaryFile(delete=False) nmap_opts = [nmap, '-iL', self.tmpfile.name, '-oG', '-'] + nmap_opts for start, count in viewvalues(target.targets.ranges): for net in utils.range2nets((start, start + count - 1)): self.tmpfile.write("%s\n" % net) self.tmpfile.close() self.proc = subprocess.Popen(nmap_opts, stdout=subprocess.PIPE) self.targetsfd = self.proc.stdout
def __init__(self, target, nmap="nmap", ports=None, nmap_opts=None): self.srctarget = target self.infos = target.infos if ports is None: ports = [80, 443] ports = ",".join(str(p) for p in ports) if nmap_opts is None: nmap_opts = [] nmap_opts += ["-n", "-PS%s" % ports, "-sS", "--open", "-p", ports] self.infos["nmap_pre_scan"] = nmap_opts[:] # TODO: use -iL and feed target randomly when needed, w/o # using a temporary file self.tmpfile = tempfile.NamedTemporaryFile(delete=False, mode="w") nmap_opts = [nmap, "-iL", self.tmpfile.name, "-oG", "-"] + nmap_opts for start, count in target.targets.ranges.values(): for net in utils.range2nets((start, start + count - 1)): self.tmpfile.write("%s\n" % net) self.tmpfile.close() self.proc = subprocess.Popen(nmap_opts, stdout=subprocess.PIPE) self.targetsfd = self.proc.stdout
def iter_nets(self) -> Generator[str, None, None]: for start, length in sorted(self.ranges.values()): for net in utils.range2nets( (utils.int2ip(start), utils.int2ip(start + length - 1))): yield net
def iter_nets(self): for start, length in sorted(viewvalues(self.ranges)): for net in utils.range2nets( (utils.int2ip(start), utils.int2ip(start + length - 1))): yield net