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
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
def dec_to_bin(ip_address): # Sanitize Input with validate_ip function if validate_ip(ip_address) == True: # Extract octects from ip_address ip_octets = ip_address.split('.') # Convert each octet into binary bin_octets = [] for i,ip_octet in enumerate (ip_octets): bin_octet = (bin(int(ip_octet))).split('b').pop() # Zero pad each octet (function written in this excersise) bin_octet = zero_pad_bin(bin_octet) bin_octets.append(bin_octet) # Build string of binary octets bin_addr = ".".join(bin_octets) return bin_addr else: print 'Error: Function dec_to_bin() detected an invalid IP (input).' quit()
def dec_to_bin(ip_address): # Sanitize Input with validate_ip function if validate_ip(ip_address) == True: # Extract octects from ip_address ip_octets = ip_address.split('.') # Convert each octet into binary bin_octets = [] for i, ip_octet in enumerate(ip_octets): bin_octet = (bin(int(ip_octet))).split('b').pop() # Zero pad each octet (function written in this excersise) bin_octet = zero_pad_bin(bin_octet) bin_octets.append(bin_octet) # Build string of binary octets bin_addr = ".".join(bin_octets) return bin_addr else: print 'Error: Function dec_to_bin() detected an invalid IP (input).' quit()
def is_valid_bucket_name(bucket_name): """ Check if bucket_name is a valid S3 bucket name (as defined by the AWS docs): 1. 3 <= len(bucket_name) <= 255 2. all chars one of: a-z 0-9 . _ - 3. first char one of: a-z 0-9 4. name must not be a valid ip """ regex = re.compile('[a-z0-9][a-z0-9\._-]{2,254}$') if not regex.match(bucket_name): return False if iptools.validate_ip(bucket_name): return False return True
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)