ulgcisco.py 34.2 KB
Newer Older
1 2 3
#!/usr/bin/env python
#
# ULG - Universal Looking Glass
4
# (C) 2012 CZ.NIC, z.s.p.o.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


# Imports
import os
import pexpect
import re
24 25
import sys
import string
26
import hashlib
27 28 29 30

import defaults

import ulgmodel
31
import ulggraph
32

33
# module globals
34 35
STRING_EXPECT_SSH_NEWKEY='Are you sure you want to continue connecting'
STRING_EXPECT_PASSWORD='(P|p)assword:'
36
STRING_EXPECT_SHELL_PROMPT_REGEXP = '\n[a-zA-Z0-9\._-]+(>|#)'
37
BGP_IPV6_SUM_TABLE_SPLITLINE_REGEXP='^\s*[0-9a-fA-F:]+\s*$'
38
IPV46_ADDR_REGEXP = '^[0-9a-fA-F:\.]+$'
39 40

BGP_IPV6_TABLE_HEADER_REGEXP='^\s*(Neighbor)\s+(V)\s+(AS)\s+(MsgRcvd)\s+(MsgSent)\s+(TblVer)\s+(InQ)\s+(OutQ)\s+(Up/Down)\s+(State/PfxRcd)\s*$'
41 42
BGP_IPV46_TABLE_LINE_REGEXP='^\s*([0-9a-fA-F:\.]+)\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)\s+([a-zA-Z0-9:]+)\s+([a-zA-Z0-9\(\)]+|[a-zA-Z0-9]+\s\(Admin\))\s*$'
#BGP_IPV6_TABLE_LINE_REGEXP='^\s*([0-9a-fA-F:]+)\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)\s+([a-zA-Z0-9:]+)\s+([a-zA-Z0-9\(\)]+|[a-zA-Z0-9]+\s\(Admin\))\s*$'
43
BGP_IPV4_TABLE_HEADER_REGEXP='^\s*(Neighbor)\s+(V)\s+(AS)\s+(MsgRcvd)\s+(MsgSent)\s+(TblVer)\s+(InQ)\s+(OutQ)\s+(Up/Down)\s+(State/PfxRcd)\s*$'
44
#BGP_IPV4_TABLE_LINE_REGEXP='^\s*([0-9\.]+)\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)\s+([a-zA-Z0-9:]+)\s+([a-zA-Z0-9\(\)]+|[a-zA-Z0-9]+\s\(Admin\))\s*$'
45 46

BGP_PREFIX_TABLE_HEADER='^(\s)\s+(Network)\s+(Next Hop)\s+(Metric)\s+(LocPrf)\s+(Weight)\s+(Path)\s*$'
47 48 49 50 51 52

RESCAN_BGP_IPv4_COMMAND='show bgp ipv4 unicast summary'
RESCAN_BGP_IPv6_COMMAND='show bgp ipv6 unicast summary'

MAC_ADDRESS_REGEXP = '^[0-9a-fA-F]{4}\.[0-9a-fA-F]{4}\.[0-9a-fA-F]{4}$'

53 54 55
BGP_RED_STATES = ['Idle', 'Active', '(NoNeg)']
BGP_YELLOW_STATES = ['Idle (Admin)',]

56
REGEX_SH_BGP_UNI_ASLINE = '^(\s*)([0-9\s]+|Local)(|,.*)\s*$'
57 58 59 60 61 62 63 64 65 66 67
regex_sh_bgp_uni_asline = re.compile(REGEX_SH_BGP_UNI_ASLINE)

REGEX_SH_BGP_UNI_AGGR = '\s*\(aggregated by ([0-9]+) [0-9a-fA-F:\.]+\).*'
regex_sh_bgp_uni_aggr = re.compile(REGEX_SH_BGP_UNI_AGGR)

REGEX_SH_BGP_UNI_RECUSE = '\s*\(received & used\).*'
regex_sh_bgp_uni_recuse = re.compile(REGEX_SH_BGP_UNI_RECUSE)

REGEX_SH_BGP_UNI_RECONLY = '\s*\(received-only\).*'
regex_sh_bgp_uni_reconly = re.compile(REGEX_SH_BGP_UNI_RECONLY)

68
REGEX_SH_BGP_UNI_TABLE_START = '\s*(Advertised\s+to\s+update-groups|Not\s+advertised\s+to\s+any\s+peer).*'
69 70 71 72 73 74 75 76
regex_sh_bgp_uni_table_start = re.compile(REGEX_SH_BGP_UNI_TABLE_START)

REGEX_SH_BGP_UNI_PEERLINE = '\s*([0-9a-fA-F:\.]+)\s.*from\s.*'
regex_sh_bgp_uni_peerline = re.compile(REGEX_SH_BGP_UNI_PEERLINE)

REGEX_SH_BGP_UNI_ORIGLINE_BEST = '\s*Origin\s.*\sbest.*'
regex_sh_bgp_uni_origline_best = re.compile(REGEX_SH_BGP_UNI_ORIGLINE_BEST)

77 78
COMMAND_NAME_GRAPH4 = 'Graph - show bgp ipv4 unicast <IP subnet>'
COMMAND_NAME_GRAPH6 = 'Graph - show bgp ipv6 unicast <IP subnet>'
79

80 81
STRING_COMMAND_LOGOUT = 'logout'

82 83
def cisco_parse_sh_bgp_uni(lines,prependas):
	def split_ases(ases):
84 85 86 87
		if ases == 'Local':
			return []
		else:
			return str.split(ases)
88 89 90 91 92

	def get_info(info):
		res = {'recuse':False, 'reconly':False, 'aggr':None}

		for g in info.split(','):
93 94
#			if(regex_sh_bgp_uni_recuse.match(g)):
#				res['recuse'] = True
95 96 97 98 99 100 101 102 103 104
			if(regex_sh_bgp_uni_reconly.match(g)):
				res['reconly'] = True

			m = regex_sh_bgp_uni_aggr.match(g)
			if(m):
				res['aggr'] = m.group(1)

		return res

	paths = []
105
	table_prestarted = False
106 107
	table_started = False
	for l in str.splitlines(lines):
108 109 110 111 112 113 114 115 116 117 118 119 120
		if not table_started:
			if table_prestarted:
				# if prestarted and a line not containing only numbers of
				# update groups, start immediately
				if not re.match('^[0-9\s]+$',l):
					table_started = True

			# if not prestarted and match occures, wait for next line
			if(regex_sh_bgp_uni_table_start.match(l)):
				table_prestarted = True


		if table_started:
121 122
			m = regex_sh_bgp_uni_asline.match(l)
			if(m):
123
				ases = [ulgmodel.annotateAS("AS"+str(asn)) for asn in [prependas] + split_ases(m.group(2))]
124
				infotext = m.group(3)
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
				if(infotext):
					paths.append((ases,get_info(infotext)))
				else:
					paths.append((ases,{'recuse':False, 'reconly':False, 'aggr':None}))
				continue

			m = regex_sh_bgp_uni_peerline.match(l)
			if(m):
				paths[-1][1]['peer'] = m.group(1)
				continue

			m = regex_sh_bgp_uni_origline_best.match(l)
			if(m):
				paths[-1][1]['recuse'] = True

	return paths


def reduce_bgp_paths(paths):
	def has_valid(paths,onepath):
		for p in paths:
			if((onepath[0] == p[0]) and (not p[1]['reconly'])):
				return True
		return False

	def has_used(paths,onepath):
		for p in paths:
			if((onepath[0] == p[0]) and (p[1]['recuse'])):
				return True
		return False

	def assign_value(path):
		if(path[1]['recuse']):
			return 1
		elif(path[1]['reconly']):
			return 100
		else:
			return 10

	newpaths = []
	for p in paths:
		if((p[1]['reconly']) and has_valid(paths,p)):
			pass
		elif(not (p[1]['recuse']) and has_used(paths,p)):
			pass
		else:
			newpaths.append(p)
	return sorted(newpaths,key=assign_value)
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193

def matchCiscoBGPLines(header,lines):
    # Match cisco lines formatted to be aligned to columns. Like:
    #    Network          Next Hop            Metric LocPrf Weight Path
    # *  79.170.248.0/21  91.210.16.6            300             0 20723 i
    # *>i91.199.207.0/24  217.31.48.123            0   1024      0 44672 i
    # 0  1                2                   3      4      5      6
    #
    #    Network          Next Hop            Metric LocPrf Weight Path
    # *>i2001:67C:278::/48
    #                     2001:1AB0:B0F4:FFFF::2
    #                                         0        1024      0 51278 i
    # *> 2001:1AB0::/32   ::                  0              32768 i
    # 0  1                2                   3      4      5      6
    #
    # First find boundaries, second section text (if it overflow next boundary
    # line wrap is expected) and then remove white spaces and return sections.
    #
    # ?1 hat happens when last element is line-wrapped? It looks like it does
    # not happen in my settings.

194
    def divideGroups(line,max_index_start=sys.maxint,table_line=False,first_group_length=3):
195 196 197 198
        # divide groups starting before max_index_start
        result = []

        # when parsing table_line (not the header and not the continuation line)
199 200 201 202 203 204 205 206 207
        # cut off first N charactes and use them as the first group (flags) when there is
        # some non-blank characters in the first two groups (therefore the line is the leading)
        if(table_line):
            if(re.match('[^\s]+',line[0:(first_group_length+2)])):
                result.append([0,first_group_length])
	        line = ' '*first_group_length + line[first_group_length:]
	else:
            # parsing header, add virtual Status group to the header
	    line = 'S'+line[1:]
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222

        last_group = False
        for r in re.compile('[^\s]+').finditer(line):
            if(not last_group):
                result.append([r.start(),r.end()])

            if(r.start() >= max_index_start):
                last_group = True

        # shortcut for empty lines / no results
        if(len(result)==0):
            return None

        # add tail to last groups
        result[-1][1] = len(line)
223 224 225 226 227

	# in header force the first group to spann till the end of the next group
	if(not table_line):
            result[0][1] = result[1][0]-1

228 229 230 231
        return result


    def matchGroup(header_groups_indexes,line_group_indexes,last_element):
232 233
        ulgmodel.debug('matchGroup header_group_indexes='+str(header_groups_indexes)+' line_group_indexes='+str(line_group_indexes))

234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
        if(len(header_groups_indexes) == 1):
            return 0

        # beginning of the second group is right of the beginning of the tested
        if((len(header_groups_indexes) > 1) and
           (header_groups_indexes[1][0] > line_group_indexes[0])):
            return 0

        # beginning of the last (and the only possible) group is left
        # the beginning of the tested
        if(header_groups_indexes[-1][0] <= line_group_indexes[0]):
            return (len(header_groups_indexes)-1)

        # linear algorithm !!!
        # rewrite to tree(?)
        for hgipos,hgi in enumerate(header_groups_indexes):

            if((hgipos >= 1) and
               (hgipos < (len(header_groups_indexes)-1)) and
               (header_groups_indexes[hgipos-1][1] <= line_group_indexes[0]) and
               (header_groups_indexes[hgipos+1][0] >= line_group_indexes[1])):
                return hgipos

            if((last_element) and
               (hgipos >= 1) and
               (hgi[0] <= line_group_indexes[0]) and
               (len(header_groups_indexes)-1 > hgipos) and
               (header_groups_indexes[hgipos+1][0] > line_group_indexes[0])):
                return hgipos

        return None


    def normalize(instr):
        return instr.strip()


    hgidxs = divideGroups(header)
    result = [[]]

    for l in lines:
        # divide groups (leave the last group in one part)
276 277
        # define boundary of the first group by the first letter of the second group beginning
        lgps = divideGroups(l,hgidxs[-1][0],True,hgidxs[1][0]-1)
278 279 280 281 282 283 284 285 286
        if(lgps==None):
            continue

        for lgpidx,lgp in enumerate(lgps):
            gidx = matchGroup(hgidxs,lgp,(lgpidx == (len(lgps)-1)))
            if(gidx == None):
                raise Exception("No group matched for line indexes. line="+l+" header_group_indexes="+
                                str(hgidxs)+" line_group_index="+str(lgp))

287

288 289 290 291 292 293 294 295 296
            if(gidx < len(result[-1])):
                result.append([])

            while(gidx > len(result[-1])):
                result[-1].append('')


            result[-1].append(normalize(l[lgp[0]:lgp[1]]))

297
    ulgmodel.debug('bgpmatchlines:'+str(result))
298

299 300 301
    return result

def normalizeBGPIPv6SumSplitLines(lines):
302
    """This function concatenates lines with longer IPv6 addresses that
303
router splits on the header boundary."""
304
    result = []
305
    slr = re.compile(BGP_IPV6_SUM_TABLE_SPLITLINE_REGEXP)
306 307 308 309 310 311 312 313 314 315 316 317
    b = None
    
    for l in lines:
        if(b):
            result.append(b + l)
            b = None
        elif(slr.match(l)):
            b = l
        else:
            result.append(l)
            
    return result
318

319
# classes
320

321 322
class CiscoCommandBgpIPv46Sum(ulgmodel.TextCommand):
    """ Abstract class, baseline for IPv4 and IPv6 versions. """
323

324 325 326
    RED_STATES = BGP_RED_STATES
    YELLOW_STATES = BGP_YELLOW_STATES

327 328
    def __init__(self,name=None,peer_address_command=None,peer_received_command=None):
        self.command=self.COMMAND_TEXT
329
        self.param_specs=[]
330
        self.peer_address_command = peer_address_command
331
        self.peer_received_command = peer_received_command
332 333 334 335 336 337 338 339 340

        if(name==None):
            if(self.param_specs):
                self.name=self.command % tuple([('<'+str(c.getName())+'>') for c in self.param_specs])
            else:
                self.name=self.command
        else:
            self.name = name

341

342
    def _getPeerURL(self,decorator_helper,router,peer_address):
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
        if decorator_helper and self.peer_address_command:
            return decorator_helper.getRuncommandURL({'routerid':str(decorator_helper.getRouterID(router)),
                                                      'commandid':str(decorator_helper.getCommandID(router,self.peer_address_command)),
                                                      'param0':peer_address})
        else:
            return None


    def _getPeerReceivedURL(self,decorator_helper,router,peer_address):
        if decorator_helper and self.peer_received_command:
            return decorator_helper.getRuncommandURL({'routerid':str(decorator_helper.getRouterID(router)),
                                                      'commandid':str(decorator_helper.getCommandID(router,self.peer_received_command)),
                                                      'param0':peer_address})
        else:
            return None


    def _getPeerTableCell(self,decorator_helper,router,peer):
        url = self._getPeerURL(decorator_helper,router,peer)
        if url:
            return decorator_helper.ahref(url,peer)
        else:
            return peer


    def _getReceivedTableCell(self,decorator_helper,router,peer,received):
        if re.compile('[0-9]+').match(received):
            url = self._getPeerReceivedURL(decorator_helper,router,peer)
            if url:
                return decorator_helper.ahref(url,received)
            else:
                return received
        else:
            return received

378
    def _decorateTableLine(self,line,decorator_helper,router):
379
        lrm = re.compile(self.TABLE_LINE_REGEXP).match(line)
380 381
        if(lrm):
            # color selection
382
            if(lrm.group(10) in self.YELLOW_STATES):
383
                color = ulgmodel.TableDecorator.YELLOW
384
            elif(lrm.group(10) in self.RED_STATES):
385 386 387 388
                color = ulgmodel.TableDecorator.RED
            else:
                color = ulgmodel.TableDecorator.GREEN

389
            # generate table content
