cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
2919
Views
0
Helpful
4
Replies

Tcl ping script respond with hostname?

steve.dibias
Level 1
Level 1

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!

1 Accepted Solution

Accepted Solutions

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

}

View solution in original post

4 Replies 4

Joe Clarke
Cisco Employee
Cisco Employee

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.

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

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

}

Well sir your snippet of code worked flawlessly, you're the man! Thank you

Sent from Cisco Technical Support iPhone App

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: