Community discussions

MikroTik App
 
drmotaadam
just joined
Topic Author
Posts: 11
Joined: Sat Nov 13, 2021 12:22 am

turn something on or off when the RouterBOARD sees an ECHO request packet from any IP on my network

Wed Jan 11, 2023 7:49 pm

Hi, I am trying to make a monitoring script and it's out of my scripting knowledge. The goal is to turn something on or off when the RouterBOARD sees an ECHO request packet from any IP on my network (I know that MK has netwatch but that won't help me because I need something different).

I will give you an example: I will ping from my PC 10.1.1.1. The script will detect that I pinged that IP from my computer and will do something. I have already done something that works but it's not ideal.

I am using this firewall raw rule: action=add-src-to-address-list address-list=Test address-list-timeout=1s chain=prerouting comment=Ether5-ON dst-address=10.1.1.1 icmp-options=8:0-255 protocol=icmp. Basically, it's adding that IP (10.1.1.1) to the IP list. Then after that, I wrote a script that monitors that specific IP list and if it sees IP in it it will do something.

Script:

:do {

:local ip [[len [ip firewall address-list find list="Ether5-OFF"]]];


if ($ip>0) do={
:log info message="Ether5-OFF";
/ip firewall filter enable 2;
/beep;}


delay 4s

} while=(true)

Yeah, it works but it's not ideal because if someone sends more than one packet it will clog the router and the log is full of the same messages. The router is RB2011 with 6.48.6 (I had some problems with V7 on that). Because of that, I would like to bin that FW rule with an IP list and implement everything into one script.

And now the part that I need help with. I have no idea how to watch for that ping from the script. I had the idea to use a torch but don't know how to implement it into the script.
 
drmotaadam
just joined
Topic Author
Posts: 11
Joined: Sat Nov 13, 2021 12:22 am

Re: turn something on or off when the RouterBOARD sees an ECHO request packet from any IP on my network

Wed Jan 11, 2023 9:47 pm

I've tried something like that but it doesn't work. There has to be some mistake in that.

:local targetIp "10.1.1.1"
:local running true

:do {
:local torchResult [/tool torch ip-protocol=icmp dst-address=$targetIp interface=ether5]
:foreach line in=$torchResult do={
:local fields [:toarray $line]
:if (($fields->1 = "icmp") && ($fields->2 = $targetIp)) do={
:log info message="ICMP Echo Request Packet Received";
:set running false
}
}
:delay 1
} while=($running)
 
tomislav91
Member
Member
Posts: 303
Joined: Fri May 26, 2017 12:47 pm

Re: turn something on or off when the RouterBOARD sees an ECHO request packet from any IP on my network

Wed Jan 11, 2023 11:57 pm

maybe something like this
:local running true
:do {
  /interface ethernet monitor numbers=ether5
  :local torchResult [/tool torch duration=0s icmp-query dst-address=10.1.1.1]
  :foreach line in=$torchResult do={
    :local fields [:toarray $line]
    :if ($fields->1 = "ICMP") do={
        :log info message="ICMP Echo Request Packet Received";
        :local running false
    }
  }
  :delay 1
} while=($running)
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: turn something on or off when the RouterBOARD sees an ECHO request packet from any IP on my network

Thu Jan 12, 2023 11:00 am

nice: duration=0s, so: how long does it ever run for?

and about this:
:local running true
:do {
[...]
        :local running false
[...]
  }
  :delay 1
} while=($running)
For me is an idiocy.
Better schedule the script every 1 second than create loops inside the device.
If for some reason the loop is interrupted, nothing start it again.
 
User avatar
BartoszP
Forum Guru
Forum Guru
Posts: 2865
Joined: Mon Jun 16, 2014 1:13 pm
Location: Poland

Re: turn something on or off when the RouterBOARD sees an ECHO request packet from any IP on my network

Thu Jan 12, 2023 5:17 pm

IMHO "..."is an idiocy" could be written as " ...should be rewritten to system-non-hogging version using scheduler instead of looping a delay"
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: turn something on or off when the RouterBOARD sees an ECHO request packet from any IP on my network

Thu Jan 12, 2023 6:15 pm

[…] " ...should be rewritten to system-non-hogging version using scheduler instead of looping a delay"
and not
Better schedule the script every 1 second than create loops inside the device.
??? :lol:
 
drmotaadam
just joined
Topic Author
Posts: 11
Joined: Sat Nov 13, 2021 12:22 am

Re: turn something on or off when the RouterBOARD sees an ECHO request packet from any IP on my network

Thu Jan 12, 2023 8:19 pm

[…] " ...should be rewritten to system-non-hogging version using scheduler instead of looping a delay"
and not
Better schedule the script every 1 second than create loops inside the device.but
??? :lol:
Yeah, I am dumb, I forgot that there is a scheduler so I can let off the loop. But still, I have a problem with that code even though I cant see the mistake. I think that this is the right path to go.

Who is online

Users browsing this forum: alexantao and 19 guests