Skip to content

Instantly share code, notes, and snippets.

@nboubakr
Created December 20, 2012 11:23
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save nboubakr/4344773 to your computer and use it in GitHub Desktop.
Save nboubakr/4344773 to your computer and use it in GitHub Desktop.
A simple python script converts a Classless Inter-Domain Routing (CIDR)-formatted IP address into an IP range and netmask.
#!/usr/bin/env python
# python subnet.py 200.100.33.65/26
import sys
# Get address string and CIDR string from command line
(addrString, cidrString) = sys.argv[1].split('/')
# Split address into octets and turn CIDR into int
addr = addrString.split('.')
cidr = int(cidrString)
# Initialize the netmask and calculate based on CIDR mask
mask = [0, 0, 0, 0]
for i in range(cidr):
mask[i/8] = mask[i/8] + (1 << (7 - i % 8))
# Initialize net and binary and netmask with addr to get network
net = []
for i in range(4):
net.append(int(addr[i]) & mask[i])
# Duplicate net into broad array, gather host bits, and generate broadcast
broad = list(net)
brange = 32 - cidr
for i in range(brange):
broad[3 - i/8] = broad[3 - i/8] + (1 << (i % 8))
# Print information, mapping integer lists to strings for easy printing
print "Address: " , addrString
print "Netmask: " , ".".join(map(str, mask))
print "Network: " , ".".join(map(str, net))
print "Broadcast: " , ".".join(map(str, broad))
@ZombieNinjaPirate
Copy link

👍
This was really helpful in one of my of scripts. Thanks! :)
👍

@creatorKoo
Copy link

very good!

@RichardBronosky
Copy link

This is awesome! I'm going to suggest that you add line #3:

#!/usr/bin/env python
# python subnet.py 200.100.33.65/26
# from: curl -O https://gist.githubusercontent.com/nboubakr/4344773/raw/subnet.py

That way people can snatch this up and not forget where the got it. I've pointed many people here, because I was able to find the original, because I added that line. But, those people are likely to pass their copy to others directly because they don't have this reference.

@RichardBronosky
Copy link

You may be interested in some of the additions I've made to your script https://gist.github.com/RichardBronosky/1aed6606b1283277e7ff9eaa18097e78/revisions or the way I am using it to analyze my AWS subnets https://github.com/RichardBronosky/AWS_subnets

@Chkcmd
Copy link

Chkcmd commented Sep 18, 2017

Very nice! Ty

mask = [0, 0, 0, 0]
for i in range(cidr):
mask[int(i/8)] = mask[int(i/8)] + (1 << (7 - i % 8))

Copy link

ghost commented Apr 13, 2018

Sweet. ;)

@ebeng
Copy link

ebeng commented Oct 15, 2018

really nice,
what about if I want to add the first address of the Network to be the gateway?

@nitishprajapati5
Copy link

(addrString,cidrString) = sys.argv[1].split('/')
IndexError: list index out of range

@DevBaka
Copy link

DevBaka commented Jun 17, 2019

Thanks for that, it have rescue much time for me! :)
But i use Python3 and no Python2 atm, because i have some little errors.
I have just only addet a / to the calculations and addet the new () syntax for the prints.
old:
mask[i/8] = mask[i/8] + (1 << (7 - i % 8))
print "Address: " , addrString
new:
mask[i//8] = mask[i//8] + (1 << (7 - i % 8))
print("Address: " , addrString)

For Python3 users:
`
import sys

(addrString, cidrString) = sys.argv[1].split('/')

addr = addrString.split('.')
cidr = int(cidrString)

mask = [0, 0, 0, 0]
for i in range(cidr):
mask[i//8] = mask[i//8] + (1 << (7 - i % 8))

net = []
for i in range(4):
net.append(int(addr[i]) & mask[i])

broad = list(net)
brange = 32 - cidr
for i in range(brange):
broad[3 - i//8] = broad[3 - i//8] + (1 << (i % 8))

print("Address: " , addrString)
print("Netmask: " , ".".join(map(str, mask)))
print("Network: " , ".".join(map(str, net)))
print("Broadcast: " , ".".join(map(str, broad)))`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment