cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Bookmark
|
Subscribe
|
2080
Views
0
Helpful
1
Replies

Array in TCL

astoffel1
Level 1
Level 1

So I am starting to write a script that hopefully could be ran on multiple routers at once. The basic changes are Ip address and the minor one I can handle after this problem is solved. The script below is suppose to match by hostname and run the rest of the scipt based on if the hostnames match based on the current router. After running on Router B the output is 

" Router B with a Interface address of 200.2.2.0 and a Tunnel ip of 1.1.12.1 and netmask 1.1.11.2"

::cisco::eem::event_register_none


namespace import ::cisco::eem::*
namespace import ::cisco::lib::*


if [catch {cli_open} result] {
error $result $errorInfo
} else {
array set cli1 $result
}

array set Router_Info {
Router_A 1.1.1.0 1.1.12.1 1.1.11.2
Router_B 200.2.2.0 99.2.22.1 2.2.22.23
}


if [catch {cli_exec $cli1(fd) "enable"} _cli_result] {
error $_cli_result $errorInfo
}

if [catch {cli_exec $cli1(fd) "show run | inc hostname"} _hostname] {
error $_hostname $errorInfo
}
set _ip_scapper [regexp {hostname ([a-zA-Z0-9_]+)} "$_hostname" match hostname]
action_syslog msg "$hostname"

foreach {Local_Hostname Interface_IP Tunnel_IP NETMASK} [ array get Router_Info] {
action_syslog msg " $Local_Hostname $Interface_IP $Tunnel_IP $NETMASK"

if { $Local_Hostname == $hostname } {
set Interface_IP_Address $Interface_IP
set Tunnel_IP_Address $Tunnel_IP
set NETMASK_IP $NETMASK
break
} else {
}
}

action_syslog msg "$hostname with a Interface address of $Interface_IP_Address and a Tunnel ip of $Tunnel_IP_Address and netmask $NETMASK_IP"

catch {cli_close $cli1(fd) $cli1(tty_id)} result

1 Accepted Solution

Accepted Solutions

Dan Frey
Cisco Employee
Cisco Employee

Arrays can return the elements out of order.   List data structure referenced by index can return the data in the order required.  Hostname can be captured without opening cli in the script.

set host [info hostname]
set Router_Info [list \
Router_A 1.1.1.0 1.1.12.1 1.1.11.2 \
Router_B 200.2.2.0 99.2.22.1 2.2.22.23 \
]
set element 0
foreach item $Router_Info {
if {[string equal $host $item]} {
puts "[lindex $Router_Info $element] [lindex $Router_Info [expr $element + 1]] [lindex $Router_Info [expr $element + 2]] [lindex $Router_Info [expr $element + 3]]"
}
incr element
}

View solution in original post

1 Reply 1

Dan Frey
Cisco Employee
Cisco Employee

Arrays can return the elements out of order.   List data structure referenced by index can return the data in the order required.  Hostname can be captured without opening cli in the script.

set host [info hostname]
set Router_Info [list \
Router_A 1.1.1.0 1.1.12.1 1.1.11.2 \
Router_B 200.2.2.0 99.2.22.1 2.2.22.23 \
]
set element 0
foreach item $Router_Info {
if {[string equal $host $item]} {
puts "[lindex $Router_Info $element] [lindex $Router_Info [expr $element + 1]] [lindex $Router_Info [expr $element + 2]] [lindex $Router_Info [expr $element + 3]]"
}
incr element
}