user3717431
user3717431

Reputation: 103

How to compare IPV6 addresses in python

I am using python --version 2.6 and Solaris 10 as a OS

These are some valid IPV6 addresses. I have these Ip addresses as string.

I have to compare all the ip and check that whether they are representing same ipv6 address (represented as different notation) or not. I have search a lot,and find some classes available in python 3 like ipaddress, but I can not use that as I have python 2.6 version.Is there any class available in python 2.6?

Thanks

Upvotes: 1

Views: 3206

Answers (5)

Amir
Amir

Reputation: 6186

You can use socket.

import socket
ip1 = "2001:cdba:0000:0000:0000:0000:3257:9652"
ip2 = "2001:cdba::3257:9652"
if socket.inet_pton(socket.AF_INET6, ip1) == socket.inet_pton(socket.AF_INET6, ip2):
     print "match"

Upvotes: 4

erlc
erlc

Reputation: 680

I would just use some string manipulations such as

def pad(addr):
    groups = addr.split(':')
    empty = None
    for i,g in enumerate(groups):
        if empty is None and len(g) == 0: empty = i
        elif len(g) < 4:
            groups[i] = '0'*(4-len(g))+g


    if empty is not None:
        groups=groups[:empty] + ['0000',]*(8-len(groups)+1) + groups[empty+1:]

    return ':'.join(groups)

if pad('2001:cdba:0000:0000:0000:0000:3257:9652') == pad('2001:cdba::3257:9652'):
    print('Same!')

Upvotes: 1

Ankur Ankan
Ankur Ankan

Reputation: 3066

You can use regex.sub:

st_arr = ['2001:cdba:0000:0000:0000:0000:3257:9652', '2001:cdba:0:0:0:0:3257:9652', '2001:cdba::3257:9652']

new_string_arr = [re.sub(r'[:]+', ':', re.sub(':[0]+', ':', st))) for st in st_arr]

new_string_arr
Out[63]: ['2001:cdba:3257:9652', '2001:cdba:3257:9652', '2001:cdba:3257:9652']

new_string_arr[0] == new_string_arr[1] == new_string_arr[2]
Out[64]: True

First the regex replaces the pattern :[0]+ with : and after this since there could be multiple colons, I replaced the pattern [:]+ with :.

Upvotes: 2

Sanjay
Sanjay

Reputation: 1108

Before comparing ipaddress you can check whether they are valid ipv6 format or not, by using socket lib:

import socket

def is_valid_ipv6_address(address):
try:
    socket.inet_pton(socket.AF_INET6, address)
except socket.error:  # not a valid address
    return False
return True

after validatation now you can use regular expression to compare whether they are equal or not iterate for all the ipv6 addresses,:

if(is_valid_ipv6_address(ipv6Address)):
    new_ipv6Address=re.sub(r'::[0]', '::',re.sub(r'[::]+', '::', re.sub(':[0]+:', '::', re.sub(':[0]+:', '::', ipv6Address))))

This will work for aaaa:bbbb:cccc:dddd:eeee:ffff:1111:: and aaaa:bbbb:cccc:dddd:eeee:ffff:1111:0 format as well.

Upvotes: 0

Padraic Cunningham
Padraic Cunningham

Reputation: 180401

Install ipaddr

import ipaddr

s1="2001:cdba:0000:0000:0000:0000:3257:9652"
ips=["2001:cdba:0:0:0:0:3257:9652","2001:cdba::3257:9652","2001:cdba:0000:0000:0000:0000:3257:9651"]

results=[]


for i in ips:
    print ipaddr.IPv6Address(i).exploded # re adds leading zeros etc..
    if ipaddr.IPv6Address(i).exploded==s1:
        results.append(i)
print results

2001:cdba:0000:0000:0000:0000:3257:9652  
2001:cdba:0000:0000:0000:0000:3257:9652
2001:cdba:0000:0000:0000:0000:3257:9651
['2001:cdba:0:0:0:0:3257:9652', '2001:cdba::3257:9652']

Upvotes: 0

Related Questions