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:
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.
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.
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)#
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide