Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
parsing output buffer

Posted by priyadharshini (priyadharshini), 5 June 2008
Hi,
        I need to write a tcl proc to parse output buffer of routers.

This is the content of output buffer
rtrA#show ip bgp
BGP table version is 9, local router ID is 172.17.1.1
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal
Origin codes: i - IGP, e - EGP, ? - incomplete
Network Next Hop Metric LocPrf Weight Path
*> 172.16.0.0/24 10.1.1.2 0 0 2 i
*> 172.16.1.0/24 10.1.1.2 0 0 2 i
*> 172.16.2.0/24 10.1.1.2 0 0 2 i
*> 172.16.3.0/24 10.1.1.2 0 0 2 i
*> 198.16.0.0 10.1.1.2 850 2 3 i
*> 198.16.1.0 10.1.1.2 850 2 3 i
*> 198.16.2.0 10.1.1.2 850 2 3 i
*> 198.16.3.0 10.1.1.2 850 2 3 I

the proc should check whether the following lines are present in the output buffer or not
*> 172.16.1.0/24 10.1.1.2 0 0 2 i
*> 172.16.2.0/24 10.1.1.2 0 0 2 i
*> 198.16.2.0 10.1.1.2 850 2 3 i
*> 198.16.3.0 10.1.1.2 850 2 3 I

Can any one help me?

Thank you

Posted by admin (Graham Ellis), 5 June 2008
Yes - we can help - how far have you got so far?  Have you got the information in to your Tcl (expect?) program and you're looking for assistance with the data checking, or are you having trouble getting the data in?

Please post up some code to show us which part of the job is a problem, and describe how it fails.   Thanks

Posted by priyadharshini (priyadharshini), 5 June 2008
Thanks for your quick reply.

This is the proc i wrote to check one entry in the routing table.

#! /usr/bin/tclsh
proc parseNRT { tcid tcdesc code network metric nexthop interface } {
global cod
global des
global nhop
global met
global iface
global status
global match
global routefound
set routefound 0
set match 1
set in [ open "rt.txt" "r"]
set countline 0
while {[gets $in line] != -1} {

incr countline
if { $countline < 7 } { continue }
set des [lindex $line 1]
if { $des != $network } { continue }
set line [string trim $line "^M"]
set temp [llength $line]
set cod [lindex $line 0]
if { $temp == 4 } {
set met [lindex $line 2]
set nhop ""
set iface [lindex $line 3]
}
if { $temp == 5 } {
set nhop [lindex $line 2]
set iface [lindex $line 4]
set met [lindex $line 3]
}
if { $des != $network } { continue }
set routefound 1
if {![string match $cod $code] } { set match 0
puts "match1" }
if {![string match $met $metric] } { set match 0
puts "match2" }
if {![string match $nhop $nexthop] } { set match 0
puts match3 }
if {![string match $interface $iface] } { set match 0
puts match4 }
if { $match == 0} {
 set status "FAILURE"
} else {set status "SUCCESS" }
}
close $in
if { !$routefound } { set status "route not found" }
set reportentry [open "rf.txt" "a"]
puts $reportentry "$tcid \t $tcdesc \t $status"
close $reportentry
}



this is how 'm calling the the proc in some other tcl file


set tempfile [ open "rt.txt" "w" ]
 puts $tempfile [ sendcommand $host $port "$showcmd ip rip database" ]
 close $tempfile
 parseNRT $tcid $tcdesc Rc $RTR1_2nw 1 "" [join [lindex $RTR1_2intf 0][lindex $RTR1_2intf 1]]

In this case the content of output buffer is

show ip rip database^M

^M^M

Codes: R - RIP, Rc - RIP connected, Rs - RIP static, K - Kernel,^M^M

      C - Connected, S - Static, O - OSPF, I - IS-IS, B - BGP^M^M

^M^M

  Network            Next Hop         Metric From            If     Time^M^M

Rc 3.3.3.3/32                               1                 3     ^M^M

R  10.0.0.0/8         20.1.1.2              2 20.1.1.2        wan    02:51^M^M

Rc 20.1.1.0/24                              1                 wan   ^M^M

Rc 50.1.1.0/24                              1                 ethernet0/2^M^M

Rc 60.1.1.0/24                              1                 ethernet0/1^M^M


but in some times i need to check the presence multiple lines where as the proc parseNRT can check be used to check single line

Thank you,



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2021: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho