Custom Script to trigger output when WAN successfully ON

Hi,

Is anyone have example of custom script to trigger an output to turn on the LED when WAN connection (remotely access) is established?

1 Like

Hello,

The requirements are not quite clear for the complete solution, but below is a skeleton, of how a similar task can be achieved on RUT956:

  • Log into your router via SSH;

  • Execute command:

    • vi /etc/hotplug.d/iface/99-wan-connected
  • Press letter ‘i’ to enable editing;

  • Paste the following:

if [ "$ACTION" = "ifup" ] && [ "$INTERFACE" = "wan" ]; then
   # Execute the ubus command to update GPIO
   ubus call ioman.gpio.dout1 update '{"value":"1"}'
   logger -t hotplug "WAN interface is up, executing ubus command"
fi
  • Press Esc, write “:wq” an press Enter to finish editing, save the script and exit the text editor.

  • Execute command below to make the script executable:

    • chmod +x /etc/hotplug.d/iface/99-wan-connected

The script will enable digital output, after the WAN port is physically connected to the RUT956 and create a log entry informing about it.

I assume, you want the output to be triggered temporarily and be disabled automatically, therefore, you can add the following:

sleep 5
ubus call ioman.gpio.dout1 update '{"value":"0"}'

I also assume, you want the LED to be on only if the WAN interface has internet access. In this case, modify the script as follows to include a simple ping check:

#!/bin/sh

if [ "$ACTION" = "ifup" ] && [ "$INTERFACE" = "wan" ]; then
    # Check internet connectivity by pinging a Google's DNS server
    ping 8.8.8.8 -s 0 -c 3 -W 2 -q >/dev/null
    ret=$?
    if [ "$ret" -eq 0 ]; then
        # If ping is successful, set output to high
        ubus call ioman.gpio.dout1 update '{"value":"1"}'
        sleep 5
        ubus call ioman.gpio.dout1 update '{"value":"0"}'
    else
        # Log failure to detect internet connectivity
        logger -t hotplug "WAN interface is up but has no internet connectivity"
    fi
fi

If the ping check is successful, output will be triggered for 5 seconds and then disabled.

The ioman.gpio.dout1 is the output pin on the 4-pin connector.

The galvanically isolated open collector output is indicated as ioman.gpio.dout2

Relay is ioman.relay.relay0. To change its state use:

  • ubus call ioman.relay.relay0 update ‘{“state”:“open/closed”}’

Best regards,

1 Like

Also, instead of ubus, you can utilize router’s API, for example, to set the output on the 4-pin connector high, you can use:

  • api POST /io/dout1/actions/change_state ‘{“data”:{“value”:“0”}}’

Hi Sigismundus,

Appreciated your guidance. What actually my project requirement is when there is internet connectivity, it will turn on the LED that mounting on control panel to let the operator know the cellular network is good (can remote access to this modem anywhere from the world). That mean this LED will always in ON condition if the cellular network is good and off once the cellular network is failed or disconnected.

I’m newbie in writing the custom script; can you guide me in detail how to write the custom script into teltonika modem RUT955?

Hello,

In this case you will need to modify the suggested approach a bit.

Log into the router via SSH. The process is explained here: https://wiki.teltonika-networks.com/view/Command_Line_Interfaces#Windows

Execute command:

  • vi /etc/cellular_check.sh
  • Paste the following content:
#!/bin/sh

while true; do
    ping -I qmimux0 8.8.8.8 -s 0 -c 3 -W 2 -q >/dev/null
    ret=$?
    if [ "$ret" -eq 0 ]; then
        # If ping is successful, set output to high
        ubus call ioman.gpio.dout1 update '{"value":"1"}'
    else
        # Ping failure, mobile down, set output to low
        logger "Mobile connectivity loss"
        ubus call ioman.gpio.dout1 update '{"value":"0"}'
    fi
    sleep 10
done

  • The above script will send 3 ping packets over mobile interface to 8.8.8.8 of zero size and will wait for 2 seconds for a response. If pings succeed, mobile is considered online and output pin is set to high state. If pings fail, output is set to off. The process is repeated every 10 seconds. You can modify the frequency by adjusting the sleep value.

  • Execute command below to make the script executable:

    • chmod +x /etc/cellular_check.sh
  • Next, log into the router’s WebUI

  • Navigate to System → Maintenance → Custom scripts page

  • Add the following lines:

sleep 45
sh /etc/cellular_check.sh &

This way the script will be started at the end of the router’s boot sequence and remain running in the background among other device’s processes.

The script is basic, it does not take into consideration, whether interface qmimux0 is present at the time the script is executed, therefore I added 45 second delay to ensure the mobile connectivity gets a chance to be established. So there is room for improvement.

Best regards,

1 Like

Hi Sigismundus,

i will try it out when i have chance. Thank you so much for the guidance and support.

Best Regards,

Cryan

Hi Sigismundus,

i have tried as your suggested but nothing change to the output. Please find enclosed snapshot i did for your reference incase which steps i have been done wrongly. Kindly help.