# Script for pinging an IP for 24/7

**URL:** <https://forum.mikrotik.com/t/script-for-pinging-an-ip-for-24-7/155567>\
**Category:** Scripting\
**Created:** [February 4, 2022, 2:37am UTC](https://forum.mikrotik.com/t/script-for-pinging-an-ip-for-24-7/155567 "2022-02-04T02:37:14Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![jaxed8](https://forum.mikrotik.com/letter_avatar/jaxed8/32/6_449c533f586939b6e5f246632a46421f.png) [@jaxed8](https://forum.mikrotik.com/u/jaxed8)\
**Post date:** [February 4, 2022, 2:37am UTC](https://forum.mikrotik.com/t/script-for-pinging-an-ip-for-24-7/155567/1 "2022-02-04T02:37:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Amm0](https://forum.mikrotik.com/user_avatar/forum.mikrotik.com/amm0/32/1630_2.png) [@Amm0](https://forum.mikrotik.com/u/Amm0)\
**Post date:** [February 4, 2022, 3:12am UTC](https://forum.mikrotik.com/t/script-for-pinging-an-ip-for-24-7/155567/2 "2022-02-04T03:12:49Z")

</div>

[https://help.mikrotik.com/docs/display/ROS/Netwatch](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:

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

```

The

```plaintext
:log

```

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

---

<div class="post-metadata">

**Author:** ![nichky](https://forum.mikrotik.com/user_avatar/forum.mikrotik.com/nichky/32/1516_2.png) [@nichky](https://forum.mikrotik.com/u/nichky)\
**Post date:** [February 4, 2022, 8:18am UTC](https://forum.mikrotik.com/t/script-for-pinging-an-ip-for-24-7/155567/3 "2022-02-04T08:18:22Z")

</div>

@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?

---

<div class="post-metadata">

**Author:** ![Amm0](https://forum.mikrotik.com/user_avatar/forum.mikrotik.com/amm0/32/1630_2.png) [@Amm0](https://forum.mikrotik.com/u/Amm0)\
**Post date:** [February 4, 2022, 1:49pm UTC](https://forum.mikrotik.com/t/script-for-pinging-an-ip-for-24-7/155567/4 "2022-02-04T13:49:51Z")

</div>

> [@](#):
>
> 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.

```plaintext
 
 :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

```

---

<div class="post-metadata">

**Author:** ![Amm0](https://forum.mikrotik.com/user_avatar/forum.mikrotik.com/amm0/32/1630_2.png) [@Amm0](https://forum.mikrotik.com/u/Amm0)\
**Post date:** [February 4, 2022, 1:53pm UTC](https://forum.mikrotik.com/t/script-for-pinging-an-ip-for-24-7/155567/5 "2022-02-04T13:53:28Z")

</div>

> [@](#):
>
> And also can be added into specific logging action?

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”.

---

<div class="post-metadata">

**Author:** ![jaxed8](https://forum.mikrotik.com/letter_avatar/jaxed8/32/6_449c533f586939b6e5f246632a46421f.png) [@jaxed8](https://forum.mikrotik.com/u/jaxed8)\
**Post date:** [February 5, 2022, 3:08am UTC](https://forum.mikrotik.com/t/script-for-pinging-an-ip-for-24-7/155567/6 "2022-02-05T03:08:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rextended](https://forum.mikrotik.com/user_avatar/forum.mikrotik.com/rextended/32/67729_2.png) [@rextended](https://forum.mikrotik.com/u/rextended)\
**Post date:** [February 5, 2022, 8:35am UTC](https://forum.mikrotik.com/t/script-for-pinging-an-ip-for-24-7/155567/7 "2022-02-05T08:35:20Z")

</div>

**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

---

<div class="post-metadata">

**Author:** ![Jotne](https://forum.mikrotik.com/user_avatar/forum.mikrotik.com/jotne/32/56097_2.png) [@Jotne](https://forum.mikrotik.com/u/Jotne)\
**Post date:** [February 5, 2022, 9:10am UTC](https://forum.mikrotik.com/t/script-for-pinging-an-ip-for-24-7/155567/8 "2022-02-05T09:10:25Z")

</div>

Just a small correction.

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

This is from 6.48.4

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

```

---

<div class="post-metadata">

**Author:** ![rextended](https://forum.mikrotik.com/user_avatar/forum.mikrotik.com/rextended/32/67729_2.png) [@rextended](https://forum.mikrotik.com/u/rextended)\
**Post date:** [February 5, 2022, 9:14am UTC](https://forum.mikrotik.com/t/script-for-pinging-an-ip-for-24-7/155567/9 "2022-02-05T09:14:06Z")

</div>

The topic of this thread is “ping”, it is logical that we are only talking about that command… 😒😉😆

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](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.

---

<div class="post-metadata">

**Author:** ![Amm0](https://forum.mikrotik.com/user_avatar/forum.mikrotik.com/amm0/32/1630_2.png) [@Amm0](https://forum.mikrotik.com/u/Amm0)\
**Post date:** [February 5, 2022, 1:09pm UTC](https://forum.mikrotik.com/t/script-for-pinging-an-ip-for-24-7/155567/10 "2022-02-05T13:09:20Z")

</div>

> [@](#):
>
> 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.

/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.

```plaintext
> /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):

```plaintext
 
 > /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.

---

<div class="post-metadata">

**Author:** ![Jotne](https://forum.mikrotik.com/user_avatar/forum.mikrotik.com/jotne/32/56097_2.png) [@Jotne](https://forum.mikrotik.com/u/Jotne)\
**Post date:** [February 5, 2022, 1:15pm UTC](https://forum.mikrotik.com/t/script-for-pinging-an-ip-for-24-7/155567/11 "2022-02-05T13:15:42Z")

</div>

> [@](#):
>
> The topic of this thread is "ping", it is logical that we are only talking about that command... 😒😉😆

I do know, but not all read all detail 😉

---

<div class="post-metadata">

**Author:** ![nichky](https://forum.mikrotik.com/user_avatar/forum.mikrotik.com/nichky/32/1516_2.png) [@nichky](https://forum.mikrotik.com/u/nichky)\
**Post date:** [February 5, 2022, 11:15pm UTC](https://forum.mikrotik.com/t/script-for-pinging-an-ip-for-24-7/155567/12 "2022-02-05T23:15:25Z")

</div>

@Jotne ,@rextended

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

---

<div class="post-metadata">

**Author:** ![Jotne](https://forum.mikrotik.com/user_avatar/forum.mikrotik.com/jotne/32/56097_2.png) [@Jotne](https://forum.mikrotik.com/u/Jotne)\
**Post date:** [February 6, 2022, 9:12am UTC](https://forum.mikrotik.com/t/script-for-pinging-an-ip-for-24-7/155567/13 "2022-02-06T09:12:35Z")

</div>

As far as I know, no.

---

<div class="post-metadata">

**Author:** ![nichky](https://forum.mikrotik.com/user_avatar/forum.mikrotik.com/nichky/32/1516_2.png) [@nichky](https://forum.mikrotik.com/u/nichky)\
**Post date:** [February 9, 2022, 8:48pm UTC](https://forum.mikrotik.com/t/script-for-pinging-an-ip-for-24-7/155567/14 "2022-02-09T20:48:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nichky](https://forum.mikrotik.com/user_avatar/forum.mikrotik.com/nichky/32/1516_2.png) [@nichky](https://forum.mikrotik.com/u/nichky)\
**Post date:** [February 9, 2022, 11:32pm UTC](https://forum.mikrotik.com/t/script-for-pinging-an-ip-for-24-7/155567/15 "2022-02-09T23:32:14Z")

</div>

disregard the last
