473,287 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

help parsing ipv6 addresses and subnets

Hello list,

I would like to parse IPv6 addresses and subnet using re module in
python. I am able to either parse the ipv6 address or ipv6 network but
not both using single line. any help appreciated. BTW is there a
metacharacter for hex digits.

Thanks
Prabhu
-

---------------------------------------------------------------------
#!/usr/bin/env python2.5
# $Id: $

import re, os, Queue, sys
from optparse import OptionParser

# for debug purposes only
import pdb

argc = len(sys.argv)
(dirname, program) = os.path.split(sys.argv[0])

def error(Message):
if Message:
global program
print "%s: %s" %(program, Message)
sys.exit(1)

def cisco_parse(FileName):
if FileName:
fh = None
if os.path.exists(FileName):
try:
fh = open(FileName, "r")
except IOError, message:
error(message)
else:
count = 0
flag = True
while flag:
try:
lines = fh.next()
except StopIteration:
flag = False
else:
line = lines.strip()
rehex = "[A-Fa-f0-9]"
# to parse ipv6 address
format = \
"((%s{1,4}:?:){1,7}%s{1,4})" %(rehex, rehex)
# to parse ipv6 subnet
# format = \
# "((%s{1,4}:?:){1,7}%s{1,4}(?=(::/\d{1,3})))"
%(rehex, rehex)
reip6 = re.compile(format)
match = reip6.search(line)
if match is not None:
tupleLen = len(match.groups())
if tupleLen == 2:
print count, match.groups()[0]
elif tupleLen == 3:
print count, match.groups()[0] + match.groups()[2]
count += 1

fh.close()
fh = None

def ParseCmdLine():
parser = OptionParser(usage="%prog [options]", version="%prog 1.0")
parser.add_option("-f", "--filename",
help="cisco config to read", dest="file")

(options, args) = parser.parse_args()

fileName = None
if options.file:
fileName = options.file

if fileName:
cisco_parse(fileName)

if __name__ == "__main__":
if argc <= 1:
error("too few arguments, use -h or --help to view all options")
else:
ParseCmdLine()
Nov 8 '07 #1
2 3698
* Prabhu Gurumurthy (Wed, 07 Nov 2007 22:34:14 -0800)
I would like to parse IPv6 addresses and subnet using re module in
python.
Just don't: http://pypi.python.org/pypi?%3Aaction=search&term=ipv6
&submit=search
Nov 8 '07 #2
On Nov 8, 3:11 am, Thorsten Kampe <thors...@thorstenkampe.dewrote:
* Prabhu Gurumurthy (Wed, 07 Nov 2007 22:34:14 -0800)
I would like to parse IPv6 addresses and subnet using re module in
python.

Just don't:http://pypi.python.org/pypi?%3Aaction=search&term=ipv6
&submit=search
And even if you do end up doing the IPv6 parsing yourself (which you
shouldn't), regular expressions would be the wrong approach--there's
no way an RE can deal with replacing a :: with the correct number of
zeroes, among other complications.

Hyuga

Nov 8 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: John Burton | last post by:
I'm not sure if this is a question about python socket support or sockets in general ... I have a socket server with which I want to accept incoming connections on ipv4 and ipv6. I've found...
4
by: Kaare Rasmussen | last post by:
Sorry if this is obvious to you, but it's not obvious to me. In 7.4 this psql -h 127.0.0.1 test and psql -h localhost test will be authenticated as ipv6 addresses. As long as you know, it's...
0
by: Addam | last post by:
Anyone, I am writting a server that accepts a list of IPv4 and IPv6 address. The IPv4 address are padded with leading zeros. I belive this allows me to just point to the beging and run both IPv4...
2
by: prabhuram.k | last post by:
Can anybody know how to validate IPV4 and IPV6 address in PHP. I am using PHP 4.2.3 for some compatiability reason.
3
by: jiri.juranek | last post by:
Hello, is there any common function for validation if string contains valid ip address(both ipv4 and ipv6)? Or does sb wrote some regular expression for this? thanks J
3
by: Pramod TK | last post by:
Hello All, I have some queries related to python support for IPv6. Can you kindly clarify the doubts which I have - 1. Does python support IPv6? 2. Does it support setting of QoS flags? 3. Does...
7
by: John Nagle | last post by:
Is there something available that will parse the "netloc" field as returned by URLparse, including all the hard cases? The "netloc" field can potentially contain a port number and a numeric IP...
8
by: Giampaolo Rodola' | last post by:
I'm not sure if this is a question about python programming, system administration or sockets in general... I have the FTP server in my signature to which I'd want to add IPv6 support. My hosting...
14
by: Simon | last post by:
Hi, is there a straight forward way of converting IPv4 to IPv6? I thought that it was just a matter of converting 32 bits to 128 bits, (by adding 96 leading 0s), but that does not seem right...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.