OpenWrt detect new device and send text message (Mail)

Good evening guys, I have a Xiaomi Mi Router 4A Gigabit Edition with OpenWrt 22.03.2 i'll found a script at to be notified via email when a new device connects to my network. I read that it doesn't work in version something like that valid? Do you have a better suggestion? ill try it and didnt work for me.....also ill make openwrt dhcp client because connects to isp router.......when i connect from wifi device to Openwrt it is delay to take internet ...

There's also a workaround in the last post?

what do you mean

and it works?

Worth a try ?!

not working

Use a hotplug script as described in this topic.

only this

You can create a hotplug script (in /etc/hotplug.d/dhcp)

[ "$ACTION" == "add" ] || exit 0


if ! /bin/grep -iq "$MACADDR" "$known_mac_addr"; then
        msg="IP $IPADDR assigned to a new device with MAC addr $MACADDR"
        echo -e "From: \nTo:$notification_email \nSubject:DHCP\n\n$msg" | msmtp "$notification_email"
exit 0

or everything i mean and this

Are you wanting to actually receive an SMTP email sent via the internet to your personal inbox, or just a local sendmail message notification?

Good morning from Greece my friend, SMTP mail from internet.

If you don't have your own mail server that you can use as a smart host, see the configuration example of how to set up gmail. You will need to generate an App Password.

Make sure you get an email when you run:

echo -e "Subject: Test mail\n\nThis is a test \"message\"." | msmtp

Dump the current known devices to a file

cat /tmp/dhcp.leases | awk '{ print $2" "$3" "$4 }' >/etc/known_macs

The script below should do the job. Don't forget to set the correct mail address.

For testing purposes you could delete /etc/known_macs, comment line 1 and uncomment line 2.

cat << "EOF" > /etc/hotplug.d/dhcp/90-newdev
[ "$ACTION" == "add" ] || exit 0
# [ "$ACTION" == "add" -o "$ACTION" == "update" ] || exit 0


if ! /bin/grep -iq "$MACADDR" "$known_macs"; then
        msg="New device detected:\n\nMAC: $MACADDR\nIP: $IPADDR\nHostname: $HOSTNAME"
	    echo "$MACADDR $IPADDR $HOSTNAME" >> /etc/known_macs
        echo -e "To:$email_addr \nSubject:New device\n\n$msg" | msmtp "$email_addr"
exit 0

To add to @pavelgl ’s post (a novel approach :smiley:). This should get you up with adding gmail 2 Step Verification and modifying your msmtprc config.
[Solved]Mailsend (cant send a mail) Invalid login or password - #7 by RuralRoots

1 Like

put the script in ```
/etc/hotplug.d/dhcp/ with the name 90-newdev......install msmtp and check it...send email.......everything looks i check if the script works?......note receive any mail from script

The idea is to receive emails only when a new device is detected on the network.
So connect a new device to your network, change the MAC address of an existing device or


rm /etc/known_macs
# [ "$ACTION" == "add" ] || exit 0
[ "$ACTION" == "add" -o "$ACTION" == "update" ] || exit 0

and you will receive emails until each device on your network updates its dhcp lease and it is written to /etc/known_macs.

i dont have file in /etc/known_macsto delete is the problem........maybe openwrt is dhcp client?

Obviously you didn't do that

Just edit the script

and disconnect/connect some device.

root@Chrisrer:~# cat /tmp/dhcp.leases | awk '{ print $2" "$3" "$4 }' >/etc/known
cat: can't open '/tmp/dhcp.leases': No such file or directory

i was make the changes.........maybe the problem is something else connecteting device but not give access to internet

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.