390
            return [
391
                (self._getPeerTableCell(decorator_helper,router,lrm.group(1)),color),
392
                (lrm.group(2),color),
393
                (decorator_helper.decorateASN(lrm.group(3),prefix=''),color),
394 395 396 397 398 399
                (lrm.group(4),color),
                (lrm.group(5),color),
                (lrm.group(6),color),
                (lrm.group(7),color),
                (lrm.group(8),color),
                (lrm.group(9),color),
400
                (self._getReceivedTableCell(decorator_helper,router,lrm.group(1),lrm.group(10)),color),
401
                ]
402
        else:
403
            raise Exception("Can not parse line: "+line)
404

405 406 407
    def decorateResult(self,session,decorator_helper=None):
        if(not session):
            raise Exception('Can not decorate result without valid session.')
408 409 410

	if(session.getResult() == None):
            return (decorator_helper.pre(defaults.STRING_EMPTY), 1)
411 412 413
        
        if((not session.getRouter()) or (not decorator_helper)):
            return "<pre>\n%s\n</pre>" % session.getResult()
414

415 416 417 418
	lines=[]
        res=''
        for l in normalizeBGPIPv6SumSplitLines(str.splitlines(session.getResult())):
            lines.append(l)
419 420 421 422 423 424 425

        before=''
        after=''
        table=[]
        table_header=[]

        tb = False
426 427
        header_regexp = re.compile(self.TABLE_HEADER_REGEXP)
        line_regexp = re.compile(self.TABLE_LINE_REGEXP)
428 429 430
        for l in lines:
            if(tb):
                # inside table body
431
                table.append(self._decorateTableLine(l,decorator_helper,session.getRouter()))
432 433 434 435 436 437 438

            else:
                # should we switch to table body?
                thrm = header_regexp.match(l)
                if(thrm):
                    tb = True
                    table_header = [g for g in thrm.groups()]
439 440 441
                else:
                    # not yet in the table body, append before-table section
                    before = before + l + '\n'
442

443
        result_len = len(table)
444
        table = table[session.getRange():session.getRange()+defaults.range_step]
445 446

        return (ulgmodel.TableDecorator(table,table_header,before=decorator_helper.pre(before)).decorate(),result_len)
447 448


449 450
class CiscoCommandBgpIPv4Sum(CiscoCommandBgpIPv46Sum):
    COMMAND_TEXT='show bgp ipv4 unicast summary'
451
    TABLE_LINE_REGEXP=BGP_IPV46_TABLE_LINE_REGEXP
452
    TABLE_HEADER_REGEXP=BGP_IPV4_TABLE_HEADER_REGEXP
453 454 455 456 457 458 459

    def __init__(self,name=None,peer_address_command=None,peer_received_command=None):
        return CiscoCommandBgpIPv46Sum.__init__(self,name,peer_address_command,peer_received_command)


class CiscoCommandBgpIPv6Sum(CiscoCommandBgpIPv46Sum):
    COMMAND_TEXT='show bgp ipv6 unicast summary'
460
    TABLE_LINE_REGEXP=BGP_IPV46_TABLE_LINE_REGEXP
461
    TABLE_HEADER_REGEXP=BGP_IPV6_TABLE_HEADER_REGEXP
462 463 464 465 466

    def __init__(self,name=None,peer_address_command=None,peer_received_command=None):
        return CiscoCommandBgpIPv46Sum.__init__(self,name,peer_address_command,peer_received_command)


467 468 469
class CiscoCommandShowBgpIPv4Neigh(ulgmodel.TextCommand):
    COMMAND_TEXT='show bgp ipv4 unicast neighbor %s'

470 471
    def __init__(self,router,name=None):
        ulgmodel.TextCommand.__init__(self,self.COMMAND_TEXT,param_specs=[router.getBGPIPv4Select()],name=name)
472 473 474 475

class CiscoCommandShowBgpIPv6Neigh(ulgmodel.TextCommand):
    COMMAND_TEXT='show bgp ipv6 unicast neighbor %s'

476 477
    def __init__(self,router,name=None):
        ulgmodel.TextCommand.__init__(self,self.COMMAND_TEXT,param_specs=[router.getBGPIPv6Select()],name=name)
478

479
class CiscoCommandShowBgpIPv46Select(ulgmodel.TextCommand):
480 481 482
    TABLE_HEADER_REGEXP=BGP_PREFIX_TABLE_HEADER
    LASTLINE_REGEXP='^\s*Total number of prefixes [0-9]+\s*$'

483 484
    def __init__(self,peer_select_param,name=None):
        ulgmodel.TextCommand.__init__(self,self.COMMAND_TEXT,param_specs=[peer_select_param],name=name)
485

486 487 488 489 490
    def _decorateASPath(self,path,decorator_helper):
        result = ''
        for asnm in re.compile('[^\s]+').finditer(path):
            asn = asnm.group(0)
            if(asn.isdigit()):
491
                result = result + ' ' + decorator_helper.decorateASN(asn,prefix='')
492
            else:
493
                if(re.match('^\s*\{[0-9,]+\}\s*', asn)):
494 495
                    result = result + '{'
                    isnext = False
496
                    for sasnm in re.compile('([0-9]+)(,|\})').finditer(asn):
497 498 499
                        sasn = sasnm.group(0)
                        if(sasn.isdigit()):
                            if(isnext):
500
                                result = result + ',' + decorator_helper.decorateASN(sasn,prefix='')
501 502
                            else:
                                isnext = True
503
                                result = result + decorator_helper.decorateASN(sasn,prefix='')
