Community discussions

MikroTik App
 
petrasl1976
just joined
Topic Author
Posts: 4
Joined: Thu Mar 25, 2021 9:20 am

Trigger script on new entries in wireless registration-table

Thu Mar 25, 2021 1:50 pm

Hello,

Already a couple of days i am trying to find how to send webhooks when device is connected to WIFI. The closest thing i managed to find is the combination of "lease-script" and "/tool fetch". I wrote the following script to the /ip dhcp-server edit defconf lease-script and tested.
:log info "Call webhook:  http://192.168.88.25:1880/client?state=$leaseBound&mac=$leaseActMAC&ip=$leaseActIP"
/tool fetch url=("http://192.168.88.25:1880/client?state=".$leaseBound."&mac=$leaseActMAC&ip=$leaseActIP")
The http://192.168.88.25:1880 is my NodeRed container on IOTstack with "http in" nodes. It work as expected. But it only informs when new lease is created or device is disconnected and lease expires.

My lease time on dhcpd is 24:00:00. If i connect and disconnect multiple times during lease time - script is not triggered and i do not receive notifications in NodeRed.

What i would like to achieve is to trigger similar script when entries in /interface wireless registration-table appear or disappear.

Thanks in advance
PetrasL
Last edited by petrasl1976 on Sun Apr 04, 2021 7:44 am, edited 1 time in total.
 
User avatar
loloski
Member Candidate
Member Candidate
Posts: 296
Joined: Mon Mar 15, 2021 9:10 pm

Re: Trigger script on new entries in wireless registration-table

Sat Mar 27, 2021 2:43 pm

Hey,

I know this is not directly answer your question but this will achieved what you want but you have to do more.https://wiki.mikrotik.com/wiki/Log_Pars ... ger_Script
The idea is you have to capture wirless logging and parse it, you can have a script run periodically as a polling mechanism, I know this is less than ideal but this would work.

Another approach send your logs to a remote syslog server and parse it there, again less than ideal but it should work

https://wiki.mikrotik.com/wiki/Manual:System/Log
 
petrasl1976
just joined
Topic Author
Posts: 4
Joined: Thu Mar 25, 2021 9:20 am

Re: Trigger script on new entries in wireless registration-table

Sun Mar 28, 2021 4:12 pm

I thought about sending logs away and parse them periodically too. Also thought about ssh with keys and get table output directly. Finally found this lease-script option and wandering why there is now similar capability with registration table? As per my opinion it would be very convenient for people who playing with home automation stuff and want to trigger some actions when they come home or leave apartaments.

One more thought - might be possible to achieve this by reducing lease time and do some other checks?
Regards
PetrasL
 
User avatar
loloski
Member Candidate
Member Candidate
Posts: 296
Joined: Mon Mar 15, 2021 9:10 pm

Re: Trigger script on new entries in wireless registration-table

Mon Mar 29, 2021 5:13 pm

Hey,

I can feel you, i believed there is no similar functionality in the wireless registration table, only on the DHCP lease section that you can make a script. You can file a feature request on mikrotik but that' a long shot :)
 
petrasl1976
just joined
Topic Author
Posts: 4
Joined: Thu Mar 25, 2021 9:20 am

Re: Trigger script on new entries in wireless registration-table

Sun Apr 11, 2021 9:38 am

... but still, I don't like how lease-script works here :(. When user leaves the house, it does not trigger anything, but then user comes back lease-script is triggered twice - offline and online.

I wrote simple shell script, so my raspberry could ssh to Mikrotik, get recent registration-table and after simple comparison with diff send those hooks on behalf of Mikrotik:
#!/bin/sh
new=/tmp/registration-table.new			# define a file for new registration-table ...
old=/tmp/registration-table.old			# ... and for previous one
[ -f $new ] && mv $new $old || touch $old	# shift "new" table to "old", as we will get newer soon. 
						# If this is a first run - create empty "old" table ...
						# ... so diff command would not complain

# ssh to Mikrotik (with ssh keys) and print fresh registration-table
# awk - let the field separator be " " and "=" characters. ...
# ... Leave only 6-th column (mac address) and store whole output as "new" table
ssh admin@192.168.88.1 /interface wireless registration-table print terse |awk -F'[ =]' '{print $6}' > $new

# diff - let's use more meaningful online/offline labels instead of > < to display differences between tables
# grep - filter diff command output and leave lines only with our defined labels
# while - with each line store 1st column to ${status} variable (online/offline) and 2-nd to ${mac}
diff --old-line-format="offline %L" --new-line-format="online %L" $old $new | egrep "line"|while read status mac
do
	# finally silently call our webhook with recent updates in the "new" table
	curl -so /dev/null "http://myddnshost123at.duckdns.org:1880/wifi?status=${status}&mac=${mac}"
done

But i don't like ssh in this script now :D Its too heavy/slow/unnecessary and for security reasons too...

I am complete newbie in RouterOS and I would really appreciate if someone would rewrite similar logic to its native script language.

Few more questions:
* Does RouterOS have cron daemon or something similar? Can it schedule job to run each 15s?
* Would it be significant load increment for Mikrotik to run such script each 15s or 1min if there is no such fine granularity?

Thanks in advance!
PetrasL
 
User avatar
jvanhambelgium
Forum Veteran
Forum Veteran
Posts: 985
Joined: Thu Jul 14, 2016 9:29 pm
Location: Belgium

Re: Trigger script on new entries in wireless registration-table

Sun Apr 11, 2021 9:50 am

Few more questions:
* Does RouterOS have cron daemon or something similar? Can it schedule job to run each 15s?

Sure it has. Check under /system scheduler

* Would it be significant load increment for Mikrotik to run such script each 15s or 1min if there is no such fine granularity?


Hmm, your action is not really complex. Don't think running it every 15sec is going to be a problem.

But that depends a bit on the hardware box you are using, but even a simple board should not choke on this.
 
petrasl1976
just joined
Topic Author
Posts: 4
Joined: Thu Mar 25, 2021 9:20 am

Re: Trigger script on new entries in wireless registration-table

Sun Apr 11, 2021 12:01 pm

Great! So remain just to wait for "good Samaritan" who can write this piece of script.

Thanks!

Who is online

Users browsing this forum: Batterio, Kuitz and 52 guests