Created
January 27, 2022 11:41
-
-
Save jfmaes/9b50dd9cd656c8d905fa619868b578de to your computer and use it in GitHub Desktop.
explode ips
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
from ipaddress import IPv4Network | |
import os | |
def banner(): | |
print(r""" | |
c=====e | |
H | |
____________ _,,_H____ | |
(__((__((___() //| | | |
(__((__((___()()_____________________________________// |IPaddr | | |
(__((__((___()()()------------------------------------' |_______| | |
""") | |
def explode(ipaddress): | |
exploded_ips = [] | |
if os.path.exists(ipaddress): | |
file = open(ipaddress, 'r') | |
lines = file.readlines() | |
for line in lines: | |
ip_network_hosts = IPv4Network(line.strip()).hosts() | |
for host in ip_network_hosts: | |
exploded_ips.append(str(host)) | |
return exploded_ips | |
else: | |
raise Exception(ipaddress + " is not a valid file.") | |
def exclude(listOfIps: list, ipaddress: str): | |
if not ipaddress: | |
return listOfIps | |
else: | |
ipaddresses = ipaddress.split(",") | |
for ip in ipaddresses: | |
if ip in listOfIps: | |
listOfIps.remove(ip) | |
return listOfIps | |
def print_to_console(switch, explodedlist): | |
if not switch: | |
pass | |
else: | |
for ip in explodedlist: | |
print(ip) | |
def print_to_file(filename, explodedlist): | |
if not filename: | |
pass | |
else: | |
outfile = open(filename, "w") | |
for ip in explodedlist: | |
outfile.write(str(ip) + "\n") | |
print("All done, enjoy!") | |
def main(): | |
banner() | |
parser = argparse.ArgumentParser(description='explode ip addresses in file.') | |
parser.add_argument('-f', '--file', required=True, help="the file with ip address ranges to explode") | |
parser.add_argument('-e', '--exclude', required=False, help="any exclusions delimited with ','") | |
parser.add_argument('-v', '--verbose', type=bool, required=False, help="prints the addresses to console") | |
parser.add_argument('-o', '--out-file', required=False, help="saves exploded addresses to file") | |
args = parser.parse_args() | |
if not args.out_file and not args.verbose: | |
print("you did not specify an outfile, assuming verbose") | |
args.verbose = True | |
exploded = explode(args.file) | |
exploded = exclude(exploded, args.exclude) | |
print_to_console(args.verbose, exploded) | |
print_to_file(args.out_file, exploded) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment