472,107 Members | 1,263 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,107 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 3563
* 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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by John Burton | last post: by
4 posts views Thread by Kaare Rasmussen | last post: by
reply views Thread by Addam | last post: by
2 posts views Thread by prabhuram.k | last post: by
3 posts views Thread by jiri.juranek | last post: by
3 posts views Thread by Pramod TK | last post: by
7 posts views Thread by John Nagle | last post: by
8 posts views Thread by Giampaolo Rodola' | last post: by
14 posts views Thread by Simon | last post: by

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.