I apologize if the question is stupid...
how do I use the command line to understand / get the remaining time in which the IP address assigned to the wan will be valid.
Is there a file somewhere that keeps track of the lease time obtained via DHCP protocol on the WAN?
something similar but from the command line

I tried with the command:
ubus call network.interface.wan status
but it just gives me time:
uptime
leasetime
I would need time to
Expires (which I assume is leasetime - uptime)
For now I solved it like this, but there is a cleaner way
expr $(ubus call network.interface.wan status | grep "leasetime" | awk '{print $2}') - $(ubus call network.interface.wan status | grep "uptime" | awk '{print $2}' | grep -o '[0-9]\+')
I don't think this is a correct assumption -- for example, on my device, I have an uptime of 1211693 but a leasetime of 86400. This would work if the device was rebooted or the interface was completely taken down, but if the interface remains up through DHCP renewals, the uptime should be longer than the leasetime.
You can findout when the lease was most recently issued by looking at the logs:
logread -e udhcpc
Then, take the timestamp of the most recent lease and the number of seconds for that lease and do the math for the expiration. You can look at it at the 50% and 87.5% (of lease length) to determine the first (and second, if necessary) attempts to renew the lease.
2 Likes
I hadn't thought of this... 
then I ask a more technically correct question
I would like to run a script every time the IP address of the WAN is renewed even if the IP address could be the same as the previous one, how can I do it?
for now it's just an idea I have I would like to create a log file of all the connections/disconnections of a router in which I will install openwrt
to check/monitor internet connectivity upon IP change
the router where I will install Openwrt is this:
and the help commands, do you mean these two commands?
# Logging DHCP client
cat << "EOF" > /etc/udhcpc.user.d/00-logger
logger -t ${0##*/} ${@} $(env)
EOF
# Reading logs
logread -e dhcp.script
cat << "EOF" > /etc/udhcpc.user.d/30-default-route
DHCPC_EVENT="${1}"
DHCPC_IF="${interface}"
DHCPC_GW="${router}"
case ${DHCPC_EVENT} in
(bound|renew) ;;
(*) exit 0 ;;
esac
ip route delete default dev "${DHCPC_IF}"
ip route add default via "${DHCPC_GW}" dev "${DHCPC_IF}"
EOF
on the WAN or on the lan? Two different things.
You could also just send your logs to a syslog server if you want.
only wan
save logs into file to usbkey
You'd probably want to put a hotplug script in /etc/hotplug.d/dhcp
That script would just append some log data to the file stored on the USB stick with the lease renewal info. Or, send all logs to the USB stick which is the easiest solution of all.
1 Like
do you say it could go well (then I'll evaluate it in the field)?
cat << "EOF" > /etc/hotplug.d/dhcp/00-logger
logger -t hotplug-dhcp $(env)
if [ -f /tmp/mnt/sda1/logger/dhcp-event ]; then
echo "----------------------------------------------------" >> /tmp/mnt/sda1/logger/dhcp-event
date >> /tmp/mnt/sda1/logger/dhcp-event
echo $(env) >> /tmp/mnt/sda1/logger/dhcp-event
ping -c 1 8.8.8.8 2> /dev/null
if [ $? -eq 0 ]; then
echo "ping ok" >> /tmp/mnt/sda1/logger/dhcp-event
else
echo "ping error" >> /tmp/mnt/sda1/logger/dhcp-event
fi
fi
EOF
Give it a shot and see what happens.
FWIW, your DHCP lease should ideally renew at 50% of the lease time, which means there should never be an interruption. The only time you'll see an interruption is if the DNS lease fails to renew, in which case you will also see that at the 50%, 87.5% and then 100% lease time attempts in the logs.
1 Like
and thank you for your kindness and knowledge