Exemple #1
0
def bgp_raw_to_csv(fname, out):
    out = open(os.path.join(config.GEOIP_PATH, out), 'w')
    cur = None
    with open(os.path.join(config.GEOIP_PATH, fname), 'rb') as fdesc:
        for line in fdesc:
            start, stop = (utils.ip2int(elt) for elt in
                           utils.net2range(line[:-1].split(None, 1)[0]))
            if cur:
                if start >= cur[0] and stop <= cur[1]:
                    continue
                if start >= cur[0] and start <= cur[1]:
                    cur = (cur[0], stop)
                    continue
                if stop >= cur[0] and stop <= cur[1]:
                    cur = (start, cur[1])
                    continue
                if start <= cur[0] and stop >= cur[1]:
                    cur = (start, stop)
                    continue
                if start == cur[1] + 1:
                    cur = (cur[0], stop)
                    continue
                if stop == cur[0] + 1:
                    cur = (start, cur[1])
                    continue
                out.write('%d,%d\n' % cur)
            cur = (start, stop)
    if cur:
        out.write('%d,%d\n' % cur)
Exemple #2
0
def bgp_raw_to_csv(fname: str, outname: str) -> None:
    cur = None
    assert config.GEOIP_PATH is not None
    with open(os.path.join(config.GEOIP_PATH, fname),
              "rb") as fdesc, open(os.path.join(config.GEOIP_PATH, outname),
                                   "w",
                                   encoding="utf8") as out:
        for line in fdesc:
            start, stop = (
                utils.ip2int(elt)
                for elt in utils.net2range(line[:-1].split(None, 1)[0]))
            if cur:
                if start >= cur[0] and stop <= cur[1]:
                    continue
                if cur[0] <= start <= cur[1]:
                    cur = (cur[0], stop)
                    continue
                if cur[0] <= stop <= cur[1]:
                    cur = (start, cur[1])
                    continue
                if start <= cur[0] and stop >= cur[1]:
                    cur = (start, stop)
                    continue
                if start == cur[1] + 1:
                    cur = (cur[0], stop)
                    continue
                if stop == cur[0] + 1:
                    cur = (start, cur[1])
                    continue
                out.write("%d,%d\n" % cur)
            cur = (start, stop)
        if cur:
            out.write("%d,%d\n" % cur)
Exemple #3
0
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)
Exemple #4
0
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)
Exemple #5
0
def bgp_raw_to_csv(fname, out):
    out = open(os.path.join(config.GEOIP_PATH, out), 'wb')
    cur = []
    with open(os.path.join(config.GEOIP_PATH, fname), 'rb') as fdesc:
        for line in fdesc:
            start, stop = (utils.ip2int(elt) for elt in
                           utils.net2range(line[:-1].split()[0]))
            if cur:
                if start >= cur[0] and stop <= cur[1]:
                    continue
                if start >= cur[0] and start <= cur[1]:
                    cur = [cur[0], stop]
                    continue
                if stop >= cur[0] and stop <= cur[1]:
                    cur = [start, cur[1]]
                    continue
                if start <= cur[0] and stop >= cur[1]:
                    cur = [start, stop]
                    continue
                if start == cur[1] + 1:
                    cur = [cur[0], stop]
                    continue
                if stop == cur[0] + 1:
                    cur = [start, cur[1]]
                    continue
                out.write(('"%s","%s","%d","%d"\n' % (
                    utils.int2ip(cur[0]), utils.int2ip(cur[1]), cur[0], cur[1],
                )).encode())
            cur = [start, stop]
    if cur:
        out.write(('"%s","%s","%d","%d"\n' % (
            utils.int2ip(cur[0]), utils.int2ip(cur[1]), cur[0], cur[1],
        )).encode())
Exemple #6
0
def bgp_raw_to_csv(fname, out):
    out = open(os.path.join(config.GEOIP_PATH, out), 'w')
    cur = []
    with open(os.path.join(config.GEOIP_PATH, fname)) as fdesc:
        for line in fdesc:
            start, stop = map(utils.ip2int,
                              utils.net2range(line[:-1].split()[0]))
            if cur:
                if start >= cur[0] and stop <= cur[1]:
                    continue
                if start >= cur[0] and start <= cur[1]:
                    cur = [cur[0], stop]
                    continue
                if stop >= cur[0] and stop <= cur[1]:
                    cur = [start, cur[1]]
                    continue
                if start <= cur[0] and stop >= cur[1]:
                    cur = [start, stop]
                    continue
                if start == cur[1] + 1:
                    cur = [cur[0], stop]
                    continue
                if stop == cur[0] + 1:
                    cur = [start, cur[1]]
                    continue
                out.write('"%s","%s","%d","%d"\n' % (utils.int2ip(
                    cur[0]), utils.int2ip(cur[1]), cur[0], cur[1]))
            cur = [start, stop]
    if cur:
        out.write('"%s","%s","%d","%d"\n' %
                  (utils.int2ip(cur[0]), utils.int2ip(cur[1]), cur[0], cur[1]))
Exemple #7
0
 def __init__(self, net, categories=None, rand=True, maxnbr=None, state=None):
     super().__init__(
         *utils.net2range(net),
         rand=rand,
         maxnbr=maxnbr,
         state=state,
         name="NET-%s" % net.replace("/", "-"),
         categories=categories,
     )
Exemple #8
0
def cdnjson2table(infd: BinaryIO, outfd: BinaryIO) -> None:
    table = make_range_tables([
        net2range(net) + (cast(str, name), )
        for name, nets in json.load(infd).items() for net in nets
    ])
    outfd.write(b"[\n    (\n")
    outfd.writelines(f"        {elt[0]!r},\n".encode() for elt in table)
    outfd.write(b"    ),\n    (\n")
    outfd.writelines(f"        {elt[1]!r},\n".encode() for elt in table)
    outfd.write(b"    ),\n]\n")
Exemple #9
0
 def __init__(self,
              net,
              categories=None,
              rand=True,
              maxnbr=None,
              state=None):
     super(TargetNetwork,
           self).__init__(*utils.net2range(net),
                          rand=rand,
                          maxnbr=maxnbr,
                          state=state,
                          name='NET-%s' % net.replace('/', '-'),
                          categories=categories)
Exemple #10
0
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)
Exemple #11
0
    def searchnet(self, net, neg=False):
        """Filters (if `neg` == True, filters out) one particular IP
        network (CIDR notation).

        """
        return self.searchrange(*utils.net2range(net), neg=neg)
Exemple #12
0
    def searchnet(self, net, neg=False):
        """Filters (if `neg` == True, filters out) one particular IP
        network (CIDR notation).

        """
        return self.searchrange(*utils.net2range(net), neg=neg)
Exemple #13
0
 def __init__(self, net, **kargs):
     if 'categories' not in kargs or kargs['categories'] is None:
         kargs['categories'] = ['NET-' + net.replace('/', '_')]
     TargetRange.__init__(self, *utils.net2range(net), **kargs)
Exemple #14
0
 def __init__(self, net, **kargs):
     if 'categories' not in kargs or kargs['categories'] is None:
         kargs['categories'] = ['NET-' + net.replace('/', '_')]
     TargetRange.__init__(self, *utils.net2range(net), **kargs)