script tclsh for give ip from multiple vrf, not works :(
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2023 08:24 AM - edited 08-23-2023 08:29 AM
Hi community, can someone help me? I can't figure out why it doesn't return the split ip's for each vrf when I launch getipfor_vrfs, also when i even when I specify the vrfs under set vrfs ecc..
Thank you so much!
reported below :
Statement of VRFs
set vrfs {
"VRF1"
"VRF2"
"VRF3"
# Add other VRF needed
}
Function to get the IPs for each VRF
proc getipfor_vrfs {} { global vrfs
# Cycle on each VRF
foreach vrf $vrfs {
# Command to get the IPs for the current VRF
set command "show ip vrf $vrf"
# Run the command on the CLI
set output [ios_config $command]
# Parsing the output to get the IPs
set lines [split $output "\n"]
set ips {}
foreach line $lines {
if {[regexp {([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/[0-9]+)} $line match ip] } {
lappen ips $ip
}
}
# Print the IPs for the current VRF
puts "IP for VRF $vrf:"
foreach ip $ips {
puts $ip
}
}
}
Running the getipfor_vrfs function
getipfor_vrfs
MPLS# get_ip_for_vrfs
IP for VRF vrf:
IP for VRF vrf:
IP for VRF vrf:
IP for VRF vrf:
- Labels:
-
Network Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2023 09:57 AM
- The getipfor_vrfs function is not correctly looping over the vrfs list. Try the following
proc getipfor_vrfs {} { global vrfs
# Cycle on each VRF
foreach vrf [lsort $vrfs] {
# Command to get the IPs for the current VRF
set command "show ip vrf $vrf"
# Run the command on the CLI
set output [ios_config $command]
# Parsing the output to get the IPs
set lines [split $output "\n"]
set ips {}
foreach line $lines {
if {[regexp {([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/[0-9]+)} $line match ip] } {
lappend ips $ip
}
}
# Print the IPs for the current VRF
puts "IP for VRF $vrf:"
foreach ip $ips {
puts $ip
}
}
}
The main difference is that the foreach loop now iterates over the sorted vrfs list. This ensures that the IPs for each VRF are printed out on separate lines.
M.
-- Each morning when I wake up and look into the mirror I always say ' Why am I so brilliant ? '
When the mirror will then always repond to me with ' The only thing that exceeds your brilliance is your beauty! '
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-24-2023 01:15 AM
Hi thank you for your support!
I'm new to scripting, but I can't get it to start. i'm doing this test for a service provider on production router. when i run the scripts inside tclsh nothing happens. but do I need to save it to a file with .tcls extension in flash memory?
I have attached screenshot, I hope I explained myself well.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2023 02:04 PM - edited 08-26-2023 02:06 PM
I updated some of lines and this was saved to bootflash on the router as vrf.tcl
proc getipfor_vrfs {} {
set vrfs [list mpls metro-ethernet]
foreach vrf [lsort $vrfs] {
set result [exec sh ip route vrf $vrf | inc ^L]
set lines [split $result "\n"]
puts "IP for VRF $vrf:"
foreach line $lines {
if {[regexp {([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/[0-9]+)} $line match ip] } {
puts $ip
}
}
}
}
to run the script on the router CLI:
MPLS#
MPLS#tclsh
MPLS(tcl)#source bootflash:vrf.tcl
MPLS(tcl)#getipfor_vrfs
IP for VRF metro-ethernet:
10.64.34.1/32
10.241.59.1/32
172.27.1.161/32
192.168.55.5/32
IP for VRF mpls:
10.64.34.1/32
10.64.64.254/32
10.241.59.1/32
172.27.1.1/32
192.168.55.5/32
MPLS(tcl)#
