Can I run a script only when a wan interface gets a IP?

I made a service to check 8.8.8.8 every 10 seconds and turn on the 'Internet' led on my device if it's successful. The service runs at boot but I was wondering if I can get it to run only when a wan interface gets an IP. I do have multiple wans. The interfaces are designated as wans in the firewall. Also, hotplug.d/net/ doesn't really have much of an explanation in the hotplug guide.

A bit hacky, but my unix fu is very limited:
ip addr show eth0 | grep -q "inet \d\{1,3\}.\d\{1,3\}.\d\{1,3\}.\d\{1,3\}
and echo $? will then return if there is a IPv4 on that interface or not. You can do the same for a IPv6 address. Keep in mind this wont validate that IPv4. 999.999.999.999 would be valid too.
Replace eth0 with your interface.

But the real question is: why would you even wan't to replace that script? 8.8.8.8 is almost always reachable. So that would be a way better indicator of "having internet" imo.

You can put a script in /etc/hotplug.d/iface, say 90-led or whatever else you want to name it

#!/bin/sh
  
[ "$ACTION" == "ifup" ] && [ "$INTERFACE" == "wan" ] && {

    logger -t led-monitor "Starting LED monitoring of wan status";
    /usr/bin/your-led-monitor;

}

Note that these scripts get called whenever there is a network config change, so they may well be called multiple times after boot. Your code should check for this.

2 Likes

Here's my dirty version.


#!/bin/sh
wan_iface="ens3"

check_if_wan_up="$(ip -br a | grep "$wan_iface" | grep UP)"
check_wan_IP="$(ip -br a | grep "$wan_iface" | grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')"

if [ "$check_if_wan_up" != "" ] && [ "$check_wan_IP" != "" ]; then

    logger -t led-monitor "Starting LED monitoring of wan status"
    /usr/bin/your-led-monitor

fi
1 Like

This is originally what I'm using. I don't know if the service part is written up to par. I did have a hotplug script starting the ping script but I could be using one of 3 wans. I could just put all 3 wan interfaces in the hot plug but I wanted to try something a little more "universal". Maybe with the logic of:
if $ACTION = ifup; then
get list a 'wans' from firewall config (uci get firewall.cfg03dc81.network)
if $INTERFACE = ANY of those in the list of wans; then
check is service is running, if not start service.

for the ifdown part it would be the same except check if ALL wan interfaces in the list are down, and if they are, then stop the script. I just don't know how to parse the list of wans and check their state.

/etc/init.d/ledpingchecksvc

#!/bin/sh /etc/rc.common

START=21
STOP=89
USE_PROCD=1

start_service() {
logger -t custom.script.ledpingchecksvc "Starting web ping check for INTERNET LED"
procd_open_instance
procd_set_param command "/usr/sbin/ledpingchecksvc"
procd_set_param stdout 0
procd_set_param stderr 0
procd_set_param respawn
procd_close_instance
}
service_triggers()
{
procd_add_reload_trigger "network"
}
reload_service() {
logger -t custom.script.ledpingchecksvc "Restarting web ping check for INTERNET LED"
echo 0 > /sys/class/leds/y1:blue:internet/brightness #turn off LED
}
stop_service() {
logger -t custom.script.ledpingchecksvc "Stopping web ping check for INTERNET LED"
echo 0 > /sys/class/leds/y1:blue:internet/brightness #turn off LED
}

usr/sbin/ledpingcheck

#!/bin/sh

while :
do
ping -q -w 5 -c 1 8.8.8.8
if [ "$?" -eq 0 ]; then
  echo 1 > /sys/class/leds/y1:blue:internet/brightness #turn on LED
else                                              
  echo 0 > /sys/class/leds/y1:blue:internet/brightness #turn off LED
fi
sleep 10
done