Script for pinging an IP for 24/7

Hello everyone
I have few VPN clients (PPTPs and IKEV2) on my router which they are always working fine except when no user on the network send or receive any packet so their servers start to drop the connection and router will reconnect and this happens over and over again to solve this I want to use a script or scripts to ping different IPs like 9.9.9.9 or 1.1.1.1 all the time for all of the VPN interfaces and also IKEV2 which is the main route on the router, (right now I’m doing this by creating few VMs on the server and inside each they are pinging 9.9.9.9 or 1.1.1.1) so this way there would be no need for VMs and also the problem will be solved.
Any help will be highly appreciated.

https://help.mikrotik.com/docs/display/ROS/Netwatch

Tools>Netwatch in winbox/webfig can do this. There are scripts in Netwatch the control what to do if the ping fails (or come back). From the CLI, looks like this:

/tool/netwatch/add host=9.9.9.9 down-script={:log error "ping test failed"} up-script={:log info "ping test working"}

The

:log

in “up-script” or “down-script” could be replace with /tool/email or whatever script you’d like.

@Ammo

how you will do ping from src-add, Is there any chance to be created from particular VRF? And also can be added into specific logging action?

Yeah, there is no interface selection for netwatch. So unfortunately you cannot directly set something like a VRF interface – it just follows the route selection based on the dst-addr being ping’ed. I use PBR (IP>Routes>Rules in winbox) to select the routing table for what’s being ping’ed as a test.

You can use normal “ping”, which allows selecting the VRF, in a System>Schedule script. The only problem with using “ping” in a scheduled script, is it’s more code to check the results, while netwatch is pretty simple. See “ping” will output an array of values from the ping that have to be checked to know it worked.

But if all you want is a ping running, you can ignore the results and just run ping from the System>Schedule script. If you want to know if ping worked fromed from a script, the basic approach using ping involves using “as-value” to be able to store the results in variable.

 
 :global lastping [/ping address=8.8.8.8 count=1 as-value]
:put $x
  # .id=*0;host=8.8.8.8;seq=0;size=56;time=00:00:00.002854;ttl=117
:put ($x->"ttl")
  # 117

If you log from a script (e.g. :log ) , the “topic” is always “script”, so control the log dest/etc based on the topic ~= “script”.

Thank you @Ammo, but I want to send it with different interfaces. for example: like one with PPTP1 another one with PPTP2 and one with IPsec/IKEV2 and one with L2TP4.

Remember everytime to set "count" parameter or, for each ping launched on scheduler, one process is keeped running forever on background
"as-value" (EDIT: on ping command)ª do not exist on 6.x,x, is added on v7.x.x
Without specify for what version, only confusion.

On 6.46.8 the "ping" give back only the number of successfully "ping".

6.6.6.6 is unreachable

[@_@] > :put [:ping 6.6.6.6 count=3]
0
On 7.1.1 the result is "one array of array" of values if as-value is specified
(one "array" for each ping)

6.6.6.6 is unreachable

[@_@] > :put [:ping 6.6.6.6 count=3 as-value]
.id=*0;seq=0;status=no route to host;.id=*1;seq=1;status=no route to host;.id=*2;seq=2;status=no route to host
ping parameters, on 6.x just press <?> or after you write "ping " on terminal, some descriptions are missing, but are intuitive on 7.x.x press or for the same (v7 also suggest discovered MAC addresses) [@_@] > ping <?>
Send ICMP Echo packets. Repeat after given time interval.

-- IP address arp-ping -- count -- Number of packets do-not-fragment -- Do not fragment ping packets dscp -- interface -- interval -- Delay between messages routing-table -- size -- Packet size src-address -- Source IP address to use when pinging ttl -- Time to live

[@@] > ping
count interface routing-table src-address address
arp-ping dscp interval size ttl
[@
@] > ping

-- IP address arp-ping -- as-value -- count -- Number of packets do-not-fragment -- Do not fragment ping packets dscp -- interface -- interval -- Delay between messages size -- Packet size src-address -- Source IP address to use when pinging ttl -- Time to live vrf --

Press F1 for general console usage help
[@_@] > ping
00:0C:42:xx:xx:xx CC:2D:E0:xx:xx:x address arp-ping count dscp interface interval size src-address ttl vrf
Both versions accepts "interface parameter:

pptp-noexit do not acept routed connections, but the traffic still pass the link

[@_@] > ping 1.1.1.1 count=3 interface=
SEQ HOST SIZE TTL TIME STATUS
0 1.1.1.1 timeout
1 1.1.1.1 timeout
2 1.1.1.1 timeout
sent=3 received=0 packet-loss=100%

ether8 is unused/unconnected

[@_@] > :put [:ping 1.1.1.1 count=3 interface=ether8 as-value]
.id=*0;host=1.1.1.1;seq=0;status=timeout;.id=*1;host=1.1.1.1;seq=1;status=timeout;.id=*2;host=1.1.1.1;seq=2;status=timeout
Note:
ª thanks to Jotne

Just a small correction.

as-value do exist on 6.x, but not in ping.

This is from 6.48.4

:put [/interface vlan print as-value ]
.id=*c;arp=enabled;interface=Bridge1;mtu=1500;name=VLAN20;vlan-id=20

The topic of this thread is “ping”, it is logical that we are only talking about that command… :unamused: :wink: :laughing:

On more complex way I have “created” as-value for ping on v6:
http://forum.mikrotik.com/t/how-to-get-the-avg-rtt-value-of-command-ping/63496/5
is valid for both normal and MAC ping.

/ping can be script with the interface name to use, or even VRF to use. Under V6, you can add routing-table= or interface= options to control it.

> /ping address=8.8.8.8 count=1 interface=ether1 <tab>
arp-ping         dscp       interval       size         ttl
do-not-fragment  interface  routing-table  src-address

The tab key shows what options are available.

Under V7, they have a specific vrf= option (guessing it works but don’t have a VRF setup to check):

 
 > /ping address=8.8.8.8 count=1 
arp-ping  as-value  do-not-fragment  dscp  interface  interval  size  src-address  ttl  vrf

So should be able to ping anything from anything, but you have to use “as-value” with ping to store the results (and add code to do logging etc) in a /system/scheduler script.

I do know, but not all read all detail :wink:

@Jotne ,@rextended

ill ask again , is there any script even on v7, that i can ping from VRF with specific src-add?

As far as I know, no.

Hi Jotne,

is there any chance that e can do similar like @Amm0 posted, but also i want so to the latency on the log

disregard the last