This forum is in archive mode. You will not be able to post new content.

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - benzies

Pages: [1]
1
Scripting Languages / Re: [Python] Subnet Calculator
« 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.


2
Scripting Languages / Re: [Python] Subnet Calculator
« 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.

3
Scripting Languages / Re: [Python] Subnet Calculator
« 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)



Pages: [1]


Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.