def testEmbeddedIpv4(self): ipv4_string = '192.168.0.1' ipv4 = ipaddr.IPv4Network(ipv4_string) v4compat_ipv6 = ipaddr.IPv6Network('::%s' % ipv4_string) self.assertEqual(int(v4compat_ipv6.ip), int(ipv4.ip)) v4mapped_ipv6 = ipaddr.IPv6Network('::ffff:%s' % ipv4_string) self.assertNotEqual(v4mapped_ipv6.ip, ipv4.ip) self.assertRaises(ipaddr.AddressValueError, ipaddr.IPv6Network, '2001:1.1.1.1:1.1.1.1')
def find_ip(): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(("8.8.8.8", 80)) x = s.getsockname()[0] mask = ipaddr.IPv4Network(x) y = mask.netmask t1.insert(END, x) t2.insert(END, y) s.close()
def __init__(self): self.ipv4_networks = [] for ip in WHATSAPP_IPV4.split("\n"): try: self.ipv4_networks.append(ipaddr.IPv4Network(ip)) except Exception: log.err("IP is wrong") log.msg(ip) self.ipv6_networks = map(ipaddr.IPv6Network, WHATSAPP_IPV6.split("\n"))
def _CheckCIDRNetNotation(value): """Ensure a given CIDR notation type is valid. """ try: ipaddr.IPv4Network(value) except ipaddr.AddressValueError: return False return True
def _construct_start_args(self, node_name, http_port, gossip_port, genesis): # only create 'genesis' ledger if there is not existing network if len(self.get_node_names()) > 0: genesis = False # Check for running the network sawtooth and get the subnet subnet_arg = ['docker', 'network', 'inspect', 'sawtooth'] try: output = yaml.load(subprocess.check_output(subnet_arg)) subnet = unicode(output[0]['IPAM']['Config'][0]['Subnet']) subnet_list = list(ipaddr.IPv4Network(subnet)) except subprocess.CalledProcessError as e: raise CliException(str(e)) num = int(node_name[len('validator-'):]) + 3 if num < len(subnet_list) - 1: ip_addr = str(subnet_list[num]) else: raise CliException("Out of Usable IP Addresses") local_project_dir = '/project' args = ['docker', 'run', '-t', '-d', '--network', 'sawtooth'] args.extend(['--name', node_name]) args.extend(['--label', 'sawtooth.cluster=default']) args.extend(['--ip', ip_addr]) args.extend(['-p', str(http_port)]) args.extend(['-p', '{}/udp'.format(gossip_port)]) args.extend(['-e', 'CURRENCYHOME=/project/sawtooth-core/validator']) args.extend(['-v', '{}:/project'.format(local_project_dir)]) args.append('sawtooth-build-ubuntu-xenial') args.extend(['bash', '-c']) cmd = [] bin_path = '/project/sawtooth-core/bin' initial_connectivity = 0 if genesis else 1 cmd.append('echo "{\\\"InitialConnectivity\\\": %d}"' % initial_connectivity) cmd.append('> ${CURRENCYHOME}/data/%s.json;' % node_name) if genesis: cmd.append('%s/sawtooth keygen %s; ' % (bin_path, node_name)) cmd.append('%s/sawtooth admin' % bin_path) cmd.append('poet1-genesis -vv --node %s; exec' % node_name) cmd.append('%s/txnvalidator' % bin_path) cmd.extend(['--node', node_name]) cmd.append('-vv') cmd.append("--listen '{}:{}/UDP gossip'".format(ip_addr, gossip_port)) cmd.append("--listen '{}:{}/TCP http'".format(ip_addr, http_port)) # Set Ledger Url cmd.append("--url 'http://{}:8800'".format(str(subnet_list[3]))) cmd.append('--config ${CURRENCYHOM}}/data/%s.json' % node_name) args.append(' '.join(cmd)) return args
def test_balance_by_load(self): self.balancer = SimpleBalancer(ignoreSensorLoad=0, ignorePrefixBW=0) self.balancer = SimpleBalancer() sensors = defaultdict(list) sensors[1] = { "sensor_id": 1, "of_port_id": 1, "description": "sensor foo" } sensors[2] = { "sensor_id": 2, "of_port_id": 2, "description": "sensor foo2" } res = self.balancer.addSensorGroup({ "group_id": 1, "bw": "10GE", "admin_status": "active", "description": "some descr", "sensors": sensors }) self.assertTrue(res == 1) sensors2 = defaultdict(list) sensors2[1] = { "sensor_id": 3, "of_port_id": 3, "description": "sensor foo3" } sensors2[2] = { "sensor_id": 4, "of_port_id": 4, "description": "sensor foo4" } res = self.balancer.addSensorGroup({ "group_id": 2, "bw": "10GE", "admin_status": "active", "description": "some descr", "sensors": sensors2 }) self.assertTrue(res == 1) net = ipaddr.IPv6Network("2001:0DB8::/48") res = self.balancer.addGroupPrefix(1, net, 0) self.assertTrue(res == 1) net2 = ipaddr.IPv4Network("10.0.0.0/10") res = self.balancer.addGroupPrefix(1, net2, 0) self.assertTrue(res == 1) res = self.balancer.setPrefixBW(net, 5000000, 5000000) self.assertTrue(res == 1) res = self.balancer.setPrefixBW(net2, 500, 500) self.assertTrue(res == 1) res = self.balancer.setSensorLoad(1, .9) self.assertTrue(res == 1) res = self.balancer.setSensorLoad(2, .1) self.assertTrue(res == 1) self.balancer.balance()
def get(self, ip): vlan = str([ item[0] for item in VLANS.vlan_map.items() if ipaddr.IPv4Address(ip) in ipaddr.IPv4Network( str(ipaddr.IPv4Interface(item[1]['address']).network_address) + str( maskre.search(str(ipaddr.IPv4Interface( item[1]['address']))).group(1))) ][0]) self.write(vlan)
def get_ip_list_from_net(net): """ 网段: net: '211.195.0.0/24' """ try: net = ipaddr.IPv4Network(net) except ipaddr.AddressValueError: return [] return [ip for ip in net.iterhosts() if not (ip.is_private or ip.is_loopback)]
def __init__(self): self.log = logging.getLogger("avocado.test") parser = cartesian_config.Parser() cfg = os.path.join(settings.get_value('datadir.paths', 'base_dir'), 'config/tests.cfg') parser.parse_file(cfg) dicts = parser.get_dicts() self.ips_list = [] self.post_check = 'false' execute_flag = True for params in (_ for _ in dicts): if execute_flag: self.params = params self.post_check = params.get('perform_health_check_after_job') host_list = params.get("host_ips") if host_list: host_list = host_list.split(",") else: return for item in host_list: if item.find("/") != -1: for ip_info in ipaddr.IPv4Network(item): self.ips_list.append(str(ip_info)) elif item.find("-") != -1: begin_ip, end_ip = item.split("-") ip_ranges = ipaddr.summarize_address_range( ipaddr.IPv4Address(begin_ip), ipaddr.IPv4Address(end_ip)) for ip_range in ip_ranges: for ip_info in ipaddr.IPv4Network(str(ip_range)): self.ips_list.append(str(ip_info)) else: self.ips_list.append(item) self.ips_list = sorted(set(self.ips_list), key=self.ips_list.index) self.log.info("All health check ip list:") self.log.info(self.ips_list) execute_flag = False if 'health_check' in params.get('ct_type'): self.post_check = params.get('perform_health_check_after_job') break self.post_check = self.post_check.lower() == "true"
def check_spf1(sender, ip): domain = sender.split('@')[-1] array = SPF2IP.SPF2IP(domain).IPArray() if len(array) == 0: return None ip = ipaddr.IPv4Address(ip) for net in array: if ip in ipaddr.IPv4Network(net): return True return False
def testGetitem(self): # http://code.google.com/p/ipaddr-py/issues/detail?id=15 addr = ipaddr.IPv4Network('172.31.255.128/255.255.255.240') self.assertEqual(28, addr.prefixlen) addr_list = list(addr) self.assertEqual('172.31.255.128', str(addr_list[0])) self.assertEqual('172.31.255.128', str(addr[0])) self.assertEqual('172.31.255.143', str(addr_list[-1])) self.assertEqual('172.31.255.143', str(addr[-1])) self.assertEqual(addr_list[-1], addr[-1])
def generate_from_subnets(subnets): key_set = set() for subnet in subnets: subnet = subnet.strip() sub = ipaddr.IPv4Network(subnet) length = int(math.ceil(sub.prefixlen/8.0)) new_set = {binascii.hexlify(a.packed[0:length]) for a in sub.iter_subnets(new_prefix=length*8)} key_set = key_set.union(new_set) return key_set
def connectContainerToSwitch(sw,host,containerID,of_port): hostIP=host['ip'] mac=host['mac'] nw = ipaddr.IPv4Network(hostIP) broadcast = "{}".format(nw.broadcast) router = "{}".format(nw.network + 1) cmd=['/vagrant/ovswork.sh',sw,containerID,hostIP,broadcast,router,mac,of_port,host['name']] if host.has_key('vlan'): cmd.append(host['vlan']) call(cmd)
def test_update_prefix_bw(self): #first setup the handler to get all the flows that were sent flows = [] def flowSent(dpid=None, domain=None, header=None, actions=None, command=None, priority=None, idle_timeout=None, hard_timeout=None): flows.append({ 'dpid': dpid, 'header': header, 'actions': actions, 'command': command, 'priority': priority, 'idle_timeout': idle_timeout, 'hard_timeout': hard_timeout }) self.api.registerForwardingStateChangeHandler(flowSent) datapath = Mock(id=1) self.api.switchJoined(datapath) self.assertEquals(len(flows), 25) self.api.updatePrefixBW("%016x" % datapath.id, ipaddr.IPv4Network("10.0.19.0/24"), 500, 500) self.assertTrue( self.api.getBalancer("%016x" % datapath.id, "R&E").getPrefixBW( ipaddr.IPv4Network("10.0.19.0/24")), 1000) self.api.updatePrefixBW("%016x" % datapath.id, ipaddr.IPv4Network("10.0.17.0/24"), 500, 500) self.assertTrue( self.api.getBalancer("%016x" % datapath.id, "R&E").getPrefixBW( ipaddr.IPv4Network("10.0.17.0/24")), 1000) self.api.updatePrefixBW("%016x" % datapath.id, ipaddr.IPv6Network("2001:0DB8::/48"), 500, 500) self.assertTrue( self.api.getBalancer("%016x" % datapath.id, "R&E").getPrefixBW( ipaddr.IPv6Network("2001:0DB8::/48")), 1000)
def clean(self, value): if not value: return value try: value = str(ipaddr.IPv4Network(value)) except ipaddr.AddressValueError as e: raise forms.ValidationError(_("Invalid address: %s") % e) except ipaddr.NetmaskValueError as e: raise forms.ValidationError(_("Invalid network: %s") % e) value = super(Network4Field, self).clean(value) return value
def getRandomIP(subnet, mask, ip6=False): nw = subnet + '/' + mask if ip6: network = ipaddr.IPv6Network(nw) random_ip = ipaddr.IPv6Address(random.randrange(int(network.network) + 1,\ int(network.broadcast) - 1)) else: network = ipaddr.IPv4Network(nw) random_ip = ipaddr.IPv4Address(random.randrange(int(network.network) + 1,\ int(network.broadcast) - 1)) return random_ip
def create_specific_rtes(routes): rtes = [] for requested_rt in routes: ip = ipaddr.IPv4Network(requested_rt) rtes.append( ripserv.RIPRouteEntry(address=ip.ip.exploded, mask=ip.prefixlen, nexthop="0.0.0.0", metric=0, tag=0)) return rtes
def find_path_to_destination(self, destination): ''' Find routes destination_ip_address (str): The destination IP address to which the packet must be routed. ''' path = [] dest = ipaddr.IPv4Network(destination) for cidr, v in self.routing_table.iteritems(): entry = dict.fromkeys(['as_path', 'next_hop', 'prefix_len', 'source_as'], '') curr_net = ipaddr.IPv4Network(cidr) # build network from CIDR address if dest in curr_net: # this method is a built in method to see whether one range of CIDR covers another entry['as_path'] = v['as_path'] entry['next_hop'] = v['next_hop'] entry['prefix_len'] = int(cidr.split('/')[1]) entry['source_as'] = v['source_as'] path.append(entry) path.sort(key = lambda x: x['prefix_len'], reverse=True) if len(path) == 0: return [{"as_path": None, "next_hop": None, "prefix_len": None, "source_as": None}] return path
def CIDR_to_IPRange(self, ip): #ipv4 = ipaddr.IPv4Address(ip) ipv4 = ipaddr.IPv4Network(ip) print(ipv4, "'s CIDR to IPRange\n") print("Is private IP?: ", ipv4.is_private) print("Total Host: ", ipv4.numhosts) print("(broadcast)Last IP: ", ipv4.broadcast) ##廣播位址 Last IP print("Widlcard Bits: ", ipv4.hostmask) ##Widlcard Bits print("Netmask: ", ipv4.netmask) ##Netmask print("Is loopback IP?: ", ipv4.is_loopback) ##是否是loopback位址 print("ip range is : ", str(ipv4[0]), "to", ipv4.broadcast)
def add_subnet(subnets, subnet_cidr_str, gateway_str=None): value = {} subnet = ipaddr.IPv4Network(subnet_cidr_str) value['network'] = subnet.network value['netmask'] = subnet.netmask value['broadcast'] = subnet.broadcast if gateway_str is not None and gateway_str != '': gateway = ipaddr.IPv4Address(gateway_str) value['gateway'] = gateway subnets.append(value)
def resolve_subnets_to_hosts(self, json, location): if not json: return None iplist = [] for subnet in json['results']: if subnet['location'] != location: continue #other area ips = '%s/%s' % (subnet['subnet'], subnet['netmask']) for ip in ipaddr.IPv4Network(ips).iterhosts(): #no broadcast ip ip = str(ip) if ip in subnet['gateways']: continue iplist.append(ip) return iplist
def set_routes(self): """ Set the covert routes to use the VPN. """ # On windows, we need to make sure that the TUN device is "ready" # before we try to add routes through it; otherwise the routes will # be rejected. time.sleep(10) # Set the route(s) # # NOTE: we make no effort to check that the set of routes # is sensible and consistent. This should be done elsewhere. # This routine just plugs them in, ignoring any errors that # might occur. # # FIXME: we don't even log errors. lame. # for subnet in self._covert_subnets: subnet_ip = ipaddr.IPv4Network(subnet) subnet_prefix = str(subnet_ip.network) subnet_mask = str(subnet_ip.netmask) # Must drop routes before we can replace them. # This should be harmless if the routes are absent, # so just blindly pre-drop everything we want to add. # FIXME: don't use os.system route_cmd = 'route delete %s' % (subnet_mask,) os.system(route_cmd) route_cmd = 'route add %s MASK %s %s' % ( subnet_prefix, subnet_mask, self.tun_ip) # print "subnet route: " + route_cmd # FIXME: don't use os.system os.system(route_cmd) if self._set_default: # FIXME: don't use os.system # route_cmd = 'route delete 0.0.0.0' # os.system(route_cmd) route_cmd = 'route add 0.0.0.0 MASK 128.0.0.0 %s' % ( self.tun_ip,) print 'route_cmd: [%s]' % (route_cmd,) os.system(route_cmd) route_cmd = 'route add 128.0.0.0 MASK 128.0.0.0 %s' % ( self.tun_ip,) print 'route_cmd: [%s]' % (route_cmd,) os.system(route_cmd) print 'default route set'
def __init__(self, src_net, max_connections): self.free_list = range(max_connections) self.net = ipaddr.IPv4Network(src_net) self.ports = range(1025, 65536) self.ips = [] num_ips = max_connections / len(self.ports) + 1 if num_ips > (self.net.numhosts - 2): raise InsufficientNetAddresses() for i in range(1, num_ips + 1): self.ips.append(str(self.net[i]))
def GenerateDefinition(self, config, unused_global_config): """Generates a list of all nodes in a network definition. This method basically processes all the configuration which is hierarchically below "networks" in the "definitions" section in the configuration file to generate a list of all nodes in that definition. Args: config: YAML configuration structure (dictionaries, lists and strings) representing the "networks" section in "definitions" of the configuration file. unused_global_config: YAML configuration structure (dictionaries, lists and strings) representing the "global" section of the configuration file. Returns: Tuples of IPNetwork objects and string comments representing all the nodes in one definition. Raises: DefinateConfigError: The configuration is not well formed. DnsGeneratorError: There is a problem generating the output. """ nodes = [] yaml_structure = { 'names': ['str'], 'types': ['str'], } for network in config: self._yaml_validator.CheckConfiguration(network, yaml_structure) for typ in network['types']: if typ not in self.SUPPORTED_TYPES: raise DnsGeneratorError('Unsupported DNS type found: %s' % typ) for name in network['names']: try: addr_list = socket.getaddrinfo(name, None) except socket.gaierror: raise DnsGeneratorError('Hostname not found: %s' % name) for family, _, _, _, sockaddr in addr_list: ip_addr = None if family == socket.AF_INET and 'A' in network['types']: # sockaddr = (address, port) ip_addr = ipaddr.IPv4Network(sockaddr[0]) elif family == socket.AF_INET6 and 'AAAA' in network[ 'types']: # sockaddr = (address, port, flow info, scope id) ip_addr = ipaddr.IPv6Network(sockaddr[0]) else: logging.debug('Skipping unknown AF \'%d\' for: %s', family, name) if ip_addr: nodes.append((ip_addr, name)) return nodes
def do_expect(expect_rule_type): links = ovs.tests['bidi-nat'][scenario]['links'] logging.info("\n<<< EXPECT : %s >>>\n" % expect_rule_type) for link in links: c = link['c_domain'] p = link['p_domain'] c_spat_ip_from_p = link["c_spat_ip_from_p"] p_spat_cfg = link["p_spat_cfg"] p_maps = link["p_maps"] c_maps = link["c_maps"] c_exit_domain = link["c_exit_domain"] e = "" bidir = True if (expect_rule_type == 'bidir_exit_domain') else False if expect_rule_type in ['bidir_exit_domain', 'overlay_exit_domain']: for v in c_exit_domain: rule_pattern = ( BIDIR_EXIT_DOMAIN % (str(hex(int(c))), ipaddr.IPv4Network(ovs.vms[v + '-veth1']['ip'] + '/24').network, str(hex(int(p))))) rule = ovs.expect_rule(rule_pattern, bidir, 'Bidir Exit-Domain') rule_pattern = ( BIDIR_SNAT % (str(hex(int(p))), str(hex(int(c))), ipaddr.IPv4Network(ovs.vms[v + '-veth1']['ip'] + '/24').network)) ovs.expect_rule(rule_pattern, bidir, 'table=26 SNAT') rule_pattern = ( BIDIR_SNAT % (str(hex(int(c))), str(hex(int(p))), ipaddr.IPv4Network(ovs.vms[v + '-veth1']['ip'] + '/24').network)) ovs.expect_rule(rule_pattern, False, 'table=26 SNAT') rule_pattern = (OVERLAY_EXIT_DOMAIN \ %(str(hex(int(c))), ipaddr.IPv4Network(ovs.vms[v+'-veth1']['ip']+ '/24').network)) ovs.expect_rule(rule_pattern, not bidir, 'Overlay Exit-Domain')
def clean(self): cleaned_data = super(NetworkForm, self).clean() network_str = cleaned_data.get('network_str', '') try: ip_type = cleaned_data.get('ip_type') if ip_type not in IP_TYPES: raise ValidationError("IP type must be either IPv4 or IPv6.") if ip_type == IP_TYPE_4: network = ipaddr.IPv4Network(network_str) ip_upper, ip_lower = 0, int(network.network) elif ip_type == IP_TYPE_6: network = ipaddr.IPv6Network(network_str) ip_upper, ip_lower = ipv6_to_longs(network.network) if cleaned_data["routers"]: for key in ["gateway", "subnet_mask"]: value = cleaned_data.get(key) if value: if ip_type == IP_TYPE_4: value = ipaddr.IPv4Network(value) size = 32 elif ip_type == IP_TYPE_6: value = ipaddr.IPv6Network(value) size = 128 if key == "gateway" and not network.overlaps(value): raise ValidationError("Network does not contain " "specified gateway.") if key == "subnet_mask": binstring = bin(int(value.broadcast)) binstring = "".join(binstring[2:]) if "01" in binstring or len(binstring) != size: raise ValidationError("Invalid subnet mask.") prefixlen = binstring.count('1') if prefixlen > network.prefixlen: raise ValidationError( "Subnet mask is smaller than network.") value = str(value.broadcast) cleaned_data[key] = value except ipaddr.AddressValueError, e: raise ValidationError("Bad IP address {0}".format(e))
def ip_to_site(ip_str, base_site_path): """Given the dotted decimal of an IP address, find the site it corresponds to. If no site is found return None. This function assumes that the ip is in valid format. :param ip_str: The ip to use. :type ip_str: str :param base_site_path: The dir containing all other sites (usually MOZ_SITE_PATH) :type base_site_path: str :returns key_value: The kv representing the site the ip belongs in. :type key_value: truth.models.KeyValue """ print "IP string" + ip_str ip = ipaddr.IPv4Network(ip_str) # This will end up being <ip>/32 sites = get_all_sites(base_site_path) ip2v = Truth.objects.get(name='ip_to_vlan_mapping').keyvalue_set.all() for kv in ip2v: vlan_site, network_str = kv.key, kv.value if ipaddr.IPv4Network(network_str).overlaps(ip): return kv return None
def process(self): #The processing thread. Take data from the input(s) and generate information to be sent to the output. #This generic backend takes one packet from the input and is the base for the station detector. Other more complex backend may be more effective if not inherited from this. all_zeros = ipaddr.IPv4Network('0.0.0.0/0') invalid_masks = [ipaddr.IPv4Address('255.255.255.255'),ipaddr.IPv4Address('255.255.255.254'),ipaddr.IPv4Address('255.255.0.0'),ipaddr.IPv4Address('255.254.0.0'),ipaddr.IPv4Address('255.252.0.0'),ipaddr.IPv4Address('255.248.0.0'),ipaddr.IPv4Address('255.240.0.0'),ipaddr.IPv4Address('255.224.0.0'),ipaddr.IPv4Address('255.192.0.0'),ipaddr.IPv4Address('255.128.0.0'),ipaddr.IPv4Address('255.0.0.0'),ipaddr.IPv4Address('254.0.0.0'),ipaddr.IPv4Address('252.0.0.0'),ipaddr.IPv4Address('248.0.0.0'),ipaddr.IPv4Address('240.0.0.0'),ipaddr.IPv4Address('224.0.0.0'),ipaddr.IPv4Address('192.0.0.0'),ipaddr.IPv4Address('128.0.0.0'),ipaddr.IPv4Address('0.0.0.0')] while True: data = self.input() information = self.extract_info(data) if information['network'].netmask not in invalid_masks: self.output(information)
def saveVlanToIP(sender, **kwargs): obj = kwargs['instance'] ipStr = kwargs['instance'].ip ipObj = ipaddr.IPv4Address(ipStr) if not obj.vlan: for i in Vlan.network_objects.values(): subObj = ipaddr.IPv4Network(i['network']) if ipObj in subObj: # We creates a Vlan object based on the id from the vlans we are looping trough obj.vlan = Vlan.network_objects.get(id=i['id']) obj.save() break
def tupleIp(mip, ports): Portlist = glist(ports) tlist = [] if '/' in mip: hosts = ipaddr.IPv4Network(mip).iterhosts() for i in hosts: for j in Portlist: tlist.append((str(i), int(j))) else: for j in Portlist: tlist.append((str(mip), int(j))) return tlist