cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
498
Views
0
Helpful
3
Replies

script tclsh for give ip from multiple vrf, not works :(

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:

3 Replies 3

marce1000
VIP
VIP

 

 -  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! '

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.

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)#

 

Review Cisco Networking for a $25 gift card