504 505 506 507 508 509
                    result = result + '}'
                else:
                    result = result + ' ' +asn

        return result

510 511
    def _genTable(self,table_lines,decorator_helper,router):
        mls = matchCiscoBGPLines(self.table_header,table_lines)
512

513 514
        result = []
        for ml in mls:
515 516 517
		# generate table content
		result.append([
				(ml[0],),
518
				(decorator_helper.decoratePrefix(ml[1]),),
519 520 521 522 523 524 525
				(ml[2],),
				(ml[3],),
				(ml[4],),
				(ml[5],),
				(self._decorateASPath(ml[6],decorator_helper),),
				])
	return result
526

527
    def decorateResult(self,session,decorator_helper=None):
528 529 530 531 532 533 534 535 536
	def restrict(lines,start,count):
            res=[]
            s=start
	    r = re.compile("^\s{6,}.*")
	    
            while(r.match(lines[s]) and s > 0):
                s=s-1

	    e=s+count
537 538
	    if(e >= len(lines)):
		    e=len(lines)-1
539 540 541 542 543
	    while(r.match(lines[e]) and e < (len(lines)-1)):
                e=e+1

	    return lines[s:e]

544 545 546
        if(not session):
            raise Exception("Can not decorate result without valid session passed.")

547 548 549
	if(session.getResult() == None):
            return (decorator_helper.pre(defaults.STRING_EMPTY), 1)

550 551
        if((not session.getRouter()) or (not decorator_helper)):
            return "<pre>\n%s\n</pre>" % session.getResult()
552

553
        lines = str.splitlines(session.getResult())
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584

        before=''
        after=None
        table=[]
        table_header_descr=[]

        tb = False
        header_regexp = re.compile(self.TABLE_HEADER_REGEXP)
        lastline_regexp = re.compile(self.LASTLINE_REGEXP)
        table_lines = []
        for l in lines:
            if(tb):
                # inside table body
                if(lastline_regexp.match(l)):
                    after = l
                else:
                    table_lines.append(l)

            else:
                # should we switch to table body?
                thrm = header_regexp.match(l)
                if(thrm):
                    # set header accoring to the local router alignment
                    # include (unnamed) states (=S)
                    self.table_header = 'S'+(l[1:].replace('Next Hop','Next_Hop',1))
                    tb = True
                    table_header_descr = [g for g in thrm.groups()]
                else:
                    # not yet in the table body, append before-table section
                    before = before + l + '\n'

585
        result_len = len(table_lines)
586
        if(table_lines):
587
            table = self._genTable(restrict(table_lines,session.getRange(),defaults.range_step),
588
                                   decorator_helper,session.getRouter())
589 590 591 592

        if(after):
            after=decorator_helper.pre(after)

593
        return (ulgmodel.TableDecorator(table,table_header_descr,before=decorator_helper.pre(before),
594
                                       after=after).decorate(),result_len)
595

596
        
597 598 599
class CiscoCommandShowBgpIPv4NeighAdv(CiscoCommandShowBgpIPv46Select):
    COMMAND_TEXT='show bgp ipv4 unicast neighbor %s advertised'

600 601 602
    def __init__(self,router,name=None):
        ulgmodel.TextCommand.__init__(self,self.COMMAND_TEXT,[router.getBGPIPv4Select()],name=name)

603 604 605
class CiscoCommandShowBgpIPv6NeighAdv(CiscoCommandShowBgpIPv46Select):
    COMMAND_TEXT='show bgp ipv6 unicast neighbor %s advertised'

606 607 608
    def __init__(self,router,name=None):
        ulgmodel.TextCommand.__init__(self,self.COMMAND_TEXT,[router.getBGPIPv6Select()],name=name)

609 610 611
class CiscoCommandShowBgpIPv4NeighRecv(CiscoCommandShowBgpIPv46Select):
    COMMAND_TEXT='show bgp ipv4 unicast neighbor %s received-routes'

612 613 614
    def __init__(self,router,name=None):
        ulgmodel.TextCommand.__init__(self,self.COMMAND_TEXT,[router.getBGPIPv4Select()],name=name)

615 616 617
class CiscoCommandShowBgpIPv6NeighRecv(CiscoCommandShowBgpIPv46Select):
    COMMAND_TEXT='show bgp ipv6 unicast neighbor %s received-routes'

618 619 620
    def __init__(self,router,name=None):
        ulgmodel.TextCommand.__init__(self,self.COMMAND_TEXT,[router.getBGPIPv6Select()],name=name)

621 622 623
class CiscoCommandGraphShowBgpIPv46Uni(ulgmodel.TextCommand):
    TABLE_HEADER_REGEXP=BGP_PREFIX_TABLE_HEADER

624
    def __init__(self,name=None,param=None):
625 626 627
        ulgmodel.TextCommand.__init__(self,self.COMMAND_TEXT,
				      param_specs=[param],
				      name=name)
628 629

    def decorateResult(self,session,decorator_helper=None):
630
        if(session.isFinished()):
631
		if(session.getData() != None) and (session.getData() != []):
632
			return (decorator_helper.img(decorator_helper.getSpecialContentURL(session.getSessionId()),defaults.STRING_BGP_GRAPH),1)
633
		else:
634
			return (decorator_helper.pre(defaults.STRING_BGP_GRAPH_ERROR), 1)
635 636 637 638 639
	else:
		return ('',0)

    def finishHook(self,session):
	    session.setData(cisco_parse_sh_bgp_uni(session.getResult(),str(session.getRouter().getASN())))
640 641

    def getSpecialContent(self,session,**params):
642
        paths = session.getData()
643 644 645 646 647 648 649
        print "Content-type: image/png\n"
        ulggraph.bgp_graph_gen(reduce_bgp_paths(paths),start=session.getRouter().getName(),
			       end=session.getParameters()[0])

    def showRange(self):
        return False

650 651 652

class CiscoCommandGraphShowBgpIPv4Uni(CiscoCommandGraphShowBgpIPv46Uni):
    COMMAND_TEXT='show bgp ipv4 unicast %s'
653

654 655
    def __init__(self,router,name=None):
        CiscoCommandGraphShowBgpIPv46Uni.__init__(self,name,param=ulgmodel.IPv4SubnetParameter())
656

657 658 659

class CiscoCommandGraphShowBgpIPv6Uni(CiscoCommandGraphShowBgpIPv46Uni):
    COMMAND_TEXT='show bgp ipv6 unicast %s'
660

661 662
    def __init__(self,router,name=None):
        CiscoCommandGraphShowBgpIPv46Uni.__init__(self,name,param=ulgmodel.IPv6SubnetParameter())
663 664


665

666
class CiscoShowBgpIPv46Uni(ulgmodel.TextCommand):
667 668
	def __init__(self,param):
		ulgmodel.TextCommand.__init__(self,self.COMMAND_TEXT,[param]),
669

670 671 672 673 674 675 676
	def decorateResult(self,session,decorator_helper=None):
		def decorateLine(l):			
			m = regex_sh_bgp_uni_asline.match(l)
			if(m):
				r = m.group(1)
				ases = str.split(m.group(2))
				for asn in ases:
677
					r = r + ' ' + decorator_helper.decorateASN(asn,prefix='')
678
				r = r + m.group(3)
679
				return decorator_helper.annotateIPs(r)
680
			else:
681
				return decorator_helper.annotateIPs(l)
682 683 684 685 686 687 688 689 690 691 692


		s = str.splitlines(session.getResult())
		lbeg = 0
		lend = len(s)
		if(session.getRange() != None and self.showRange()):
			lbeg = session.getRange()
			lend = session.getRange()+defaults.range_step+1

		r=''
		table_started=False
693
		table_prestarted = False
694 695 696 697 698
		for sl in s[lbeg:lend]:
			if(table_started):
				r += decorateLine(sl) + "\n"
			else:
				r += sl + "\n"
699
				if(table_prestarted):
700
					table_started = True
701 702
				if(regex_sh_bgp_uni_table_start.match(sl)):
					table_prestarted = True
703 704 705

		return ("<pre>\n%s\n</pre>" % r, len(s))

706 707 708

class CiscoShowBgpIPv4Uni(CiscoShowBgpIPv46Uni):
	COMMAND_TEXT = 'show bgp ipv4 unicast %s'
709 710 711 712

	def __init__(self,name=None):
		CiscoShowBgpIPv46Uni.__init__(self,param=ulgmodel.IPv4SubnetParameter())

713 714 715

class CiscoShowBgpIPv6Uni(CiscoShowBgpIPv46Uni):
	COMMAND_TEXT = 'show bgp ipv6 unicast %s'
716 717 718 719

	def __init__(self,name=None):
		CiscoShowBgpIPv46Uni.__init__(self,param=ulgmodel.IPv6SubnetParameter())

720

721
class CiscoRouter(ulgmodel.RemoteRouter):
722 723 724 725
    PS_KEY_BGPV4 = '-bgpipv4'
    PS_KEY_BGPV6 = '-bgpipv6'

    def _getDefaultCommands(self):
Tomas Hlavacek's avatar
Tomas Hlavacek committed
726
        return self._getBGPCommands()
727 728

    def _getAllCommands(self):
