-def generate_ip_range(args):
- """
- Generate IP range in string format for given CIDR.
- This function works for both IPv4 and IPv6.
-
- args is expected to contain the following members:
- CIDR: any valid CIDR representation.
- start_position: starting index, default to first address in subnet (1)
- end_position: ending index, default to last address in subnet (-1)
-
- Returns IP range in string format. A single IP is returned if start and
- end IPs are identical.
- """
- cidr = ipaddress.ip_network(args.CIDR)
- (start_index, end_index) = (args.start_position, args.end_position)
- if cidr[start_index] == cidr[end_index]:
- return str(cidr[start_index])
- else:
- return ','.join(sorted([str(cidr[start_index]), str(cidr[end_index])]))
-
-