Skip to content

Instantly share code, notes, and snippets.

@Dapacruz
Created February 8, 2021 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dapacruz/b1ee302a2f235c7a96438433791b5494 to your computer and use it in GitHub Desktop.
Save Dapacruz/b1ee302a2f235c7a96438433791b5494 to your computer and use it in GitHub Desktop.
Generate a list of IP addresses
#!/usr/bin/env python3
'''Generate a list of IP addresses
generate-ip-addresses.py
Author: David Cruz (davidcruz72@gmail.com)
Python version >= 3.6
Required Python packages:
none
Features:
Takes a space separated list of IP subnets and generates a list of IP addresses
'''
import argparse
from ipaddress import IPv4Interface
import platform
import re
import sys
os = platform.system()
system = {
'Windows': {
'ext': 'exe',
'clip': 'clip.exe',
'prompt': r'C:\PS>',
'slash': '\\'
},
'Linux': {
'ext': 'py',
'clip': 'xclip',
'prompt': '$',
'slash': '/'
},
'Darwin': {
'ext': 'py',
'clip': 'pbcopy',
'prompt': '$',
'slash': '/'
}
}
help_examples = f"""
examples:
-------------------------- EXAMPLE 1 --------------------------
{system[os]["prompt"]} echo "192.168.1.1/24
192.168.2.22 255.255.255.192" | .{system[os]["slash"]}generate-ip-addresses.{system[os]["ext"]}
-------------------------- EXAMPLE 2 --------------------------
{system[os]["prompt"]} .{system[os]["slash"]}generate-ip-addresses.{system[os]["ext"]} 192.168.1.64/24 "192.168.3.33 255.255.255.0" | {system[os]["clip"]}
-------------------------- EXAMPLE 3 --------------------------
{system[os]["prompt"]} .{system[os]["slash"]}generate-ip-addresses.{system[os]["ext"]} 192.168.200.69/25 "10.1.3.33 255.255.255.0" > ips.txt
"""
def main():
parser = argparse.ArgumentParser(
description='Returns a list of IP addresses',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=help_examples
)
parser.add_argument('addresses', type=str, nargs='*', help='Space separated list of IP addresses/networks')
args = parser.parse_args()
# Receive IP addresses from stdin
if not sys.stdin.isatty():
args.addresses = [i.strip() for i in sys.stdin]
# Remove empty strings (Windows PowerShell Select-String cmdlet issue)
args.addresses = list(filter(None, args.addresses))
elif not args.addresses:
parser.print_help()
subnets = [IPv4Interface(re.sub(r'\s+/*', '/', ip.strip())).network for ip in args.addresses]
for subnet in subnets:
for ip in subnet.hosts():
print(ip)
if __name__ == '__main__':
main()
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(['generate-ip-addresses.py'],
pathex=['C:\\Users\\[USER]\\Repos\\python\\Generate IP Addresses'],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name='generate-ip-addresses',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=True )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment