Ahmad_R
Ahmad_R

Reputation: 62

sending "ping" output to a variable

I am trying to ping some ip addresses in my router. I use this code:

for {set n 0} {$n < 10} {incr n} {puts [exec "ping 199.99.$n.1]}

but this will show the output. the issue is that I don't want to see the output. I would like to send that output into another variable and the search the content of variable with "regexp" and get the result, and do the rest of the story. but I don't know how I can do that.

Upvotes: 3

Views: 4902

Answers (3)

It is very important to use ping -c1 <IP address> , otherwise the script will never end as the ping process never ends :)

My code uses an array of results of every IP

for {set i 2 } {$i < 10} {incr i} {

catch {if {[regexp {bytes from} [exec ping -c1 192.168.12.$i]]} { 
          set flag "reachable" 
        } else     { set flag "not reachable"}

set  result(192.168.12.$i) $flag
} 
}
parray result

OUTPUT :

result(192.168.12.2) = reachable

result(192.168.12.3) = reachable

result(192.168.12.5) = reachable

result(192.168.12.6) = reachable

result(192.168.12.7) = reachable

result(192.168.12.9) = reachable

Instead of storing and manipulating , I used regexp .

Upvotes: 0

potrzebie
potrzebie

Reputation: 1798

Use the set command. The puts command prints it's argument.

set pingOutput [exec ping "199.99.$n.1"]

Or append if you want all IP's results in one variable.

set allPingOutput ""
for {set n 0} {$n < 10} {incr n} {
    append allPingOutput [exec ping "199.99.$n.1"]
}

Upvotes: 9

d g
d g

Reputation: 1604

Try calling the ping with the -c flag: ping -c 1 10.0.1.1

Not sure how to do it in tcl but in php for example:

Upvotes: 0

Related Questions