6 Replies - 653 Views - Last Post: 12 January 2012 - 06:51 PM Rate Topic: -----

Topic Sponsor:

#1 blank_program  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 244
  • Joined: 22-July 09

Calculating network addresses

Posted 07 January 2012 - 12:00 PM

I am making a program that will take an IP, or generate one, and subnet mask and output relevant information. Right now I have the generation functions working fine but now I am at a standstill getting the adresses. I know ipcalc can do this for me but I would much rather write it myself if possible.

I am currently lost with where to go from here. I assume it won't matter that I am using the decimal numbers for each octet int he IPv4 address but I could be wrong. When I do the calculations by hand on paper I do the math in binary but was unsure how to translate that logic into this application.

I will eventually go back and edit this but for now this is the part I am stuck at. What would you all recommend, besides using the ipcalc module, that will help me reach my goal?

Here is the function I have so far to get the addresses:
#determine netid, first host, last host, and broadcast addresses
def netaddrs(genaddr):
    netinfo.netaddr

    noctets = []
    octets = genaddr.split('/')[0].split('.')
    netbits = int(genaddr.split('/')[1])

    #set nclass equal the octet number network bits end to set network class
    nclass = 0
    
    if 8 <= netbits < 16:
        nclass = 2
    elif 16 <= netbits < 24:
        nclass = 3
    elif 24 <= netbits:
        nclass = 4

    n = 255 - (mp(2, (8 * nclass) - netbits))
    if nclass == 2:

    elif nclass == 3:

    elif nclass == 4:



Is This A Good Question/Topic? 0
  • +

Replies To: Calculating network addresses

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 474
  • View blog
  • Posts: 1,248
  • Joined: 23-December 08

Re: Calculating network addresses

Posted 08 January 2012 - 09:26 AM

Any binary math you do by hand shouldn't be terribly difficult to translate into python. Mind walking us through an example?
Was This Post Helpful? 0
  • +
  • -

#3 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 443
  • View blog
  • Posts: 792
  • Joined: 08-June 10

Re: Calculating network addresses

Posted 09 January 2012 - 07:20 AM

Or would you at least post an example of what we should pass to your function, and what it should return?
Was This Post Helpful? 0
  • +
  • -

#4 blank_program  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 244
  • Joined: 22-July 09

Re: Calculating network addresses

Posted 09 January 2012 - 05:10 PM

View Postatraub, on 08 January 2012 - 11:26 AM, said:

Any binary math you do by hand shouldn't be terribly difficult to translate into python. Mind walking us through an example?

This I don't think would work so well as it is more a matter of finding when the bits start being 0 as opposed to being 1 for the network bits. I guess I am thinking of it in a more difficult manner than expected for the math I mentioned.

View PostMotoma, on 09 January 2012 - 09:20 AM, said:

Or would you at least post an example of what we should pass to your function, and what it should return?

An easy simple example would be you pass in say 192.168.1.4/24, the program should output 192.168.1.0, 192.168.1.1, 192.168.1.254, 192.168.1.255. These addresses in order are the network address, first host in subnet, last host in subnet, and broadcast address. This of course is a simple example but it should work on any subnet matching the /xx subnet that is passed in.

This post has been edited by blank_program: 09 January 2012 - 05:14 PM

Was This Post Helpful? 0
  • +
  • -

#5 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 443
  • View blog
  • Posts: 792
  • Joined: 08-June 10

Re: Calculating network addresses

Posted 11 January 2012 - 08:15 AM

Binary masking is quite easy in Python:

>>> address = '192.168.1.4/24'
>>> ip, mask = address.split('/')
>>> ip
'192.168.1.4'
>>> mask
'24'
>>> octets = ip.split('.')
>>> addr = 0
>>> for octet in octets:
...     addr = addr * 256 + int(octet)
... 
>>> addr
3232235780
>>> print("%x" % addr)
c0a80104
>>> addr & 0xFFFFFF00
3232235776
>>> addr & 0b11111111111111111111111100000000
3232235776



This should get you on your way, let me know if you have any questions.
Was This Post Helpful? 0
  • +
  • -

#6 blank_program  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 244
  • Joined: 22-July 09

Re: Calculating network addresses

Posted 12 January 2012 - 06:16 PM

I ended up getting this worked out earlier today in a slightly different, probably overly complex way.
Here is what I ended up doing:
def gennmask(genaddr):
    #netmask octets
    noctets = []

    #split given info into relevant parts
    octets = genaddr.split('/')[0].split('.')
    netbits = int(genaddr.split('/')[1])

    #using netbits generate a binary netmask as a single string
    netmask = ''
    while len(netmask) < 32:
        if len(netmask) < netbits:
            netmask = netmask + '1'
        else:
            netmask = netmask + '0'

    #build noctets array by splitting netmask binary string
    noctets.append(netmask[0:8])
    noctets.append(netmask[8:16])
    noctets.append(netmask[16:24])
    noctets.append(netmask[24:32])

    #find decimal values for binary parts of netmask
    sub = ''
    for i in range(4):
        n = noctets[i].count('1')
        
        if n != 0:
            noctets[i] = int(mp(2, n))
        else:
            noctets[i] = 0
            
        if noctets[i] == 256:
            noctets[i] = int(noctets[i] - 1)
        elif noctets[i] > 0:
            noctets[i] = int(256 - mp(2, 8 - n))

    #create printable string from decimal netmask values
    for i in range(3):
        sub = sub + str(noctets[i]) + '.'
    sub = sub + str(noctets[3])

    netinfo.netmask = sub

    #after setting netmask run netaddrs function to get remaining addresses
    netaddrs()



However I have an issue with netaddrs(). Below is my class and the netaddrs() function.
class netinfo:
    addr = ''
    netmask = ''
    netaddr = ''
    fhost = ''
    lhost = ''
    bcast = ''

    hostcount = 0
    netcount = 0

def netaddrs():
    noctets = netinfo.netmask.split('.')
    print(netinfo.addr)
    print(noctets)



The line for noctets works perfect but the netinfo.addr doesn't. However I know the variable is set as below that code I print it and it works properly so I am not sure what I am doing wrong.

I think I am on the right track though I probably did things incorrectly to get the results I want since I am still learning Python. Hopefully when this works correctly and is complete I can go back and make the code more correct.

Thanks for the help.
Was This Post Helpful? 0
  • +
  • -

#7 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 443
  • View blog
  • Posts: 792
  • Joined: 08-June 10

Re: Calculating network addresses

Posted 12 January 2012 - 06:51 PM

Could you post enough code that I could run this and see the error occur? Some stripped down representative code that demonstrates the error will allow me to debug it for you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1