07-18-2012 04:31 PM
Hey Everyone!
I need to run a Tcl script against a bunch of IP's and I would like the output to come back with the nodes hostnames as oppsed to just the IP (it will make it easier to read and less work trying to manually reverse lookup these things later)
So far I have created static hostname-to-address mappings and a very basic Tcl script but this isn't working out so well...
Basically this allows me to ping by name but it only comes back with the IP. With the large number of IP's this will be done with it would be a lot easier if we could return with a name instead, is this possible?
Here is a sample of what I've done:
Set the hostnames
ip host host1 192.168.1.1
ip host host2 192.168.1.2
ip host host3 192.168.1.3
Run a Tcl ping script
R1#tclsh
R1(tcl)#foreach addr {
+>host1
+>host2
+>host3
+>} {ping vrf Mgmt-intf $addr repe 2}
Type escape sequence to abort.
Sending 2, 100-byte ICMP Echos to 192.168.1.1, timeout is 2 seconds:
!!
Success rate is 100 percent (2/2), round-trip min/avg/max = 1/1/2 ms
Type escape sequence to abort.
Sending 2, 100-byte ICMP Echos to 192.168.1.2, timeout is 2 seconds:
!!
Success rate is 100 percent (2/2), round-trip min/avg/max = 1/1/2 ms
Type escape sequence to abort.
Sending 2, 100-byte ICMP Echos to 192,168.1.3 timeout is 2 seconds:
!!
Success rate is 100 percent (2/2), round-trip min/avg/max = 1/1/2 ms
So I'm wondering if it would be possible to see the hostname returned as opposed to the IP address shown above?
Thank you!
Solved! Go to Solution.
07-19-2012 08:17 AM
Something like this should work:
set output [exec "show hosts | include IP"]
array set hostname [list]
array set ipaddr [list]
foreach line [split $output "\n"] {
set hostname([lindex $line end]) [lindex $line 0]
set ipaddr([lindex $line 0]) [lindex $line end]
}
foreach addr {host 1 host2 host3} {
set output [exec "ping vrf Mgmt-intf $addr repe 2"]
set ip $ipaddr($addr)
regsub -all "$ip" $output $hostname($ip) output
puts $output
}
07-18-2012 06:35 PM
You could call "show hosts" and cache the results. Then use a regsub to replace the IPs with the hostnames in the ping output. In terms of a command line option to ping, there isn;t one.
07-18-2012 10:27 PM
Hey Joseph that sounds cool, however its beyond my Tcl knowledge can you help get me going in the right direction?
Sent from Cisco Technical Support iPhone App
07-19-2012 08:17 AM
Something like this should work:
set output [exec "show hosts | include IP"]
array set hostname [list]
array set ipaddr [list]
foreach line [split $output "\n"] {
set hostname([lindex $line end]) [lindex $line 0]
set ipaddr([lindex $line 0]) [lindex $line end]
}
foreach addr {host 1 host2 host3} {
set output [exec "ping vrf Mgmt-intf $addr repe 2"]
set ip $ipaddr($addr)
regsub -all "$ip" $output $hostname($ip) output
puts $output
}
07-19-2012 09:50 AM
Well sir your snippet of code worked flawlessly, you're the man! Thank you
Sent from Cisco Technical Support iPhone App
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