本文整理汇总了Python中netaddr.IPSet.add方法的典型用法代码示例。如果您正苦于以下问题:Python IPSet.add方法的具体用法?Python IPSet.add怎么用?Python IPSet.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类netaddr.IPSet
的用法示例。
在下文中一共展示了IPSet.add方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_ipset_converts_to_cidr_networks_v6
# 需要导入模块: from netaddr import IPSet [as 别名]
# 或者: from netaddr.IPSet import add [as 别名]
def test_ipset_converts_to_cidr_networks_v6():
s1 = IPSet(IPNetwork('fe80::4242/64'))
s1.add(IPNetwork('fe90::4343/64'))
assert list(s1.iter_cidrs()) == [
IPNetwork('fe80::/64'),
IPNetwork('fe90::/64'),
]
示例2: test_ipset_converts_to_cidr_networks_v4
# 需要导入模块: from netaddr import IPSet [as 别名]
# 或者: from netaddr.IPSet import add [as 别名]
def test_ipset_converts_to_cidr_networks_v4():
s1 = IPSet(IPNetwork('10.1.2.3/8'))
s1.add(IPNetwork('192.168.1.2/16'))
assert list(s1.iter_cidrs()) == [
IPNetwork('10.0.0.0/8'),
IPNetwork('192.168.0.0/16'),
]
示例3: parse
# 需要导入模块: from netaddr import IPSet [as 别名]
# 或者: from netaddr.IPSet import add [as 别名]
def parse(self, data):
mynets = IPSet()
for line in data.split("\n"):
if not line or line[0] == ";":
continue
ip, sbl = line.split(";")
ip = IPNetwork(ip.strip())
mynets.add(ip)
return mynets
示例4: test_ipset_member_insertion_and_deletion
# 需要导入模块: from netaddr import IPSet [as 别名]
# 或者: from netaddr.IPSet import add [as 别名]
def test_ipset_member_insertion_and_deletion():
s1 = IPSet()
s1.add('192.0.2.0')
assert s1 == IPSet(['192.0.2.0/32'])
s1.remove('192.0.2.0')
assert s1 == IPSet([])
s1.add(IPRange("10.0.0.0", "10.0.0.255"))
assert s1 == IPSet(['10.0.0.0/24'])
s1.remove(IPRange("10.0.0.128", "10.10.10.10"))
assert s1 == IPSet(['10.0.0.0/25'])
示例5: summarizeIPs
# 需要导入模块: from netaddr import IPSet [as 别名]
# 或者: from netaddr.IPSet import add [as 别名]
def summarizeIPs(inFile, outFile):
netSet = IPSet()
with open(inFile, 'r') as f:
for line in f.readlines():
net = IPSet()
try:
net.add(line.strip())
except AddrFormatError:
continue
else:
netSet = netSet | net
netMin = netSet.iter_cidrs()
with open(outFile, 'w') as f:
for net in netMin:
f.write('{}\n'.format(net))
示例6: test_ipset_adding_and_removing_members_ip_addresses_as_ints
# 需要导入模块: from netaddr import IPSet [as 别名]
# 或者: from netaddr.IPSet import add [as 别名]
def test_ipset_adding_and_removing_members_ip_addresses_as_ints():
s1 = IPSet(['10.0.0.0/25'])
s1.add('10.0.0.0/24')
assert s1 == IPSet(['10.0.0.0/24'])
integer1 = int(IPAddress('10.0.0.1'))
integer2 = int(IPAddress('fe80::'))
integer3 = int(IPAddress('10.0.0.2'))
s2 = IPSet([integer1, integer2])
assert s2 == IPSet(['10.0.0.1/32', 'fe80::/128'])
s2.add(integer3)
assert s2 == IPSet(['10.0.0.1/32', '10.0.0.2/32', 'fe80::/128'])
s2.remove(integer2)
assert s2 == IPSet(['10.0.0.1/32', '10.0.0.2/32'])
s2.update([integer2])
assert s2 == IPSet(['10.0.0.1/32', '10.0.0.2/32', 'fe80::/128'])
示例7: valid
# 需要导入模块: from netaddr import IPSet [as 别名]
# 或者: from netaddr.IPSet import add [as 别名]
def valid(self):
"""
Make sure the input parameters class stick to the form of below example
parameters_tuple(src_zone = 'untrust', src_ip = ['100.1.4.2/32', '100.1.2.0/24'],
dst_zone = 'trust', dst_ip = ['10.1.3.0/30'],
application = {'tcp': {'dst-port': ['80', '20'], 'src-port': ['any', '5-20']}}
)
"""
def ip_validate(ip):
ip_validator = re.compile('^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)/(\d{1,2})$')
if ip_validator.match(ip) != None and int(ip_validator.match(ip).groups()[3]) <= 32:
return True
return False
def application_validate(application):
"""
Unfold the ports from 1-3 to [1,2,3], keep 'any' in it origin form
"""
try:
for tuple in application.values():
if not isinstance(tuple, dict) or not isinstance(tuple['src-port'], list) or not isinstance(
tuple['dst-port'], list):
return False
src_port_list = []
for port in tuple['src-port']:
if not port_validate(port):
return False
src_port_list.extend(port_cal(port))
tuple['src-port'] = src_port_list
dst_port_list = []
for port in tuple['dst-port']:
if not port_validate(port):
return False
dst_port_list.extend(port_cal(port))
tuple['dst-port'] = dst_port_list
except Exception as e:
logging.warn(str(application) + ' is not a valid application input, and the error is "' + str(e) + '"')
return False
else:
return True
def port_validate(port):
port_validator = re.compile(r'(\d+-\d+)|(\d*)|any')
if port_validator.match(port) is None:
return False
return True
src_ip = IPSet([])
for ip in self.src_ip:
if ip == 'any':
ip = '0.0.0.0/0'
if not ip_validate(ip):
return False
src_ip.add(ip)
self.src_ip = src_ip
dst_ip = IPSet([])
for ip in self.dst_ip:
if ip == 'any':
ip = '0.0.0.0/0'
if not ip_validate(ip):
return False
dst_ip.add(ip)
self.dst_ip = dst_ip
if not application_validate(self.application):
return False
return True
示例8: parse_ip_set
# 需要导入模块: from netaddr import IPSet [as 别名]
# 或者: from netaddr.IPSet import add [as 别名]
def parse_ip_set(ipaddrs):
"""Parse a string specification into an IPSet.
This function takes a string representing a set of IP addresses and
parses it into an IPSet object. Acceptable formats for the string
include:
* "all": all possible IPv4 and IPv6 addresses
* "local": all local addresses of the machine
* "A.B.C.D" a single IP address
* "A.B.C.D/N" a network address specification
* "A.B.C.*" a glob matching against all possible numbers
* "A.B.C.D-E" a glob matching against a range of numbers
* a whitespace- or comma-separated string of the above
"""
ipset = IPSet()
ipaddrs = ipaddrs.lower().strip()
if not ipaddrs:
return ipset
for ipspec in _COMMA_OR_WHITESPACE.split(ipaddrs):
# The string "local" maps to all local addresses on the machine.
if ipspec == "local":
ipset.add(IPNetwork("127.0.0.0/8"))
for addr in get_local_ip_addresses():
ipset.add(addr)
# The string "all" maps to app IPv4 and IPv6 addresses.
elif ipspec == "all":
ipset.add(IPNetwork("0.0.0.0/0"))
ipset.add(IPNetwork("::"))
# Strings containing a "/" are assumed to be network specs
elif "/" in ipspec:
ipset.add(IPNetwork(ipspec))
# Strings containing a "*" or "-" are assumed to be glob patterns
elif "*" in ipspec or "-" in ipspec:
for cidr in IPGlob(ipspec).cidrs():
ipset.add(cidr)
# Anything else must be a single address
else:
ipset.add(IPAddress(ipspec))
return ipset
示例9: targets_to_ip_list
# 需要导入模块: from netaddr import IPSet [as 别名]
# 或者: from netaddr.IPSet import add [as 别名]
def targets_to_ip_list(targets):
ipset = IPSet()
for t in targets:
ipset.add(t)
return [str(ip) for ip in ipset]
示例10: IPSet
# 需要导入模块: from netaddr import IPSet [as 别名]
# 或者: from netaddr.IPSet import add [as 别名]
help='Seconds to wait before timeout, default is 2')
argparser.add_argument('-s', '--shuffle',
action='store_true',
default=False,
dest='shuffle',
help='Shuffle the target list')
args = argparser.parse_args()
# Check if we are running in a pipe and read from STDIN
if not sys.stdin.isatty():
args.targets = sys.stdin.readlines()
# Add target IPs/Networks to a netaddr-IPSet
targetSet = IPSet()
for t in args.targets:
targetSet.add(t)
# Render IPSets to a list
targetlist = list()
for ip in targetSet:
targetlist.append(str(ip))
# Check for shuffle argument
if args.shuffle:
shuffle(targetlist)
# Split list into [maxThreads] smaller batches
targetlist = split_list(targetlist, args.maxThreads)
# Launch threads
for batch in targetlist:
示例11: test_ipset_cidr_fracturing
# 需要导入模块: from netaddr import IPSet [as 别名]
# 或者: from netaddr.IPSet import add [as 别名]
def test_ipset_cidr_fracturing():
s1 = IPSet(['0.0.0.0/0'])
s1.remove('255.255.255.255')
assert s1 == IPSet([
'0.0.0.0/1', '128.0.0.0/2', '192.0.0.0/3',
'224.0.0.0/4', '240.0.0.0/5', '248.0.0.0/6',
'252.0.0.0/7', '254.0.0.0/8', '255.0.0.0/9',
'255.128.0.0/10', '255.192.0.0/11', '255.224.0.0/12',
'255.240.0.0/13', '255.248.0.0/14', '255.252.0.0/15',
'255.254.0.0/16', '255.255.0.0/17', '255.255.128.0/18',
'255.255.192.0/19', '255.255.224.0/20', '255.255.240.0/21',
'255.255.248.0/22', '255.255.252.0/23', '255.255.254.0/24',
'255.255.255.0/25', '255.255.255.128/26', '255.255.255.192/27',
'255.255.255.224/28', '255.255.255.240/29', '255.255.255.248/30',
'255.255.255.252/31', '255.255.255.254/32'])
cidrs = s1.iter_cidrs()
assert len(cidrs) == 32
assert list(cidrs) == [
IPNetwork('0.0.0.0/1'), IPNetwork('128.0.0.0/2'), IPNetwork('192.0.0.0/3'),
IPNetwork('224.0.0.0/4'), IPNetwork('240.0.0.0/5'), IPNetwork('248.0.0.0/6'),
IPNetwork('252.0.0.0/7'), IPNetwork('254.0.0.0/8'), IPNetwork('255.0.0.0/9'),
IPNetwork('255.128.0.0/10'), IPNetwork('255.192.0.0/11'), IPNetwork('255.224.0.0/12'),
IPNetwork('255.240.0.0/13'), IPNetwork('255.248.0.0/14'), IPNetwork('255.252.0.0/15'),
IPNetwork('255.254.0.0/16'), IPNetwork('255.255.0.0/17'), IPNetwork('255.255.128.0/18'),
IPNetwork('255.255.192.0/19'), IPNetwork('255.255.224.0/20'), IPNetwork('255.255.240.0/21'),
IPNetwork('255.255.248.0/22'), IPNetwork('255.255.252.0/23'), IPNetwork('255.255.254.0/24'),
IPNetwork('255.255.255.0/25'), IPNetwork('255.255.255.128/26'), IPNetwork('255.255.255.192/27'),
IPNetwork('255.255.255.224/28'), IPNetwork('255.255.255.240/29'), IPNetwork('255.255.255.248/30'),
IPNetwork('255.255.255.252/31'), IPNetwork('255.255.255.254/32')
]
assert cidrs == cidr_exclude('0.0.0.0/0', '255.255.255.255')
s1.remove('0.0.0.0')
assert s1 == IPSet([
'0.0.0.1/32', '0.0.0.2/31', '0.0.0.4/30',
'0.0.0.8/29', '0.0.0.16/28', '0.0.0.32/27',
'0.0.0.64/26', '0.0.0.128/25', '0.0.1.0/24',
'0.0.2.0/23', '0.0.4.0/22', '0.0.8.0/21',
'0.0.16.0/20', '0.0.32.0/19', '0.0.64.0/18',
'0.0.128.0/17', '0.1.0.0/16', '0.2.0.0/15',
'0.4.0.0/14', '0.8.0.0/13', '0.16.0.0/12',
'0.32.0.0/11', '0.64.0.0/10', '0.128.0.0/9',
'1.0.0.0/8', '2.0.0.0/7', '4.0.0.0/6',
'8.0.0.0/5', '16.0.0.0/4', '32.0.0.0/3',
'64.0.0.0/2', '128.0.0.0/2', '192.0.0.0/3',
'224.0.0.0/4', '240.0.0.0/5', '248.0.0.0/6',
'252.0.0.0/7', '254.0.0.0/8', '255.0.0.0/9',
'255.128.0.0/10', '255.192.0.0/11', '255.224.0.0/12',
'255.240.0.0/13', '255.248.0.0/14', '255.252.0.0/15',
'255.254.0.0/16', '255.255.0.0/17', '255.255.128.0/18',
'255.255.192.0/19', '255.255.224.0/20', '255.255.240.0/21',
'255.255.248.0/22', '255.255.252.0/23', '255.255.254.0/24',
'255.255.255.0/25', '255.255.255.128/26', '255.255.255.192/27',
'255.255.255.224/28', '255.255.255.240/29', '255.255.255.248/30',
'255.255.255.252/31', '255.255.255.254/32',
])
assert len(list(s1.iter_cidrs())) == 62
s1.add('255.255.255.255')
s1.add('0.0.0.0')
assert s1 == IPSet(['0.0.0.0/0'])