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