Community discussions

MikroTik App
 
jaxed8
Member Candidate
Member Candidate
Topic Author
Posts: 195
Joined: Tue Jul 27, 2021 8:25 pm

Script for pinging an IP for 24/7

Fri Feb 04, 2022 4:37 am

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.
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3255
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: Script for pinging an IP for 24/7

Fri Feb 04, 2022 5:12 am

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.
 
User avatar
nichky
Forum Guru
Forum Guru
Posts: 1275
Joined: Tue Jun 23, 2015 2:35 pm

Re: Script for pinging an IP for 24/7

Fri Feb 04, 2022 10:18 am

@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?
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3255
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: Script for pinging an IP for 24/7

Fri Feb 04, 2022 3:49 pm

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 
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3255
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: Script for pinging an IP for 24/7

Fri Feb 04, 2022 3:53 pm

And also can be added into specific logging action?
If you log from a script (e.g. :log <level> <message>) , the "topic" is always "script", so control the log dest/etc based on the topic ~= "script".
 
jaxed8
Member Candidate
Member Candidate
Topic Author
Posts: 195
Joined: Tue Jul 27, 2021 8:25 pm

Re: Script for pinging an IP for 24/7

Sat Feb 05, 2022 5:08 am

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.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script for pinging an IP for 24/7

Sat Feb 05, 2022 10:35 am

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

RouterOS 6.x.x code

# 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)

RouterOS 7.x.x code

# 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 <TAB> after you write "ping " on terminal, some descriptions are missing, but are intuitive
on 7.x.x press <F1> or <TAB> for the same (v7 also suggest discovered MAC addresses)

RouterOS 6.46.8 code

[@_@] > ping <?>
Send ICMP Echo packets. Repeat after given time interval.

<address> -- 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 <TAB>
          count  interface  routing-table  src-address  address
arp-ping  dscp   interval   size           ttl          

RouterOS 7.1.1 code

[@_@] > ping <F1>

<address> -- 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 <TAB>
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:

Example on RouterOS 6.46.8 code

# pptp-noexit do not acept routed connections, but the traffic still pass the link
[@_@] > ping 1.1.1.1 count=3 interface=<pptp-noexit>
  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% 

Example on RouterOS 7.1.1 code

# 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
Last edited by rextended on Sat Feb 05, 2022 11:26 am, edited 5 times in total.
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3292
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Script for pinging an IP for 24/7

Sat Feb 05, 2022 11:10 am

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
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script for pinging an IP for 24/7

Sat Feb 05, 2022 11:14 am

The topic of this thread is "ping", it is logical that we are only talking about that command... :roll: :wink: :lol:

On more complex way I have "created" as-value for ping on v6:
viewtopic.php?f=2&t=69773&p=876970#p876970
is valid for both normal and MAC ping.
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3255
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: Script for pinging an IP for 24/7

Sat Feb 05, 2022 3:09 pm

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.
> /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.
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3292
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Script for pinging an IP for 24/7

Sat Feb 05, 2022 3:15 pm

The topic of this thread is "ping", it is logical that we are only talking about that command... :roll: :wink: :lol:
I do know, but not all read all detail ;)
 
User avatar
nichky
Forum Guru
Forum Guru
Posts: 1275
Joined: Tue Jun 23, 2015 2:35 pm

Re: Script for pinging an IP for 24/7

Sun Feb 06, 2022 1:15 am

@Jotne ,@rextended

ill ask again , is there any script even on v7, that i can ping from VRF with specific src-add?
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3292
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Script for pinging an IP for 24/7

Sun Feb 06, 2022 11:12 am

As far as I know, no.
 
User avatar
nichky
Forum Guru
Forum Guru
Posts: 1275
Joined: Tue Jun 23, 2015 2:35 pm

Re: Script for pinging an IP for 24/7

Wed Feb 09, 2022 10:48 pm

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
 
User avatar
nichky
Forum Guru
Forum Guru
Posts: 1275
Joined: Tue Jun 23, 2015 2:35 pm

Re: Script for pinging an IP for 24/7

Thu Feb 10, 2022 1:32 am

disregard the last

Who is online

Users browsing this forum: stanisljevic and 22 guests