Skip to content

Instantly share code, notes, and snippets.

@rodfersou
Forked from brainix/nettools.py
Created March 4, 2016 20:41
Show Gist options
  • Save rodfersou/bbefca89d3ef47bc8e83 to your computer and use it in GitHub Desktop.
Save rodfersou/bbefca89d3ef47bc8e83 to your computer and use it in GitHub Desktop.
import jnettool.tools.Routing
import jnettool.tools.elements.NetworkElement
class NetworkElementError(Exception):
pass
class NetworkElement(object):
def __init__(self, ipaddr):
self.ipaddr = ipaddr
self.oldne = jnettool.tools.elements.NetworkElement(ipaddr)
@property
def routing_table(self):
try:
return RoutingTable(self.oldne.getRoutingTable())
except jnettool.tools.elements.MissingVar:
raise NetworkElementError('No routing table found')
def __enter__(self):
return self
def __exit__(self, exctype, excinst, exctb):
if exctype == NetworkElementError:
logging.exception('No routing table found')
self.oldne.cleanup('rollback')
else:
self.oldne.cleanup('commit')
self.oldne.disconnect()
def __repr__(self):
return '%s(%r)' % (self.__class__.__name__, self.ipaddr)
class RoutingTable(object):
def __init__(self, oldrt):
self.oldrt = oldrt
def __len__(self):
return self.oldrt.getSize()
def __getitem__(self, index):
if index >= len(self):
raise IndexError
return Route(self.oldrt.getRouteByIndex(index))
class Route(object):
def __init__(self, old_route):
self.old_route = old_route
@property
def name(self):
return self.old_route.getName()
@property
def ipaddr(self):
return self.old_route.getIPAddr()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment