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.
You need to catch the exec call to ping, in case it returns an error. Here’s your code modified to use catch.
Running it now gives: