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
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_Parser_-_Event_Trigger_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
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
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 
… 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
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
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.
Great! So remain just to wait for “good Samaritan” who can write this piece of script.
Thanks!