Getting avg-rtt of ping

Hello guys,
I am sorry for this stupid question but I tried for some hours and do not get it.
I wrote script, which should get the “avg-rtt” of a ping but it does not work.
This is my Code:

:local a [:ping 1.1.1.1 count=3 as-value]
:local avg ($a->"avg-rtt");
:put $avg;

If I run this in the terminal I get nothing as output, but if I ping in the terminal I can clearly see that there is an avg-rtt output.
What am I doing wrong?
I appreciate every advice :slight_smile:

You do not specify RouterOS version, probably v7 because v6 do not have as-value for ping.

But on v7 as-value still no return stats, but only single pings (you can calc it sum all 3 times and divide by 3)…

Alternative for v6 (work also on v7)

:put ([/tool flood-ping [:resolve "www.mikrotik.com"] count=3 as-value]->"avg-rtt")

For v7

{
:local test [:ping [:resolve "www.mikrotik.com"] count=3 as-value]
:local avg 0
:foreach try in=$test do={:set avg ($avg + ($try->"time"))}
:set avg ($avg / [:len $test])
:put $avg
}

The first method provide more human readable values (50), the second not so easy:
00:00:00.050994
Trimming the string, can be extracted only the interested part:

{
:local test [:ping [:resolve "www.mikrotik.com"] count=3 as-value]
:local avg 0
:foreach try in=$test do={:set avg ($avg + ($try->"time"))}
:set avg ($avg / [:len $test])
:local out  ""
:set out ( ([:tonum [:pick $avg 6   8]] * 1000000) + \
           ([:tonum [:pick $avg 10 12]] *    1000) + \
            [:tonum [:pick $avg 12 15]]              \
         )
:put "$($out / 1000)ms"
}

That is exactly what I searched for. Thank you very much.
It is also very usefull to know how to trimm it.
If I wanted to get packet-loss, should I do it in a similar way or is there something smarter?
Just that you know if it does matter, I would need the routing-table element in the ping, so flood-ping is not an option, if I understood it correctly.

Everytime the “status” filed is one string, is one failed ping (because containing the error code:
{
:local test [:ping [:resolve “www.mikrotik.com”] count=3 as-value]
:local avg 0
:local fails 0
:local min 00:00:10
:local max 00:00:00
:foreach try in=$test do={
:set avg ($avg + ($try->“time”))
:if ([:typeof ($try->“status”)] = “str”) do={
:set fails ($fails + 1)
} else={
:if (($try->“time”) < $min) do={:set min ($try->“time”)}
:if (($try->“time”) > $max) do={:set max ($try->“time”)}
}
}
:set avg ($avg / [:len $test])
:set avg [:pick $avg 0 15]
:put “sent=$[:len $test] received=$([:len $test]-$fails) packet-loss=$(($fails*100)/[:len $test])% min-rtt=$min avg-rtt=$avg max-rtt=$max”
}
Percentage is hard to calc precisely because MikroTik scripting do not support decimals.
Adding decimals multiply the number by 100 and splitting with :put is possible, but on this case is excessive…

Your script is more then I even needed. Thank you very much for your time and effort :slight_smile:

In case it helps, Netwatch ICMP probe offers built-in rtt-avg timer:
https://help.mikrotik.com/docs/display/ROS/Netwatch#Netwatch-ICMPprobeoptions

LOL. I was going to suggest this:

And a script can trigger based on the result, or even on just on “test” itself. The new netwatch in recent V7 is quite nice feature…