Skip to content

Instantly share code, notes, and snippets.

@klette
Created June 15, 2009 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klette/130063 to your computer and use it in GitHub Desktop.
Save klette/130063 to your computer and use it in GitHub Desktop.
import re
LAN_REGEXP = re.compile('^([\w-]+),([\w-]+),?([\w-]+)?,?(\d+)?$')
CORE_REGEXP = LAN_REGEXP
ELINK_REGEXP = LAN_REGEXP
LINK_REGEXP = re.compile('^([\w-]+),?([\w-]+)?,?(\d+)?$')
def parse_ifDescr(ifDescr):
type = ifDescr[:ifDescr.find(',')]
print type
if type not in ('lan','core','link','elink'):
return None
if type == 'lan':
print "parsing string %s" % ifDescr[4:]
data = LAN_REGEXP.findall(ifDescr[4:])
if not data:
return None
return dict(zip(('organisation','usage','comment','vlan'), data[0]))
elif type == 'core':
print "parsing string %s" % ifDescr[5:]
data = CORE_REGEXP.findall(ifDescr[5:])
if not data:
return None
return dict(zip(('organisation','usage','comment','vlan'), data[0]))
elif type == 'link':
print "parsing string %s" % ifDescr[5:]
data = LINK_REGEXP.findall(ifDescr[5:])
if not data:
return None
return dict(zip(('to_router','comment','vlan'), data[0]))
elif type == 'elink':
print "parsing string %s" % ifDescr[6:]
data = ELINK_REGEXP.findall(ifDescr[6:])
if not data:
return None
return dict(zip(('to_router','to_organisation', 'comment','vlan'), data[0]))
return None
# Test stuff
d = parse_ifDescr('lan,math,staff')
assert(d['organisation'] == 'math')
assert(d['usage'] == 'staff')
d = parse_ifDescr('lan,physics,students,campus_dragv,340')
assert(d['organisation'] == 'physics')
assert(d['usage'] == 'students')
assert(d['comment'] == 'campus_dragv')
assert(d['vlan'] == '340')
d = parse_ifDescr('lan,math,staff2,campus_lade')
assert(d['organisation'] == 'math')
assert(d['usage'] == 'staff2')
assert(d['comment'] == 'campus_lade')
d = parse_ifDescr('core,it,wlan')
assert(d['organisation'] == 'it')
assert(d['usage'] == 'wlan')
d = parse_ifDescr('core,it,fddi,manring,180')
assert(d['organisation'] == 'it')
assert(d['usage'] == 'fddi')
assert(d['comment'] == 'manring')
assert(d['vlan'] == '180')
d = parse_ifDescr('link,mts-gw')
assert(d['to_router'] == 'mts-gw')
d = parse_ifDescr('link,moholt-gw,Tn_20022350,923')
assert(d['to_router'] == 'moholt-gw')
assert(d['comment'] == 'Tn_20022350')
assert(d['vlan'] == '923')
d = parse_ifDescr('elink,trd-gw,uninett')
assert(d['to_router'] == 'trd-gw')
assert(d['to_organisation'] == 'uninett')
d = parse_ifDescr('elink,sintef-gw,sintef,,902')
assert(d['to_router'] == 'sintef-gw')
assert(d['to_organisation'] == 'sintef')
assert(d['vlan'] == '902')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment