Skip to content

Instantly share code, notes, and snippets.

@Sam-Lane
Forked from Lyrain/subnet.py
Created February 27, 2017 13:07
Show Gist options
  • Save Sam-Lane/b70547567c912f183587fa05524a655d to your computer and use it in GitHub Desktop.
Save Sam-Lane/b70547567c912f183587fa05524a655d to your computer and use it in GitHub Desktop.
Script to "cheat" at sub-netting.
#!/usr/local/bin/python3
import sys
import ipaddress as ip
def print_help():
print("Usage: ")
print("main.py address [computers]\n")
print("address: must be an IPv4 network address (including netmask in shorthand e.g. 192.168.211.0/27)")
print("computers: optional - Number of computers on subnet. Will only print first and last host address.")
exit(0)
def main():
if len(sys.argv) < 2:
print_help()
address = sys.argv[1]
if len(sys.argv) == 3:
computers = int(sys.argv[2])
else:
computers = -1
net = ip.IPv4Network(address)
print("Network Address: {0}".format(net.network_address))
print("Broadcast Address: {0}".format(net.broadcast_address))
print("Hostmask: {0}".format(net.hostmask))
if computers < 0:
print("Hosts: ")
for host in list(net.hosts()):
print(host)
elif computers <= len(list(net.hosts())):
hosts = list(net.hosts())
print("First host: {0}".format(hosts[0]))
print("Last host: {0}".format(hosts[computers - 1]))
else:
print("Invalid number of computers for subnet")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment