Author Topic: [Python] Subnet Calculator  (Read 4282 times)

0 Members and 1 Guest are viewing this topic.

Offline flowjob

  • Knight
  • **
  • Posts: 327
  • Cookies: 46
  • Pastafarian
    • View Profile
[Python] Subnet Calculator
« on: July 29, 2014, 10:31:55 pm »
NetCalc Subnet Calculator

I wrote that commandline scripts some months ago for school, as I was to lazy to calculate all that stuff by myself again and again...
It calculates the most important stuff like
  • netmask/cidr
  • maschine part (= inversed netmask)
  • max-number of possible hosts
  • broadcast addresse
  • default gateway

Usage:
Either pass an IP and a netmask or an IP and CIDR as arguments
Example:
Code: [Select]
python2 netcalc.py 10.0.0.3 255.255.255.0
python2 netcalc.py 10.0.0.3/24

Source Code:
Code: (python) [Select]
#!/usr/bin/env python2

import sys

def calcIPValue(ipaddr):
"""
Calculates the binary
value of the ip addresse
"""
ipaddr = ipaddr.split('.')
value = 0
for i in range(len(ipaddr)):
value = value | (int(ipaddr[i]) << ( 8*(3-i) ))
return value

def calcIPNotation(value):
"""
Calculates the notation
of the ip addresse given its value
"""
notat = []
for i in range(4):
shift = 255 << ( 8*(3-i) )
part = value & shift
part = part >> ( 8*(3-i) )
notat.append(str(part))
notat = '.'.join(notat)
return notat

def calcSubnet(cidr):
"""
Calculates the Subnet
based on the CIDR
"""
subn = 4294967295 << (32-cidr) # 4294967295 = all bits set to 1
subn = subn % 4294967296 # round it back to be 4 bytes
subn = calcIPNotation(subn)
return subn

def calcCIDR(subnet):
"""
Calculates the CIDR
based on the SUbnet
"""
cidr = 0
subnet = calcIPValue(subnet)
while subnet != 0:
subnet = subnet << 1
subnet = subnet % 4294967296
cidr += 1
return cidr

def calcNetpart(ipaddr,subnet):
ipaddr = calcIPValue(ipaddr)
subnet = calcIPValue(subnet)
netpart = ipaddr & subnet
netpart = calcIPNotation(netpart)
return netpart

def calcMacpart(subnet):
macpart = ~calcIPValue(subnet)
macpart = calcIPNotation(macpart)
return macpart

def calcBroadcast(ipaddr,subnet):
netpart = calcNetpart(ipaddr,subnet)
macpart = calcMacpart(subnet)
netpart = calcIPValue(netpart)
macpart = calcIPValue(macpart)
broadcast = netpart | macpart
broadcast = calcIPNotation(broadcast)
return broadcast

def calcDefaultGate(ipaddr,subnet):
defaultgw = calcNetpart(ipaddr,subnet)
defaultgw = calcIPValue(defaultgw) + 1
defaultgw = calcIPNotation(defaultgw)
return defaultgw

def calcHostNum(subnet):
macpart = calcMacpart(subnet)
hostnum = calcIPValue(macpart) - 1
return hostnum


def main():
if (len(sys.argv) == 2 and '/' in sys.argv[1]): # IPAdresse/CIDR
ipaddr = sys.argv[1].split('/')[0]
cidr = sys.argv[1].split('/')[1]
subnet = calcSubnet(int(cidr))
elif (len(sys.argv) == 3): # IPAdresse Subnet
ipaddr = sys.argv[1]
subnet = sys.argv[2]
cidr = calcCIDR(subnet)
else:
args = ' '.join(sys.argv[1:])
sys.exit('"' + args + '" is no valid combination of an IP and a Subnet/CIDR!')

print '==============================='
print 'IPv4:             ' + ipaddr
print 'Netmask:          ' + subnet
print 'CIDR:             ' + str(cidr)
macpart = calcMacpart(subnet)
print 'Inversed Netmask: ' + macpart
hostnum = calcHostNum(subnet)
print 'Hosts:            ' + str(hostnum)
netpart = calcNetpart(ipaddr,subnet)
print 'Network:          ' + netpart
broadcast = calcBroadcast(ipaddr,subnet)
print 'Broadcast:        ' + broadcast
defaultgw = calcDefaultGate(ipaddr,subnet)
print 'Default Gateway:  ' + defaultgw
print '==============================='

if __name__ == '__main__':
main()

I also ported that script to C++ >> netcalc.cpp
Quote
<phil> I'm gonna DDOS the washing machine with clothes packets.
<deviant_sheep> dont use too much soap or youll cause a bubble overflow

Offline benzies

  • NULL
  • Posts: 3
  • Cookies: -3
    • View Profile
Re: [Python] Subnet Calculator
« Reply #1 on: August 12, 2015, 07:30:30 am »
Hey there, I've modified your calcSubnet() function to use the proper decimal representation for "all bits set to 1".  I didn't like the use of the number you provided, as I couldn't find a reason (except for, maybe it just worked that way) to use '4294967295'.


Maybe you can provide some insight into why your code was written the way it was?


I feel like mine is still a little sloppy, but I'm still learning quite a lot, so I'm keen to see what you think  :)



Code: (Python) [Select]

cidr = 16


def calcSubnet(cidr):
        #Cidr into Subnet
        Bits2Zero = 32-cidr             #How many bits we'll need to zero
        Bits2Decimal = 2**Bits2Zero     #The calculation of bits into a decimal
        AllBits = 2**32                 #All bits in a IPv4 Address
        #Convert it
        Subnet = AllBits - Bits2Decimal #Decimal Representation of the subnet address
        Subnet = calcIPNotation(Subnet) #Pass the decimal subnet into the function
        print Subnet                    #Print the subnet adress




