本文整理匯總了Python中ipaddress.ip_interface方法的典型用法代碼示例。如果您正苦於以下問題:Python ipaddress.ip_interface方法的具體用法?Python ipaddress.ip_interface怎麽用?Python ipaddress.ip_interface使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類ipaddress
的用法示例。
在下文中一共展示了ipaddress.ip_interface方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: search
# 需要導入模塊: import ipaddress [as 別名]
# 或者: from ipaddress import ip_interface [as 別名]
def search(self, queryset, name, value):
if not value.strip():
return queryset
qs_filter = Q(network__name__icontains=value) | Q(
network__irr_as_set__icontains=value
)
try:
ip = ipaddress.ip_interface(value.strip())
qs_filter |= Q(network_ixlan__ipaddr6__host=str(ip))
qs_filter |= Q(network_ixlan__ipaddr4__host=str(ip))
except ValueError:
pass
try:
qs_filter |= Q(network__asn=int(value.strip()))
qs_filter |= Q(network__info_prefixes6=int(value.strip()))
qs_filter |= Q(network__info_prefixes4=int(value.strip()))
except ValueError:
pass
return queryset.filter(qs_filter)
示例2: search
# 需要導入模塊: import ipaddress [as 別名]
# 或者: from ipaddress import ip_interface [as 別名]
def search(self, queryset, name, value):
if not value.strip():
return queryset
qs_filter = (
Q(autonomous_system__name__icontains=value)
| Q(internet_exchange__name__icontains=value)
| Q(internet_exchange__slug__icontains=value)
| Q(comments__icontains=value)
)
try:
ip = ipaddress.ip_interface(value.strip())
qs_filter |= Q(ip_address__host=str(ip))
except ValueError:
pass
try:
qs_filter |= Q(autonomous_system__asn=int(value.strip()))
except ValueError:
pass
return queryset.filter(qs_filter)
示例3: to_dict
# 需要導入模塊: import ipaddress [as 別名]
# 或者: from ipaddress import ip_interface [as 別名]
def to_dict(self):
dic = {
"key": "z" + str(self.id).split("-")[3],
"name": self.cloud_zone,
"zone_name": self.name,
"ip_pool": self.ip_pools()
}
dic.update(self.vars)
ip_start = ip_address(self.vars['ip_start'])
net_mask = self.vars.get('net_mask', None)
if net_mask:
interface = ip_interface("{}/{}".format(str(ip_start), net_mask))
dic["net_mask"] = interface.network.prefixlen
else:
dic["net_mask"] = 24
return dic
示例4: ip_pools
# 需要導入模塊: import ipaddress [as 別名]
# 或者: from ipaddress import ip_interface [as 別名]
def ip_pools(self):
ip_pool = []
ip_start = ip_address(self.vars['ip_start'])
ip_end = ip_address(self.vars['ip_end'])
if self.region.template.name == 'openstack':
while ip_start <= ip_end:
ip_pool.append(str(ip_start))
ip_start += 1
for ip in self.ip_used:
if ip in ip_pool:
ip_pool.remove(ip)
return ip_pool
net_mask = self.vars['net_mask']
interface = ip_interface("{}/{}".format(str(ip_start), net_mask))
network = interface.network
for host in network.hosts():
if ip_start <= host <= ip_end:
ip_pool.append(str(host))
for ip in self.ip_used:
if ip in ip_pool:
ip_pool.remove(ip)
return ip_pool
示例5: ptr_lookup
# 需要導入模塊: import ipaddress [as 別名]
# 或者: from ipaddress import ip_interface [as 別名]
def ptr_lookup(cls, network):
ip = str(ipaddress.ip_interface(network).ip)
try:
primary_hostname, alias_hostnames, other_ips = socket.gethostbyaddr(ip)
except socket.herror as e:
logger.debug('DNS Reverse Lookup Error {}'.format(e))
return Html.div('DNS: n/a')
content = Html.div(
'DNS: {}'.format(
socket.getfqdn(primary_hostname)
)
)
if alias_hostnames:
content += Html.div('DNS Aliases:')
for hostname in alias_hostnames:
fqdn_hostname = socket.getfqdn(hostname)
logger.debug('Alias {} FQDN {}'.format(hostname, fqdn_hostname))
content += Html.div(fqdn_hostname)
return content
示例6: __netjson_address
# 需要導入模塊: import ipaddress [as 別名]
# 或者: from ipaddress import ip_interface [as 別名]
def __netjson_address(self, address, interface):
ip = ip_interface(address)
family = 'ipv{0}'.format(ip.version)
netjson = OrderedDict(
(
('address', str(ip.ip)),
('mask', ip.network.prefixlen),
('proto', 'static'),
('family', family),
)
)
uci_gateway_key = 'gateway' if family == 'ipv4' else 'ip6gw'
gateway = interface.get(uci_gateway_key, None)
if gateway and ip_address(gateway) in ip.network:
netjson['gateway'] = gateway
del interface[uci_gateway_key]
return netjson
示例7: __intermediate_route
# 需要導入模塊: import ipaddress [as 別名]
# 或者: from ipaddress import ip_interface [as 別名]
def __intermediate_route(self, route, index):
network = ip_interface(route.pop('destination'))
target = network.ip if network.version == 4 else network.network
route.update(
{
'.type': 'route{0}'.format('6' if network.version == 6 else ''),
'.name': route.pop('name', None) or self.__get_auto_name(index),
'interface': route.pop('device'),
'target': str(target),
'gateway': route.pop('next'),
'metric': route.pop('cost'),
}
)
if network.version == 4:
route['netmask'] = str(network.netmask)
return self.sorted_dict(route)
示例8: __netjson_route
# 需要導入模塊: import ipaddress [as 別名]
# 或者: from ipaddress import ip_interface [as 別名]
def __netjson_route(self, route, i):
_name = route.pop('.name')
if _name != self.__get_auto_name(i):
route['name'] = _name
network = route.pop('target')
if 'netmask' in route:
network = '{0}/{1}'.format(network, route.pop('netmask'))
route.update(
{
"device": route.pop('interface'),
"destination": str(ip_interface(network)),
"next": route.pop('gateway'),
"cost": route.pop(
'metric', self._schema['properties']['cost']['default']
),
}
)
del route['.type']
return self.type_cast(route)
示例9: testHash
# 需要導入模塊: import ipaddress [as 別名]
# 或者: from ipaddress import ip_interface [as 別名]
def testHash(self):
self.assertEqual(hash(ipaddress.ip_interface('10.1.1.0/24')),
hash(ipaddress.ip_interface('10.1.1.0/24')))
self.assertEqual(hash(ipaddress.ip_network('10.1.1.0/24')),
hash(ipaddress.ip_network('10.1.1.0/24')))
self.assertEqual(hash(ipaddress.ip_address('10.1.1.0')),
hash(ipaddress.ip_address('10.1.1.0')))
# i70
self.assertEqual(hash(ipaddress.ip_address('1.2.3.4')),
hash(ipaddress.ip_address(
int(ipaddress.ip_address('1.2.3.4')._ip))))
ip1 = ipaddress.ip_address('10.1.1.0')
ip2 = ipaddress.ip_address('1::')
dummy = {}
dummy[self.ipv4_address] = None
dummy[self.ipv6_address] = None
dummy[ip1] = None
dummy[ip2] = None
self.assertIn(self.ipv4_address, dummy)
self.assertIn(ip2, dummy)
示例10: __init__
# 需要導入模塊: import ipaddress [as 別名]
# 或者: from ipaddress import ip_interface [as 別名]
def __init__(self, if1: IPIntf, if2: IPIntf,
if1address: Union[str, IPv4Interface, IPv6Interface],
if2address: Union[str, IPv4Interface, IPv6Interface],
bidirectional=True):
""":param if1: The first interface of the tunnel
:param if2: The second interface of the tunnel
:param if1address: The ip_interface address for if1
:param if2address: The ip_interface address for if2
:param bidirectional: Whether both end of the tunnel should be
established or not. GRE is stateless so there is
no handshake per-say, however if one end of the
tunnel is not established, the kernel will drop
by default the encapsulated packets."""
self.if1, self.if2 = if1, if2
self.ip1, self.gre1 = (ip_interface(str(if1address)),
self._gre_name(if1))
self.ip2, self.gre2 = (ip_interface(str(if2address)),
self._gre_name(if2))
self.bidirectional = bidirectional
self.setup_tunnel()
示例11: _configure_interface_address
# 需要導入模塊: import ipaddress [as 別名]
# 或者: from ipaddress import ip_interface [as 別名]
def _configure_interface_address(self, interface, address, default_route=None):
next_hop = None
net = ipaddress.ip_interface(address)
if default_route:
try:
next_hop = ipaddress.ip_address(default_route)
except ValueError:
self.logger.error("next-hop address {} could not be parsed".format(default_route))
sys.exit(1)
if default_route and next_hop not in net.network:
self.logger.error("next-hop address {} not in network {}".format(next_hop, net))
sys.exit(1)
subprocess.check_call(["ip", "-{}".format(net.version), "address", "add", str(net.ip) + "/" + str(net.network.prefixlen), "dev", interface])
if next_hop:
try:
subprocess.check_call(["ip", "-{}".format(net.version), "route", "del", "default"])
except:
pass
subprocess.check_call(["ip", "-{}".format(net.version), "route", "add", "default", "dev", interface, "via", str(next_hop)])
示例12: verify_ipv4_routing_mesh
# 需要導入模塊: import ipaddress [as 別名]
# 或者: from ipaddress import ip_interface [as 別名]
def verify_ipv4_routing_mesh(self):
"""Verify hosts can route to each other via FAUCET."""
host_pair = self.hosts_name_ordered()[:2]
first_host, second_host = host_pair
first_host_routed_ip = ipaddress.ip_interface('10.0.1.1/24')
second_host_routed_ip = ipaddress.ip_interface('10.0.2.1/24')
second_host_routed_ip2 = ipaddress.ip_interface('10.0.3.1/24')
self.verify_ipv4_routing(
first_host, first_host_routed_ip,
second_host, second_host_routed_ip)
self.verify_ipv4_routing(
first_host, first_host_routed_ip,
second_host, second_host_routed_ip2)
self.swap_host_macs(first_host, second_host)
self.verify_ipv4_routing(
first_host, first_host_routed_ip,
second_host, second_host_routed_ip)
self.verify_ipv4_routing(
first_host, first_host_routed_ip,
second_host, second_host_routed_ip2)
示例13: verify_ipv6_routing_mesh
# 需要導入模塊: import ipaddress [as 別名]
# 或者: from ipaddress import ip_interface [as 別名]
def verify_ipv6_routing_mesh(self):
"""Verify IPv6 routing between hosts and multiple subnets."""
host_pair = self.hosts_name_ordered()[:2]
first_host, second_host = host_pair
first_host_ip = ipaddress.ip_interface('fc00::1:1/112')
second_host_ip = ipaddress.ip_interface('fc00::1:2/112')
first_host_routed_ip = ipaddress.ip_interface('fc00::10:1/112')
second_host_routed_ip = ipaddress.ip_interface('fc00::20:1/112')
second_host_routed_ip2 = ipaddress.ip_interface('fc00::30:1/112')
self.verify_ipv6_routing_pair(
first_host, first_host_ip, first_host_routed_ip,
second_host, second_host_ip, second_host_routed_ip)
self.verify_ipv6_routing_pair(
first_host, first_host_ip, first_host_routed_ip,
second_host, second_host_ip, second_host_routed_ip2)
self.swap_host_macs(first_host, second_host)
self.verify_ipv6_routing_pair(
first_host, first_host_ip, first_host_routed_ip,
second_host, second_host_ip, second_host_routed_ip)
self.verify_ipv6_routing_pair(
first_host, first_host_ip, first_host_routed_ip,
second_host, second_host_ip, second_host_routed_ip2)
示例14: validate_whitelist
# 需要導入模塊: import ipaddress [as 別名]
# 或者: from ipaddress import ip_interface [as 別名]
def validate_whitelist(self, data):
for address in data:
try:
ipaddress.ip_address(address)
except:
try:
ipaddress.ip_network(address)
except:
try:
ipaddress.ip_interface(address)
except:
raise serializers.ValidationError(
"The address {} is not valid".format(address))
return data
示例15: get_interface
# 需要導入模塊: import ipaddress [as 別名]
# 或者: from ipaddress import ip_interface [as 別名]
def get_interface(ip_address: Union[str, ipaddress.IPv4Address, ipaddress.IPv6Address]) \
-> Union[ipaddress.IPv4Interface, ipaddress.IPv6Interface]:
"""tries to find the network interface that connects to the given host's address"""
if isinstance(ip_address, str):
ip_address = get_ip_address(ip_address)
family = socket.AF_INET if ip_address.version == 4 else socket.AF_INET6
with socket.socket(family, socket.SOCK_DGRAM) as sock:
sock.connect((str(ip_address), 53)) # 53=dns
return ipaddress.ip_interface(sock.getsockname()[0])