13
votes

I would like to increment an ip address by a fixed value.

Precisely this is what I am trying to achieve, I have an ip address say, 192.168.0.3 and I want to increment it by 1 which would result in 192.168.0.4 or even by a fixed value, x so that it will increment my ip address by that number. so, I can have a host like 192.168.0.3+x.

I just want to know if any modules already exist for this conversion.

I tried socket.inet_aton and then socket.inet_ntoa, but I don't know how to get that working properly. Need some help or advice on that.

8

8 Answers

15
votes

In Python 3:

>>> import ipaddress
>>> ipaddress.ip_address('192.168.0.4')  # accept both IPv4 and IPv6 addresses
IPv4Address('192.168.0.4')
>>> int(_)
3232235524

>>> ipaddress.ip_address('192.168.0.4') + 256
IPv4Address('192.168.1.4')

In reverse:

>>> ipaddress.ip_address(3232235524)
IPv4Address('192.168.0.4')
>>> str(_)
'192.168.0.4'

>>> ipaddress.ip_address('192.168.0.4') -1
IPv4Address('192.168.0.3')

Python 2/3

You could use struct module to unpack the result of inet_aton() e.g.,

import struct, socket

# x.x.x.x string -> integer
ip2int = lambda ipstr: struct.unpack('!I', socket.inet_aton(ipstr))[0]
print(ip2int("192.168.0.4"))
# -> 3232235524

In reverse:

int2ip = lambda n: socket.inet_ntoa(struct.pack('!I', n))
print(int2ip(3232235525))
# -> 192.168.0.5
8
votes

From python 3.4 onwards:

>>> import ipaddress
>>> a = ipaddress.IPv4Address('192.168.0.1')
>>> a+500
IPv4Address('192.168.1.245')
>>> a = ipaddress.IPv6Address('2001:1900:2254:206a::50:0')
>>> a+200
IPv6Address('2001:1900:2254:206a::50:c8')
>>>
5
votes

There's a module that makes this and other tasks very easy: pip install iptools.

In [1]: import iptools

In [3]: iptools.ip2long('127.0.0.1')
Out[3]: 2130706433

In [4]: p = iptools.ip2long('127.0.0.1') + 1
In [6]: iptools.long2ip(p)
Out[6]: '127.0.0.2'
3
votes

Convert the last part of your IP address into a number, add 1 to it, and call ifconfig.

I think the approach of incrementing the last bit will not scale well as we span across networks. –OP

I thought of mentioning that in my original answer, but didn't, for various reasons. These reasons are as follows:

  • I thought it is unlikely you would need to do this, and could not guess why you'd want to.
  • Even if you did need to do this, you could just parse the second-to-last number.
  • This is only valid for those bits where the netmask is 0.
  • You also have to worry about "special" reserved IP ranges, such as 192.168.etc.etc. Also hex doublets with 0 and possibly ff/255 have special meaning. There are different rules in IPv6.
2
votes

It might be quicker to just use simple addition and iteration, something like:

ip = [192,168,0,0]
ip_dict = {}
ip_list = []

for i in range(100):
    new_ip = ip[3]+=1
    ip_dict[i]=new_ip
    ip_list.append(new_ip)
1
votes

EDIT: This is buggy and shouldn't be used as is.

I would use ipaddr for this

>>> import ipaddr
>>> a = ipaddr.IPAddress('192.168.0.3')
>>> a
IPv4Address('192.168.0.3')
>>> a + 1
IPv4Address('192.168.0.4')
1
votes

The library ipcalc has routines to make math on ip addresses fairly easy. As an example an iterator for an address range can be done like:

Code:

import ipcalc
network = ipcalc.Network('10.1.0.0/16')
host_first = network.host_first()
addresses = (host_first + i for i in range(network.size()-2))

Test Code:

print(next(addresses))
print(next(addresses))
print(next(addresses))
print(max(list(addresses)))

Results:

10.1.0.1
10.1.0.2
10.1.0.3
10.1.255.254
0
votes
def FunIncrementIp(IPADDRESS,IPADDRESSES):
#import the ipaddress module and also check whether it is an ipv6 or ipv4
import ipaddress
if ':' in IPADDRESS:
    IPADDRESSMOD = ipaddress.IPv6Address(IPADDRESS)
    print ('this is ipv6 address')
else:
    IPADDRESSMOD = ipaddress.IPv4Address(IPADDRESS)
    print ('this is ipv4 address')
IPADDRESSES = int(c)
IPADDRESSES = IPADDRESSMOD+IPADDRESSES
while IPADDRESSMOD < IPADDRESSES:
    IPADDRESSMOD += 1
    print(IPADDRESSMOD)

This should do it.

FunIncrementIp('1.1.1.1','10')

This will increment your ipv4 addresses to 10 more

FunIncrementIp('2001:db8:0:1:1:1:1:1','10')

This will increment your ipv6 addresses to 10 more This will also tell auto detect the type of ip address so that you don't have to have separate script for ipv4 & ipv6.