calcSubnet(cidr)




And I just made it better :)


Code: (Python) [Select]

cidr = 29


def calcSubnet(cidr):


        DecimalSubnet = ( (2**32) - (2**(32-cidr)) )
        Subnet = calcIPNotation(DecimalSubnet)
        return Subnet
        InverseSubnet = ~DecimalSubnet
        Inverse = calcIPNotation(InverseSubnet)
        return Inverse


calcSubnet(cidr)


« Last Edit: August 12, 2015, 12:04:21 pm by benzies »

Offline kenjoe41

  • Symphorophiliac Programmer
  • Administrator
  • Baron
  • *
  • Posts: 990
  • Cookies: 224
    • View Profile
Re: [Python] Subnet Calculator
« Reply #2 on: August 13, 2015, 02:22:53 am »
I will just take a cookie for this stupid nursery school error. It typically shows you didn't test your code and you just put it here and trust me, i have received enough beating for not testing my code that i know better now.

I am talking about the two return statements that make no sense at all what their significance is. Style up and clean it up.
If you can't explain it to a 6 year old, you don't understand it yourself.
http://upload.alpha.evilzone.org/index.php?page=img&img=GwkGGneGR7Pl222zVGmNTjerkhkYNGtBuiYXkpyNv4ScOAWQu0-Y8[<NgGw/hsq]>EvbQrOrousk[/img]

Offline benzies

  • NULL
  • Posts: 3
  • Cookies: -3
    • View Profile
Re: [Python] Subnet Calculator
« Reply #3 on: August 13, 2015, 08:25:54 am »
Erm, you could just replace 'return' with 'print' and you'll see the result.  You'll need the full code flowjob has provided so that it works.


Adding 'print' under each variable will also show the value it provides before it's converted into a subnet address.


Or just add this function;




Code: (Python) [Select]

def calcIPNotation(value):
        """
        Calculates the notation
        of the ip addresse given its value
        """
        notat = []
        for i in range(4):
                shift = 255 << ( 8*(3-i) )
                part = value & shift
                part = part >> ( 8*(3-i) )
                notat.append(str(part))
        notat = '.'.join(notat)
        return notat






~sourced from flowjob's code, to the script you're shoving together.

Offline benzies

  • NULL
  • Posts: 3
  • Cookies: -3
    • View Profile
Re: [Python] Subnet Calculator
« Reply #4 on: August 14, 2015, 01:12:10 am »


Code: (python) [Select]
#!/usr/bin/env python2
def calcSubnet(cidr):
   """
   Calculates the Subnet
   based on the CIDR
   """
   subn = 4294967295 << (32-cidr)   # 4294967295 = all bits set to 1
   subn = subn % 4294967296   # round it back to be 4 bytes
   subn = calcIPNotation(subn)
   return subn



As I work through this more, I think I understand how you found '4294967295 '.  Although 2^32 = 4294967296, is no the same as this; ( (((2^(8*3))*255)+(((2^(8*2))*255)+(((2^(8*1))*255)+(((2^(8*0))*255))))) ) = 4294967295, which is "255.255.255.255" technically the same value.  Still researching as to why that is, but at least that explains what's going on.


Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: [Python] Subnet Calculator
« Reply #5 on: August 14, 2015, 08:22:49 am »

As I work through this more, I think I understand how you found '4294967295 '.  Although 2^32 = 4294967296, is no the same as this; ( (((2^(8*3))*255)+(((2^(8*2))*255)+(((2^(8*1))*255)+(((2^(8*0))*255))))) ) = 4294967295, which is "255.255.255.255" technically the same value.  Still researching as to why that is, but at least that explains what's going on.



2 ^ 32 in binary:
1 0000 0000 0000 0000 0000 0000 0000 0000
whereas you want to have 2 ^ 32 - 1:
0 1111 1111 1111 1111 1111 1111 1111 1111

Offline Aurora

  • /dev/null
  • *
  • Posts: 18
  • Cookies: -31
  • Reverse Engineer
    • View Profile
Re: [Python] Subnet Calculator
« Reply #6 on: August 15, 2015, 07:27:12 pm »
This is nice, thanks for sharing. I'll play around with this later.

Offline kenjoe41

  • Symphorophiliac Programmer
  • Administrator
  • Baron
  • *
  • Posts: 990
  • Cookies: 224
    • View Profile
Re: [Python] Subnet Calculator
« Reply #7 on: August 18, 2015, 02:41:37 am »
Erm, you could just replace 'return' with 'print' and you'll see the result.  You'll need the full code flowjob has provided so that it works.


Adding 'print' under each variable will also show the value it provides before it's converted into a subnet address.


Or just add this function;




Code: (Python) [Select]

def calcIPNotation(value):
        """
        Calculates the notation
        of the ip addresse given its value
        """
        notat = []
        for i in range(4):
                shift = 255 << ( 8*(3-i) )
                part = value & shift
                part = part >> ( 8*(3-i) )
                notat.append(str(part))
        notat = '.'.join(notat)
        return notat






~sourced from flowjob's code, to the script you're shoving together.
I was waiting fot the part where you admit you made a stupid mistake because mind you, return isn't the same as print. I don't have enough time now to explain but google your SHIT.
If you can't explain it to a 6 year old, you don't understand it yourself.
http://upload.alpha.evilzone.org/index.php?page=img&img=GwkGGneGR7Pl222zVGmNTjerkhkYNGtBuiYXkpyNv4ScOAWQu0-Y8[<NgGw/hsq]>EvbQrOrousk[/img]