Commit 1b9293a9 authored by Tomas Hlavacek's avatar Tomas Hlavacek

Cisco implementation improved.

Bug with None result fixed.
New command formating using python formating string implemented.
parent e1f1403c
......@@ -159,7 +159,7 @@
<div id="result">
<hr/>
<py:choose>
<py:when test="defined('result')">
<py:when test="defined('result') and result">
${result}
</py:when>
<py:otherwise>
......
......@@ -133,6 +133,9 @@ class Session(object):
self.result = result
self.save()
def setPreResult(self,result):
self.setResult('<pre>'+result+'</pre>')
def getResult(self):
return self.result
......@@ -291,7 +294,8 @@ class ULGCgi:
session.setFinished()
except Exception as e:
# TODO: log("Exception occured while running a command")
pass
session.setPreResult(traceback.format_exc())
session.setFinished()
finally:
decreaseUsageMethod()
......@@ -379,7 +383,7 @@ class ULGCgi:
default_commandid=session.getCommandId(),
default_params=session.getParameters(),
default_sessionid=sessionid,
result=Markup(session.getResult()),
result=Markup(session.getResult()) if (session.getResult()) else None,
refresh=refresh,
getFormURL=self.decorator.getRuncommandURL
).render('html', doctype='html')
......@@ -398,7 +402,7 @@ class ULGCgi:
default_routerid=0,
default_commandid=0,
default_sessionid=None,
result=Markup(result_text),
result=Markup(result_text) if(result_text) else None,
refresh=0,
getFormURL=self.decorator.getRuncommandURL
).render('html', doctype='html')
......@@ -416,7 +420,7 @@ class ULGCgi:
default_routerid=0,
default_commandid=0,
default_sessionid=None,
result=Markup(result_text),
result=Markup(result_text) if(result_text) else None,
refresh=0,
getFormURL=self.decorator.getRuncommandURL
).render('html', doctype='html')
......
......@@ -28,7 +28,7 @@ import defaults
import ulgmodel
class BirdRouterLocal(ulgmodel.LocalRouter):
DefaultCommands = [ulgmodel.TextCommand('show protocols', [ulgmodel.TextParameter('.*')])]
DefaultCommands = [ulgmodel.TextCommand('show protocols %s', [ulgmodel.TextParameter('.*')])]
def __init__(self,sock=defaults.default_bird_sock,commands=None):
super(self.__class__,self).__init__()
......
......@@ -32,15 +32,31 @@ STRING_EXPECT_PASSWORD='(P|p)assword:'
class CiscoRouter(ulgmodel.RemoteRouter):
RegExIPv4Subnet = '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(/[0-9]{1,2}){0,1}]$'
RegExIPv4 = '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$'
RegExIPv6Subnet = '^[0-9a-fA-F:]+(/[0-9]{1,2}){0,1}$'
RegExIPv6 = '^[0-9a-fA-F:]+$'
DefaultCommands = [ulgmodel.TextCommand('show version'),
ulgmodel.TextCommand('show ip bgp', [ulgmodel.TextParameter('^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(/[0-9]{1,2}){0,1}]$',name=defaults.STRING_IPSUBNET)]),
ulgmodel.TextCommand('show bgp ipv4 uni sum'),
ulgmodel.TextCommand('show bgp ipv6 uni sum'),
ulgmodel.TextCommand('show bgp ipv4 uni neighbor',[ulgmodel.TextParameter('^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$',name=defaults.STRING_IPADDRESS)]),
ulgmodel.TextCommand('show bgp ipv4 uni neighbor',[ulgmodel.TextParameter('[0-9a-fA-F:]+)]')]),
ulgmodel.TextCommand('show bgp ipv4 uni neighbor',[ulgmodel.SelectionParameter([('91.210.16.1','91.210.16.1'),('91.210.16.2','91.210.16.2'),('91.210.16.3','91.210.16.3')],name=defaults.STRING_IPADDRESS)]),
ulgmodel.TextCommand('show bgp ipv4 unicast %s', [ulgmodel.TextParameter(RegExIPv4Subnet,name=defaults.STRING_IPSUBNET)]),
ulgmodel.TextCommand('show bgp ipv6 unicast %s', [ulgmodel.TextParameter(RegExIPv6Subnet,name=defaults.STRING_IPSUBNET)]),
ulgmodel.TextCommand('show bgp ipv4 unicast summary'),
ulgmodel.TextCommand('show bgp ipv6 unicast summary'),
ulgmodel.TextCommand('show bgp ipv4 unicast neighbor %s',[ulgmodel.TextParameter(RegExIPv4,name=defaults.STRING_IPADDRESS)]),
ulgmodel.TextCommand('show bgp ipv6 unicast neighbor %s',[ulgmodel.TextParameter(RegExIPv6,name=defaults.STRING_IPADDRESS)]),
ulgmodel.TextCommand('show bgp ipv4 unicast neighbor %s received-routes',[ulgmodel.TextParameter(RegExIPv4,name=defaults.STRING_IPADDRESS)]),
ulgmodel.TextCommand('show bgp ipv6 unicast neighbor %s received-routes',[ulgmodel.TextParameter(RegExIPv6,name=defaults.STRING_IPADDRESS)]),
ulgmodel.TextCommand('show bgp ipv4 unicast neighbor %s advertised',[ulgmodel.TextParameter(RegExIPv4,name=defaults.STRING_IPADDRESS)]),
ulgmodel.TextCommand('show bgp ipv6 unicast neighbor %s advertised',[ulgmodel.TextParameter(RegExIPv6,name=defaults.STRING_IPADDRESS)]),
ulgmodel.TextCommand('show ip route %s',[ulgmodel.TextParameter(RegExIPv4,name=defaults.STRING_IPADDRESS)]),
ulgmodel.TextCommand('show ipv6 unicast route %s',[ulgmodel.TextParameter(RegExIPv6,name=defaults.STRING_IPADDRESS)]),
# ulgmodel.TextCommand('show bgp ipv4 uni neighbor',[ulgmodel.SelectionParameter([('91.210.16.1','91.210.16.1'),('91.210.16.2','91.210.16.2'),('91.210.16.3','91.210.16.3')],name=defaults.STRING_IPADDRESS)]),
]
STRING_SHELL_PROMPT = '>'
STRING_SHELL_PROMPT_REGEXP = '\n[a-zA-Z0-9\._-]+>'
def __init__(self, host, user, password, port=22, commands=None):
self.setHost(host)
......@@ -79,25 +95,25 @@ class CiscoRouter(ulgmodel.RemoteRouter):
raise Exception("pexpect session failed: SSH error.")
# check shell and preset terminal
i=p.expect([CiscoRouter.STRING_SHELL_PROMPT,pexpect.EOF])
i=p.expect([CiscoRouter.STRING_SHELL_PROMPT_REGEXP,pexpect.EOF])
if(i==0):
p.sendline('terminal length 0')
else:
raise Exception("pexpect session failed: Missing shell prompt.")
i=p.expect([CiscoRouter.STRING_SHELL_PROMPT,pexpect.EOF])
i=p.expect([CiscoRouter.STRING_SHELL_PROMPT_REGEXP,pexpect.EOF])
if(i==0):
p.sendline('terminal width 0')
else:
raise Exception("pexpect session failed: Missing shell prompt.")
i=p.expect([CiscoRouter.STRING_SHELL_PROMPT,pexpect.EOF])
i=p.expect([CiscoRouter.STRING_SHELL_PROMPT_REGEXP,pexpect.EOF])
if(i==0):
p.sendline(command)
else:
raise Exception("pexpect session failed: Missing shell prompt.")
p.expect(['\n[^\n]*'+CiscoRouter.STRING_SHELL_PROMPT])
p.expect([CiscoRouter.STRING_SHELL_PROMPT_REGEXP,pexpect.EOF])
def stripFirstLine(string):
lines = str.splitlines(string)
......
......@@ -134,7 +134,10 @@ class TextCommand(object):
self.param_specs=param_specs
if(name==None):
self.name=command
if(self.param_specs):
self.name=command % tuple([('<'+str(c.getName())+'>') for c in self.param_specs])
else:
self.name=command
def getParamSpecs(self):
return self.param_specs
......@@ -170,18 +173,17 @@ class TextCommand(object):
def getCommandText(self,parameters=None):
if(self.checkParamsInput(parameters)):
c = self.command
parameters_normalized = self.normalizeParameters(parameters)
for p in parameters_normalized:
c = c + ' ' + p
if(parameters_normalized):
return self.command % tuple(parameters_normalized)
else:
return self.command
return c
else:
return None
def rescanHook(self):
def rescanHook(self,router):
pass
def decorateResult(self,result,router=None,decorator=None):
......@@ -224,7 +226,7 @@ class Router(object):
def rescanHook(self):
for c in self.listCommands():
c.rescanHook()
c.rescanHook(self)
def returnError(self,error=None):
r = '<em>'+defaults.STRING_ERROR_COMMANDRUN
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment