Last active
October 6, 2021 03:50
-
-
Save brainix/14eeb54be51be10c978e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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