How to detect DHCP assignment to send an email on OpenWrt

Hi.

I wanna keep monitor whether specfic ip assigned or not and once changed the ip status for specific ip,
wanna send SMTP to admin.

Some questions here. Please advice me.

For the target device Linksys WRT32x,

  • Can I use/add a 'auto-email notification' to specific email address using given SMTP information when dhcp assigned to a device?

  • Can I use/add a 'auto-email notification' to specific email address using given SMTP information when after router boot-up?

  • To archive above, how should I do? Custom build? or configuration only? or...another custom firmware not Open WRT?

You should be able to do it with simple scripting.

"Send an email [with SMTP]" though is a huge security hole in any system as you'll need to authenticate the router to your mail server. Doing so would mean that, when compromised, it could become a spam bot without much effort.

I'd look into more secure solutions, such as using MQTT or keyed, command-restricted ssh to trigger a different a pre-defined message to be sent to a predefined recipient on a more secure and less exposed host.

Secure logging, with something like syslog-ng to a remote host, preferably with TLS and authentication (which it supports).

Packages are available for what you need, depending on the route you decide on.

1 Like

To trigger commands after boot, use /etc/rc.local, to trigger commands upon DHCP assignments, create a new script in /etc/hotplug.d/dhcp/, e.g. /etc/hotplug.d/dhcp/99-notify with contents similar to this:

if [ "$ACTION" = add ]; then
    (
        echo "Subject: new DHCP lease on $(hostname)"
        echo ""
        echo "A new DHCP lease has been assigned: MAC=$MACADDR, IP=$IPADDR, NAME=$HOSTNAME"
    ) | sendmail you@example.org
fi
2 Likes

Hi.

Thanks for the information. But it seem that for DHCP Client perspective...

What I'm looking for is that
as the DHCP Server perspective, when any of device join in to the network area(which get an IP from openWRT), I'd like to execute a script with the arguments that assigned IP of the joined device.

Is there any of hint or material for that?
I really need hint.

The script hooks I mentioned are executed by the DHCP server.

1 Like

Thanks for the quick reply.
The reason why I asked you again was that
I have put the script as below

root@hostname:/etc/hotplug.d/dhcp# ls
99-notify (as 755)
root@hostname:/etc/hotplug.d/dhcp#

but nothing happened when my smartphone joined in the wlan.
(the smartphone has static lease rule)

Something wrong with me?

I think you need to restart dnsmasq once after placing the script to enable the hotplug mechanism.

Try to run /etc/init.d/dnsmasq restart and test again.

Erm,
it works only OpenWrt boot-up as the dhcp server recognize devices.

It doesn't work when

  • turn off smartphone's wifi.
  • turn on again a smartphone's wifi.

my script as below... any idea?

if [ "$ACTION" = add ]; then
    (
        send email
    )
fi

if [ "$ACTION" = remove ]; then
    (
        send email
    )
fi

When you turn off the smartphone wifi and enable it again, you likely don‘t get an entirely new DHCP lease but an updated existing one.

In this case, $ACTION is set to update. So try if [ "$ACTION" = add ] || [ "$ACTION" = update ]; ...

Okay it is better now! Thanks.

One thing still issue is that...
once a smartphone or PC device deactivated(mean that turn off wifi or power)
still no update callback(mean that seem OpenWrt doesn't call ACTION = REMOVE...).
Is there any workaround do you know?

You configure the smartphone to release the DHCP address before its shuts down wifi... Well, likely not possible.

If the phone does not explicitly release the address, how would the DHCP daemon know that the address has been removed ??? So, there is no DHCP-based inactivity monitoring.

One resource-hungry alternative would be that you have a separate script that e.g. every minutes tries pinging all known devices, and if one does not answer for 2-3 times, you then trigger the action.

Not sure, but there might be something on wifi disassociation actions with hostapd. But I have never looked into that.

Really appreciate for all your advice!

To wrap up,

  • by the 'dhcp server' base callback(which your guide) works well either when 'ip registering' and 'ip activate'(aka the action 'add' and 'update')
  • Regarding the ip inactive callback,
    a. for the WiFi devices, as you 'hostapd' mentioned, found and verified the solution at the bottom(as replies) of the https://gist.github.com/jwalanta/53f55d03fcf5265938b64ffd361502d5.
    which is working well as they mentioned.
    b. for the Wired Lan devices, which still looking for is nothing so far.
    the ifup/ifdown only care about OpenWrt's IFs...

so....one of my problem still unresolved....that for Wired Lan ip down callback.. which is actually I'm looking for...to manager PC server...

If you have more idea as expert(than me),
please share it anything further.

Well, that is the only one to which there is no solution with the default config. Nothing forces wired PCs to use DHCP, or to try to somehow register at all to the router. They can just use a fixed IP address and try to send packets to the wire (and router then decides if it forwards the packets).

You could of course configure some kind of 802.11x solution to force registration, but then you need to setup the client PCs, too.

Again, using some brute-force scripting (e.g. using "arp" combined with ping*) could be used. (As a connected device may quite legitimately be quiet for some time, so there is no real way of knowing if the device is still there, unless you ping it.)

As far as you know that, not prefer polling method since I'm using that DHCP in my network for wired PC and Wifi as well.

To make OpenWrt callback when dhcp inactive(by ACTION = UPDATE or ACTION = INACTIVE or so) like the hostapd does, what should I do?
more over if necessary, which part of source code should be modified ?

Hi Community,
i did create the script as 99-notify named in the folder given.
did then an restart of dnsmasq.
In the log i can see when activating an device for wlan and the dhcp request & ack.

But there is no Mail sending.
Is there anything i miss?
i am on Version 22.03.5

Thank You :slight_smile:
Jens

SMTP Clients

Already installed before and tested. It can send mail

Before i put the call in the dnsmasq.confdchp-script= but this runs on error

and this error only says "error" ? cool ....

I got it. was an silly copy paste error.
in the working script i used the msmtp command to send the mail in the new script used sendmail which doenst work.

Now it works. Thanks for pointing :wink:

br
jens