Cisco Support Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Announcements

Welcome to Cisco Support Community. We would love to have your feedback.

For an introduction to the new site, click here. And see here for current known issues.

New Member

Tcl ping script respond with hostname?

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!

Everyone's tags (5)
1 ACCEPTED SOLUTION

Accepted Solutions
Cisco Employee

Re: Tcl ping script respond with hostname?

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

}

4 REPLIES
Cisco Employee

Tcl ping script respond with hostname?

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.

New Member

Re: Tcl ping script respond with hostname?

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

Cisco Employee

Re: Tcl ping script respond with hostname?

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

}

New Member

Re: Tcl ping script respond with hostname?

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

Sent from Cisco Technical Support iPhone App

1501
Views
0
Helpful
4
Replies
CreatePlease login to create content