NetExec/cme/parsers/ip.py

30 lines
1.0 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
2021-09-21 09:29:07 +00:00
from ipaddress import ip_address, ip_network, summarize_address_range, ip_interface
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:
2021-09-21 09:29:07 +00:00
if ip_interface(target).ip.version == 6 and ip_address(target).is_link_local:
yield str(target)
else:
for ip in ip_network(target, strict=False):
2021-09-21 09:29:07 +00:00
yield str(ip)
except ValueError as e:
yield str(target)