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 https://gist.github.com/jwalanta/53f55d03fcf5265938b64ffd361502d5 to be notified via email when a new device connects to my network. I read that it doesn't work in version 22.03..is 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 ...

1 Like

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

known_mac_addr="/etc/known_mac_addr"
notification_email="aaaa@gmail.com"

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"
fi
exit 0

or everything i mean and this https://gist.github.com/jwalanta/53f55d03fcf5265938b64ffd361502d5

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 someone@examle.com

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

known_macs="/etc/known_macs"
email_addr="someone@examle.com"

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"
fi
exit 0
EOF
3 Likes

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 fine.....how 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

meaning

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 it...here 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
_macs
cat: can't open '/tmp/dhcp.leases': No such file or directory
root@Chrisrer:~#

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.