NetExec/cme/parsers/ip.py

24 lines
809 B
Python
Raw Normal View History

from ipaddress import ip_address, ip_network, summarize_address_range
2016-05-16 23:48:31 +00:00
def parse_targets(target):
try:
if '-' in target:
start_ip, end_ip = target.split('-')
2016-05-16 23:48:31 +00:00
try:
end_ip = ip_address(end_ip)
except ValueError:
first_three_octets = start_ip.split(".")[:-1]
first_three_octets.append(end_ip)
end_ip = ip_address(
".".join(first_three_octets)
)
2016-05-16 23:48:31 +00:00
for ip_range in summarize_address_range(ip_address(start_ip), end_ip):
for ip in ip_range:
yield str(ip)
else:
for ip in ip_network(target, strict=False):
yield str(ip)
except ValueError:
yield str(target)