コード例 #1
0
ファイル: iprange.py プロジェクト: hornc/openlibrary-1
    def add_ip_range(self, ip_range, value):
        """Adds an entry to this map.

        ip_range can be in the following forms:

            "1.2.3.4"
            "1.2.3.0/8"
            ("1.2.3.4", "1.2.3.44")
        """
        # Convert ranges in CIDR format into (start, end) tuple
        if isinstance(ip_range, six.string_types) and "/" in ip_range:
            # ignore bad value
            if not iptools.validate_cidr(ip_range):
                return
            ip_range = iptools.cidr2block(ip_range)

        # Find the integer representation of first 2 parts of the start and end IPs
        if isinstance(ip_range, tuple):
            # ignore bad ips
            if not iptools.validate_ip(ip_range[0]) or not iptools.validate_ip(ip_range[1]):
                return

            # Take the first 2 parts of the begin and end ip as integer
            start = iptools.ip2long(ip_range[0]) >> 16
            end = iptools.ip2long(ip_range[1]) >> 16
        else:
            start = iptools.ip2long(ip_range) >> 16
            end = start

        # for each integer in the range add an entry.
        for i in range(start, end+1):
            self.ip_ranges.setdefault(i, {})[iptools.IpRange(ip_range)] = value
コード例 #2
0
ファイル: utils.py プロジェクト: intr1nsic/omoto
    def __init__(self, cidr, low_gateway=False):
        if not iptools.validate_cidr(cidr):
            raise ConfigError("CIDR must be in proper format. e.g. 192.168.1.0/24")

        self.base, self.msk = cidr.split('/')
        self.mask = int(self.msk)
        self.num_addr = 2**(32-self.mask)
        self.first_address = iptools.ip2long(self.base)
        self.last_address = iptools.ip2long(self.base) + self.num_addr - 1
        self.low_gateway = low_gateway 
コード例 #3
0
ファイル: builtins.py プロジェクト: Fashiolista/gargoyle
 def _validate_cidr(self, value):
     if not iptools.validate_cidr(value):
         raise ValidationError('not a valid ip cidr notation', code='invalid')
コード例 #4
0
ファイル: addhost.py プロジェクト: kapsiry/vahti
        make_option('-n', '--network', dest="network"),
        )

    def add_host(self, ip, network):
        h = Host(ip=ip, network=network)
        self.stdout.write("%s adding\n" % ip)
        try:
            h.save()
        except IntegrityError, e:
            self.stderr.write("%s ERROR %s\n" % (ip, e))

    def handle(self, *args, **options):
        try:
            net = Network.objects.get(name=options['network'])
        except Network.DoesNotExist, e:
            self.stderr.write("ERROR %s\n" % e)
            return
        for target in args:
            if validate_ip(target):
                self.add_host(target, net)
            elif validate_cidr(target):
                hosts = list(IpRange(target))
                print hosts
                for host in hosts[1:-1]:
                    self.add_host(host, net)
            else:
                hosts = resolve_dns(target)
                for host in hosts:
                    self.add_host(host, net)