Amit Bhanushali
Amit Bhanushali

Reputation: 77

tcl script for ping not working

I have a tcl code which invokes ping command and returns its response , code is as follows

proc ping-igp {} {
foreach i {
127.0.0.1
200.200.200.1
} {
if { [regexp "0% loss"  [eval exec "ping $i -n 1" ]]} { puts “$i”} else { puts “$i  failed” }
}
}

But while executing it I get o/p as follows,

% proc ping-igp {} {
foreach i {
127.0.0.1
200.200.200.1
} {
if { [regexp "0% loss"  [eval exec "ping $i -n 1" ]]} { puts "$i"} else { puts "
$i  failed" }
}
}
% ping-igp
"127.0.0.1"
Pinging 200.200.200.1 with 32 bytes of data:
Request timed out.
Ping statistics for 200.200.200.1:
Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
child process exited abnormally
%

I want to know when I'm unable to ping 200.200.200.1 why my code doesn't process else clause and give o/p " 200.200.200.1 failed " in end . I'm matching "0% loss"

Many Thanx.

Upvotes: 1

Views: 4758

Answers (3)

ddavtian
ddavtian

Reputation: 1361

proc ping_device { router ip_address max_tries } {
    set tries 0    
    while {$tries <= $max_tries} {
        if {[catch {exec ping $ip_address -c 5} result]} { 
            puts "Ping command failed on Linux machine"
            incr tries
            if {$tries > $max_tries} {
                ats_log -error "Giving up on ping"
                return 0
            }
            continue
        } 
        if {[regexp {([1-5]+) *[packets]? *received} $result - packets]} { 
            puts "Able to ping device: $router and\
                           successfully received $packets packets"
            return 1
        } else { 
            puts "Not Able to ping device: $router"
            incr tries
        }
    }
    puts "Giving up on ping, returning 0"
    return 0
}

Upvotes: 0

Hai Vu
Hai Vu

Reputation: 40733

Here is something I created for my own use: a ping proc for Tcl. It is not perfect, but works:

package require Tclx; # Needed for lassign and wait

# Pings a host and returns 0 if OK, non-zero otherwise
proc ping {host} {
    # TODO: Use different command for Windows OS
    set childPid [exec ping -c 1 $host > /dev/null 2>@1 &]
    lassign [wait $childPid] pid howItEnded exitCode
    return $exitCode
}

# Test the ping proc
set hostList {google.com 10.0.0.99 foo.bar}
foreach host $hostList {
    set code [ping $host]
    puts [format "%4d %s" $code $host]
}

Sample output:

$ tclsh ping_test.tcl 
   0 google.com
   2 10.0.0.99
  68 foo.bar

Upvotes: 1

TrojanName
TrojanName

Reputation: 5365

You need to catch the exec call to ping, in case it returns an error. Here's your code modified to use catch.

proc ping-igp {} {
foreach i {
  127.0.0.1
  200.200.200.1
} {
if {[catch {exec ping $i -n 1} result]} { set result 0 } 
if { [regexp "0% loss"  $result]} { puts "$i"} else { puts "$i  failed" }
}
}

Running it now gives:

% ping-igp
127.0.0.1
200.200.200.1  failed
%

Upvotes: 3

Related Questions