Community discussions

MikroTik App
 
exer
just joined
Topic Author
Posts: 1
Joined: Wed Jan 11, 2023 3:28 pm

MQTT publish on new device connected

Wed Jan 11, 2023 3:34 pm

Hi Mikrotik community,

i am RouterOS scripting beginner to say the least. I have this idea in mind that i would like to implement but i dont have enough scripting knowledge so that is why i am writhing this topic.

I would like to publish MQTT message to my broker each time new or unknown MAC Address is detected in my DHCP server lease.

So i would like to create list of known MAC addresses and then run scheduled script to compare current addresses in lease with my list. If unknown MAC has been found i would like to publish a message in my MQTT broker.

Does anyone have similar script implemented or you have ideas on how i could achieve this it would be much appreciated...
 
reinerotto
Long time Member
Long time Member
Posts: 520
Joined: Thu Dec 04, 2008 2:35 am

Re: MQTT publish on new device connected

Fri Jan 13, 2023 3:11 pm

You can do this using a more appropriate device, instead of a mikrotik. I.e. a router, running openwrt.
However, _may_be, even your mikrotik is officially supported, to run openwrt. Then you only need to switch OS,
and you can use mosquitto and bash, to achieve your goal.
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3252
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: MQTT publish on new device connected

Tue Jan 17, 2023 2:26 pm

You can do this using a more appropriate device
The OP's need is certainly possible. I'd argue this is way easier on Mikrotik, than using OpenWRT and somehow integrating "mosquito" and "bash" [and cron and reading the dnsmasq leases].

Mikrotik has the IoT "extra-package", which gets your an MQTT publish via scripting, so that needs to be installed. See: https://help.mikrotik.com/docs/display/ROS/MQTT (they use AWS a broker in example, but any MQTT broker will do)

And the DHCP Server supports a "lease-script" (see https://help.mikrotik.com/docs/display/ ... HCP-Leases), so you can call "/iot mqtt publish" in the "Script" tab for your DHCP Server – e.g. here is a quick example that will call a MQTT broker named YOUR_BROKER (e.g. whatever defined in the IoT settings) with the data from the DHCP Server lease. No scheduler (e.g. UNIX "cron") required, since the script is called when the lease is assigned by the DHCP server.

/iot mqtt publish broker=YOUR_BROKER topic=NAME_OF_DHCP_TOPIC message="{\"leaseBound\": \"$leaseBound\",
\"leaseServerName\": \"$leaseServerName\",
\"leaseActMAC\": \"$leaseActMAC\",
\"leaseActIP\": \"$leaseActIP\".
\"lease-hostname\": \"$lease-hostname\",
\"lease-options\": \"$lease-options\"}" 

Now this would report on ANY renewal, not just new/different ones. While you can do this filter on the Mikrotik side, that get more complex (e.g. could use a global variable used to previous ones, but these be lost on reboot, or you could make an HTTP call to your backend to know if the MAC already exists, etc.). I this it might be better to just let the broker filter out any duplicates (or update the existing MAC address record on the broker/database side), most brokers have some rules engine that more easily allows the filtering.
 
reinerotto
Long time Member
Long time Member
Posts: 520
Joined: Thu Dec 04, 2008 2:35 am

Re: MQTT publish on new device connected

Wed Jan 18, 2023 5:18 am

I'd argue this is way easier on Mikrotik, than using OpenWRT and somehow integrating "mosquito" and "bash" [and cron and reading the dnsmasq leases].
#Sorry,but I have to correct you.
#All you have to do on openwrt is to log into openwrt-shell, using ssh or putty, for example, and type:
echo 'dhcp-script=/etc/mqttlease.sh' >> /etc/dnsmasq.conf
#Install optional package
opkg install mosquitto
#To communicate with AWS-IoT, you need
opkg install mosquitto-ssl
#instead
cat > /etc/mqttlease.sh <<END
#!/bin/sh
event="$1"
if [ $event != 'del' ] then
mac="$2"
ip="$3"
hostname="$4"
time="`date '+%Y-%m-%d %H:%M:%S'`"
message="$time $event $mac $ip $hostname"
mosquitto_pub -h 'YOURMQTTBROKER' -t 'YOURTOPIC' -m "$message"
#For AWS:
#mosquitto_pub --cafile 'PATHTOYOURAWSCAFILE' --cert 'PATHTOYOURAWSCLIENTCERTIFICATE' --key 'PATHTOYOURAWSCLIENTKEY' -h 'YOURMQTTBROKER' -t 'YOURTOPIC' -m "$message"
fi
END
#Just in case ...
chmod +x /etc/mqttlease.sh
#dnsmasq is a wide spread, tiny, open source dns-forwarder/cache and dhcp server, standard in every openwrt system.
#Thus, very well documented and maintained. Goggling for 'manpage dnsmasq' you will find more detailed info about dhcp-script.
#mosquitto is a well known and well documented open source mqtt util, too. Goggling for 'manpage mosquitto' will help you.
#Note, that deleted/expired leases will not be reported via mqtt. Further filtering to be done on broker.

I am thankful for this real world comparison of same functionality in ROS and openwrt.
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3252
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: MQTT publish on new device connected

Wed Jan 18, 2023 6:03 pm

More saying if you have to start with flashing your Mikrotik to install OpenWRT, and configure your entire network to use it – that's a bit more work than any marginal syntax difference in scripting languages.

But...I'll give you the `date` command is a tricky one for RouterOS scripting. They don't have easy way to get an ISO date format – so you'll note I don't include that in my example ;). Now since the broker should timestamp the received message, not a huge issue.

Who is online

Users browsing this forum: No registered users and 54 guests