Way to disable wifi if lan4 is disconnected?

Is there a way to disable the 5Ghz wifi (phy1-ap0) if the lan4 port cable is disconnected and then reenable it if the link is back up? A 10sec delay might be useful too.
So in effect the status of lan4 is a toggle for phy1-ap0.

Cudy wr1300 v4, 25.12.4

root@OpenWrt:~# ip address show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host proto kernel_lo 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1504 qdisc mq state UP group default qlen 1000
    link/ether 80:af:ca:de:bc:15 brd ff:ff:ff:ff:ff:ff
    inet6 fe80::82af:caff:fede:bc15/64 scope link proto kernel_ll 
       valid_lft forever preferred_lft forever
3: wan: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc cake state UP group default qlen 1000
    link/ether 80:af:ca:de:bc:16 brd ff:ff:ff:ff:ff:ff
    inet 102.xxx.xxx.xxx/24 brd 102.xxx.xxx.255 scope global wan
       valid_lft forever preferred_lft forever
    inet6 fe80::xxxx:xxxx:xxxx:xxxx/64 scope link proto kernel_ll 
       valid_lft forever preferred_lft forever
4: lan4@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master br-lan state UP group default qlen 1000
    link/ether 80:af:ca:de:bc:15 brd ff:ff:ff:ff:ff:ff
5: lan3@eth0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue master br-lan state LOWERLAYERDOWN group default qlen 1000
    link/ether 80:af:ca:de:bc:15 brd ff:ff:ff:ff:ff:ff
6: lan2@eth0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue master br-lan state LOWERLAYERDOWN group default qlen 1000
    link/ether 80:af:ca:de:bc:15 brd ff:ff:ff:ff:ff:ff
7: lan1@eth0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue master br-lan state LOWERLAYERDOWN group default qlen 1000
    link/ether 80:af:ca:de:bc:15 brd ff:ff:ff:ff:ff:ff
8: teql0: <NOARP> mtu 1500 qdisc noop state DOWN group default qlen 100
    link/void 
11: br-lan: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 80:af:ca:de:bc:15 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.1/24 brd 192.168.1.255 scope global br-lan
       valid_lft forever preferred_lft forever
    inet6 fddb:6404:fb41::1/60 scope global noprefixroute 
       valid_lft forever preferred_lft forever
    inet6 fe80::82af:caff:fede:bc15/64 scope link proto kernel_ll 
       valid_lft forever preferred_lft forever
17: phy0-ap0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master br-lan state UP group default qlen 1000
    link/ether 80:af:ca:de:bc:15 brd ff:ff:ff:ff:ff:ff
20: phy1-ap0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master br-lan state UP group default qlen 1000
    link/ether 80:af:ca:de:bc:17 brd ff:ff:ff:ff:ff:ff
23: ifb4wan: <BROADCAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc cake state UNKNOWN group default qlen 32
    link/ether d6:4e:61:02:5d:13 brd ff:ff:ff:ff:ff:ff
    inet6 fe80::d44e:61ff:fe02:5d13/64 scope link proto kernel_ll 
       valid_lft forever preferred_lft forever

Would this work? Got this with the help of ai.

/etc/hotplug.d/iface/99-wifi_lan4

#!/bin/sh

MONITOR_IF="lan4"
TARGET_WIFI="phy1-ap0"
DELAY_SECONDS=10

if [ "$INTERFACE" = "$MONITOR_IF" ]; then
    if [ "$ACTION" = "ifup" ]; then
        # Cancel any pending disable actions by killing older script instances
        # (Optional safety check: pkill -f "sleep $DELAY_SECONDS")
        
        uci set wireless.${TARGET_WIFI}.disabled='0'
        uci commit wireless
        wifi reload
    
    elif [ "$ACTION" = "ifdown" ]; then
        # Run the delay and check in the background so hotplug doesn't hang
        (
            sleep $DELAY_SECONDS
            
            # Read the current operstate of the interface
            # 'down' means the cable is still unplugged
            if [ -f "/sys/class/net/$MONITOR_IF/operstate" ] && [ "$(cat /sys/class/net/$MONITOR_IF/operstate)" = "down" ]; then
                uci set wireless.${TARGET_WIFI}.disabled='1'
                uci commit wireless
                wifi reload
            fi
        ) &
    fi
fi


chmod +x /etc/hotplug.d/iface/99-wifi_lan4

everything works on paper ,)

for me, the port status' lowerlayerdown when down.

root@OpenWrt:/sys/class/net# ls
br-lan  eth0    lan1    lan2    lan3    lan4    lo      wan
root@OpenWrt:/sys/class/net# cat lan*/operstate
up
lowerlayerdown
lowerlayerdown
lowerlayerdown
root@OpenWrt:/sys/class/net#

Ok mine is the same.

root@OpenWrt:/sys/class/net# ls
br-lan    ifb4wan   lan2      lan4      phy0-ap0  teql0
eth0      lan1      lan3      lo        phy1-ap0  wan
root@OpenWrt:/sys/class/net# cat lan*/operstate
lowerlayerdown
lowerlayerdown
lowerlayerdown
up

So I'll need to change "ifdown" and "ifup" to "lowerlayerdown" and "up"

then the check's wrong in your AI script ...

I would advise against that method. It probably works, but it rewrites the wifi configuration every time it hits. A minor concern is that this wears out the flash memory depending on how often that happens, but a major concern is that it will leave the wifi in the last state upon reboot. Set the wifi to the default in the config, then use wifi {up|down} <interface> to turn it off or on on the fly.

So I'll need to change "ifdown" and "ifup" to "lowerlayerdown" and "up"

no, something like this (incomplete script)

Do you mean global config? Wifi is on by default currently when router boots up.

Not when your script sets and commits it otherwise. The commands do what they describe, they set aspects of the configuration, and commit that configuration to be persistent.

Solution,

/etc/config/network

config interface 'lan4mon'
        option device 'lan4'
        option proto 'none'
        option force_link '0'

/etc/init.d/network restart

/etc/hotplug.d/iface/95-lan4-radio

#!/bin/sh
# /etc/hotplug.d/iface/95-lan4-radio
#
# Disables radio1 when the LAN4 cable is unplugged for more than
# DELAY_SECONDS, and re-enables it as soon as LAN4 is plugged back in.
#
# Requires this entry in /etc/config/network:
#   config interface 'lan4mon'
#           option device 'lan4'
#           option proto 'none'
#           option force_link '0'

export PATH=/usr/sbin:/usr/bin:/sbin:/bin

MONITOR_IF="lan4mon"
DELAY_SECONDS=10

[ "$INTERFACE" = "$MONITOR_IF" ] || exit 0

case "$ACTION" in
    ifup)
        logger -t lan4-radio "LAN4 plugged in — enabling radio1"
        wifi up radio1
        ;;
    ifdown)
        setsid sh -c "
            sleep $DELAY_SECONDS
            STATE=\$(cat /sys/class/net/lan4/operstate 2>/dev/null)
            if [ \"\$STATE\" = \"down\" ]; then
                logger -t lan4-radio \"LAN4 still unplugged after ${DELAY_SECONDS}s — disabling radio1\"
                wifi down radio1
            fi
        " < /dev/null > /dev/null 2>&1 &
        ;;
esac

chmod +x /etc/hotplug.d/iface/95-lan4-radio