729 730 731 732 733 734 735 736
        _show_bgp_ipv4_uni_neigh = CiscoCommandShowBgpIPv4Neigh(self)
        _show_bgp_ipv4_uni_neigh_advertised = CiscoCommandShowBgpIPv4NeighAdv(self)
        _show_bgp_ipv4_uni_neigh_received_routes = CiscoCommandShowBgpIPv4NeighRecv(self)
        _show_bgp_ipv6_uni_neigh = CiscoCommandShowBgpIPv6Neigh(self)
        _show_bgp_ipv6_uni_neigh_advertised = CiscoCommandShowBgpIPv6NeighAdv(self)
        _show_bgp_ipv6_uni_neigh_received_routes = CiscoCommandShowBgpIPv6NeighRecv(self)
        _graph_show_bgp_ipv4_uni = CiscoCommandGraphShowBgpIPv4Uni(self,COMMAND_NAME_GRAPH4)
	_graph_show_bgp_ipv6_uni = CiscoCommandGraphShowBgpIPv6Uni(self,COMMAND_NAME_GRAPH6)
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751

        return [ulgmodel.TextCommand('show version'),
                ulgmodel.TextCommand('show interfaces status'),
                CiscoCommandBgpIPv4Sum('show bgp ipv4 unicast summary',
                                        peer_address_command=_show_bgp_ipv4_uni_neigh,
                                       peer_received_command=_show_bgp_ipv4_uni_neigh_received_routes),
                CiscoCommandBgpIPv6Sum('show bgp ipv6 unicast summary',
                                       peer_address_command=_show_bgp_ipv6_uni_neigh,
                                       peer_received_command=_show_bgp_ipv6_uni_neigh_received_routes),
                _show_bgp_ipv4_uni_neigh,
                _show_bgp_ipv6_uni_neigh,
                _show_bgp_ipv4_uni_neigh_received_routes,
                _show_bgp_ipv6_uni_neigh_received_routes,
                _show_bgp_ipv4_uni_neigh_advertised,
                _show_bgp_ipv6_uni_neigh_advertised,
752 753
                CiscoShowBgpIPv4Uni(),
		CiscoShowBgpIPv6Uni(),
754
                ulgmodel.TextCommand('show ip route %s',[ulgmodel.IPv4AddressParameter()]),
755
                ulgmodel.TextCommand('show ipv6 route %s',[ulgmodel.IPv6AddressParameter()]),
756 757 758 759
                ulgmodel.TextCommand('show ip arp %s',[ulgmodel.TextParameter('.*',name=defaults.STRING_NONEORINTORIPADDRESS)]),
                ulgmodel.TextCommand('show ipv6 neighbors %s',[ulgmodel.TextParameter('.*',name=defaults.STRING_NONEORINTORIPADDRESS)]),
                ulgmodel.TextCommand('show mac-address-table address %s',[ulgmodel.TextParameter(MAC_ADDRESS_REGEXP,name=defaults.STRING_MACADDRESS)]),
                ulgmodel.TextCommand('show mac-address-table interface %s',[ulgmodel.TextParameter('.*',name=defaults.STRING_INTERFACE)]),
760
                _graph_show_bgp_ipv4_uni,
761
		_graph_show_bgp_ipv6_uni,
762 763
		ulgmodel.TextCommand('ping %s',[ulgmodel.IPv64AddressParameter()]),
		ulgmodel.TextCommand('traceroute %s',[ulgmodel.IPv64AddressParameter()]),
764 765
                ]

766
    def _getBGPCommands(self):
767 768 769 770 771 772 773 774
        _show_bgp_ipv4_uni_neigh = CiscoCommandShowBgpIPv4Neigh(self)
        _show_bgp_ipv4_uni_neigh_advertised = CiscoCommandShowBgpIPv4NeighAdv(self)
        _show_bgp_ipv4_uni_neigh_received_routes = CiscoCommandShowBgpIPv4NeighRecv(self)
        _show_bgp_ipv6_uni_neigh = CiscoCommandShowBgpIPv6Neigh(self)
        _show_bgp_ipv6_uni_neigh_advertised = CiscoCommandShowBgpIPv6NeighAdv(self)
        _show_bgp_ipv6_uni_neigh_received_routes = CiscoCommandShowBgpIPv6NeighRecv(self)
        _graph_show_bgp_ipv4_uni = CiscoCommandGraphShowBgpIPv4Uni(self,COMMAND_NAME_GRAPH4)
	_graph_show_bgp_ipv6_uni = CiscoCommandGraphShowBgpIPv6Uni(self,COMMAND_NAME_GRAPH6)
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792

        return [
		CiscoCommandBgpIPv4Sum('show bgp ipv4 unicast summary',
                                        peer_address_command=_show_bgp_ipv4_uni_neigh,
                                       peer_received_command=_show_bgp_ipv4_uni_neigh_received_routes),
                CiscoCommandBgpIPv6Sum('show bgp ipv6 unicast summary',
                                       peer_address_command=_show_bgp_ipv6_uni_neigh,
                                       peer_received_command=_show_bgp_ipv6_uni_neigh_received_routes),
                _show_bgp_ipv4_uni_neigh,
                _show_bgp_ipv6_uni_neigh,
                _show_bgp_ipv4_uni_neigh_received_routes,
                _show_bgp_ipv6_uni_neigh_received_routes,
                _show_bgp_ipv4_uni_neigh_advertised,
                _show_bgp_ipv6_uni_neigh_advertised,
                CiscoShowBgpIPv4Uni(),
		CiscoShowBgpIPv6Uni(),
                _graph_show_bgp_ipv4_uni,
		_graph_show_bgp_ipv6_uni,
793 794
		ulgmodel.TextCommand('ping %s',[ulgmodel.IPv64AddressParameter()]),
		ulgmodel.TextCommand('traceroute %s',[ulgmodel.IPv64AddressParameter()]),
795 796 797
                ]


798 799 800
    def __init__(self, host, user, password, port=22, commands=None, enable_bgp=True, asn='My ASN', name=None, acl=None):
        ulgmodel.RemoteRouter.__init__(self,acl=acl)

801 802 803 804
        self.setHost(host)
        self.setPort(port)
        self.setUser(user)
        self.setPassword(password)
805 806
        self.bgp_ipv4_peers = []
        self.bgp_ipv6_peers = []
807
        if(name):
Tomas Hlavacek's avatar
Tomas Hlavacek committed
808
            self.setName(name)
809 810
        else:
            self.setName(host)
811
	self.setASN(asn)
812 813 814 815 816 817 818

        if enable_bgp:
            if(defaults.rescan_on_display):
                self.rescanHook()
            else:
                self.loadBGPPeers()

