Triggering script on kernel event for cable plugged in to LAN/WAN port

I want to trigger execution of a script when a cable is plugged in to the LAN or WAN port of my GL-MT300N-V2. OpenWrt 21.02.0.

hotplug script on iface/net - nothing happens. "ubus monitor", also no events listed.

logread -f on WAN plugged (for example):

Plugged: kern.info kernel: [ 5052.874092] rt3050-esw 10110000.esw: link changed 0x03
Unplugged: kern.info kernel: [ 5051.140355] rt3050-esw 10110000.esw: link changed 0x02

Polling the switch state would be possible, but is not favoured. I can get the status with "swconfig dev switch0 port 0 get link" for WAN; LAN is port 6.

Is there an event driven solution for this possible?

Thanks for any suggestion.

No. The kernel log you see is printed by rt3050-esw's IRQ handler, but it does not notify the userspace.

1 Like

Thank you. Might be a feature to include in future OpenWrt versions - hotplug events on switch changes.

In our device, we must add hack to our setup. We are using simple script which sits on /dev/kmsg and is parsing every line. It is then easy to catch link changed 0x events and depending on the number, do proper action. We are then calling ubus call network.interface.lan "up" or "down" depending on the value.

2 Likes

Great suggestion, thank you! I will implement it this way.

EDIT: came up with the following script, started in background very early in an init.d script.

#!/usr/bin/awk -f

/rt3050-esw [^ ]* link changed 0x00/ { \
    system( "ubus call network.interface.lan down; \
         ubus call network.interface.wan down") }
/rt3050-esw [^ ]* link changed 0x01/ { \
    system( "ubus call network.interface.lan down; \
         ubus call network.interface.wan up" ) }
/rt3050-esw [^ ]* link changed 0x02/ { \
    system( "ubus call network.interface.lan up; \
         ubus call network.interface.wan down" ) }
/rt3050-esw [^ ]* link changed 0x03/ { \
    system( "ubus call network.interface.lan up; \
         ubus call network.interface.wan up" ) }
1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.