Python Forum
code to increment ip address
Thread Rating:
  • 2 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
code to increment ip address
#1
Hello,

The below code is used to increment the ip address by a given step for a particular class.
The ip address should not have 0 or 255 in it.
#IP increment function

ip = raw_input("Enter IP address: ")
step = int(input("Enter the step: "))
class_sel = raw_input("Enter the class: ")

def nextIP(ip, step, class_sel):
    class_sel.lower()
    octetList =  ip.split('.')
    octetList[3] = int(octetList[3])
    octetList[2] = int(octetList[2])
    octetList[1] = int(octetList[1])
    octetList[0] = int(octetList[0])
    iplist = [0,0,0,0]
    j = ord(class_sel) - ord('a')
    
    iplist[j] = octetList[j] + step
    while j >= 0:
        if iplist[j] > 254:
            octetList[j] = iplist[j] % 255
            if octetList[j] == 0:
                octetList[j] = 1
            j = j - 1
            if j >= 0:
                octetList[j] = octetList[j] + (iplist[j+1] / 255)
        else:
            j = j - 1
        
    print '{}.{}.{}.{}'.format(octetList[0],octetList[1],octetList[2],octetList[3])
    return '{}.{}.{}.{}'.format(octetList[0],octetList[1],octetList[2],octetList[3])

nextIP(ip, step, class_sel)
The code is not working for boundary conditions. Like
ip: 254.254.254.254
step: 1
class: d
o/p: 254.254.254.1

expected o/p: 1.1.1.1
Could anyone please help how to make the code work for boundary conditions also.

Thank you in advance.
Reply
#2
Your code is way too convoluted and commits the capital sin of changing the input values. Normally:
  • copy the input IP to the output IP
  • add the step to the relevant byte of the IP
  • if the result is greater than 254, replace by N-254 and increment the next byte (unless you are already at the first)
  • inspect the next byte
Also,
  • str.lower() doesn't lowercase str, it produces a new string, so it must be assigned to a variable.
  • Use comprehensions.
  • You can use function(*array) to pass elements of an array to a function as successive arguments (in other words, function(*array) is the same as function(array[0],array[1],...,array[N]))
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#3
Here's an ip unpacker from stackoverflow:
ip2int = lambda ipstr: struct.unpack('!I', socket.inet_aton(ipstr))[0]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  help to increment a third list hermine 7 1,272 Nov-29-2022, 04:19 PM
Last Post: perfringo
  mysql id auto increment not working tantony 10 2,314 Oct-18-2022, 11:43 PM
Last Post: Pedroski55
  Character Increment AnokhiRaaz 1 2,462 Apr-22-2021, 04:29 AM
Last Post: buran
  Increment text files output and limit contains Kaminsky 1 3,138 Jan-30-2021, 06:58 PM
Last Post: bowlofred
  Increment formula Kristenl2784 4 2,821 Jul-20-2020, 10:14 PM
Last Post: Kristenl2784
  [openpyxl] Increment cells being pasted into Template Kristenl2784 4 3,507 Jul-16-2020, 10:00 PM
Last Post: Kristenl2784
  How can I increment a List item with in a "for in" msteffes 4 3,492 Aug-14-2019, 08:55 AM
Last Post: DeaD_EyE
  How define iteration interval increment SriMekala 5 4,254 Jun-01-2019, 01:06 PM
Last Post: ichabod801
  get the full address from a zip (postal) code mfran2002 4 3,053 Mar-30-2019, 06:57 PM
Last Post: micseydel
  SQlite3 quickly increment INT value? jmair 1 2,404 Mar-04-2019, 08:03 PM
Last Post: stranac

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020