819 820 821 822 823 824 825
	    rid = hashlib.md5(self.getName()).hexdigest()

	    self.bgp4select = ulgmodel.CommonSelectionParameter(rid+"bgp4",[tuple((p,p,)) for p in self.getBGPIPv4Peers()],
                                                 name=defaults.STRING_IPADDRESS)
	    self.bgp6select = ulgmodel.CommonSelectionParameter(rid+"bgp6",[tuple((p,p,)) for p in self.getBGPIPv6Peers()],
                                                 name=defaults.STRING_IPADDRESS)

826 827 828
        if(commands):
            self.setCommands(commands)
        else:
829 830
            self.setCommands(self._getDefaultCommands())

831 832 833 834

    def getForkNeeded(self):
        return True

835
    def rescanBGPPeers(self,command,regexp):
836
        table = self.runRawSyncCommand(command)
837 838 839

        peers = []
        rlr = re.compile(regexp)
840 841
	lines = normalizeBGPIPv6SumSplitLines(str.splitlines(table))

842 843 844 845 846 847 848 849
        for tl in lines:
            rlrm = rlr.match(tl)
            if(rlrm):
                peers.append(rlrm.group(1))

        return peers

    def rescanBGPIPv4Peers(self):
850
        self.bgp_ipv4_peers = self.rescanBGPPeers(RESCAN_BGP_IPv4_COMMAND,BGP_IPV46_TABLE_LINE_REGEXP)
851 852

    def rescanBGPIPv6Peers(self):
853
        self.bgp_ipv6_peers = self.rescanBGPPeers(RESCAN_BGP_IPv6_COMMAND,BGP_IPV46_TABLE_LINE_REGEXP)
854 855 856 857 858 859 860 861 862 863 864 865
        
    def rescanHook(self):
        self.rescanBGPIPv4Peers()
        self.rescanBGPIPv6Peers()
        self.saveBGPPeers()

    def getBGPIPv4Peers(self):
        return self.bgp_ipv4_peers

    def getBGPIPv6Peers(self):
        return self.bgp_ipv6_peers

866 867 868 869 870 871
    def getBGPIPv4Select(self):
        return self.bgp4select

    def getBGPIPv6Select(self):
        return self.bgp6select

872 873 874 875
    def saveBGPPeers(self):
        key4 = self.getHost() + self.PS_KEY_BGPV4
        key6 = self.getHost() + self.PS_KEY_BGPV6

876
        ps = ulgmodel.loadPersistentStorage()
877 878 879 880 881 882 883 884
        ps.set(key4,self.getBGPIPv4Peers())
        ps.set(key6,self.getBGPIPv6Peers())
        ps.save()

    def loadBGPPeers(self):
        key4 = self.getHost() + self.PS_KEY_BGPV4
        key6 = self.getHost() + self.PS_KEY_BGPV6

885
        ps = ulgmodel.loadPersistentStorage()
886 887 888 889 890 891
        self.bgp_ipv4_peers = ps.get(key4)
        self.bgp_ipv6_peers = ps.get(key6)

        if(not self.getBGPIPv4Peers()) or (not self.getBGPIPv6Peers()):
            self.rescanHook()

892
    def runRawCommand(self,command,outfile):
893 894 895
        skiplines=1

	# connect
896
        c=defaults.bin_ssh+' -p'+str(self.getPort())+' '+str(self.getUser())+'@'+self.getHost()
897
        s=pexpect.spawn(c,timeout=defaults.timeout)
898 899 900

##      Debug logging
#	s.logfile = open('/tmp/ulgcisco.log', 'w')
901

902 903
	y=0
	p=0
904
        # handle ssh
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
	while True:
		i=s.expect([STRING_EXPECT_SSH_NEWKEY,STRING_EXPECT_PASSWORD,
			    STRING_EXPECT_SHELL_PROMPT_REGEXP,pexpect.EOF,pexpect.TIMEOUT])
		if(i==0):
			if(y>1):
				raise Exception("pexpect session failed: Can not save SSH key.")

			s.sendline('yes')
			y+=1

		elif(i==1):
			if(p>1):
				raise Exception("pexpect session failed: Password not accepted.")

			s.sendline(self.password)
			p+=1
		elif(i==2): # prompt
			break
		elif(i==3):
			raise Exception("pexpect session failed: Remote server disconnected.")
		elif(i==4):
			raise Exception("pexpect session failed: Connection timeout.")
		else:
			raise Exception("pexpect session failed: Unknown error. last output: "+s.before)

	s.sendline('terminal length 0')
	s.expect(['\n',pexpect.EOF,pexpect.TIMEOUT])

	s.sendline('terminal width 0')
	s.expect(['\n',pexpect.EOF,pexpect.TIMEOUT])

	l=0
	s.sendline(command)
	while True:
		i=s.expect([STRING_EXPECT_SHELL_PROMPT_REGEXP,'\n',pexpect.EOF,pexpect.TIMEOUT])
		if(i==0): # prompt
			s.sendline(STRING_COMMAND_LOGOUT)
			s.expect(['\n',pexpect.EOF,pexpect.TIMEOUT])
			break
		elif(i==1): # anything to capture
			if(l>=skiplines):
				outfile.write(s.before + "\n")
			l+=1
		elif(i==2):
			raise Exception("pexpect session failed: Remote server disconnected.")
		elif(i==3):
			raise Exception("pexpect session failed: Connection timeout.")
		else:
			raise Exception("pexpect session failed: Unknown error. last output: "+s.before)