Hotplug Script not triggered automatically

See updated script here - [Help] Auto Adjust WiFi Script

Hotplug to run this script [Help] Auto Adjust WiFi Script
#!/bin/bash

# Sample DHCP hotplug script
log_entry=$(logread | grep "STA" | tail -n 1)

# Debug statement
echo "Log entry: $log_entry"

# Check if log entry is not empty
if [ -n "$log_entry" ]; then
    # Extract MAC address and SSID
    mac_address=$(echo "$log_entry" | awk '{print $6}')
    ssid=$(echo "$log_entry" | awk -F: '{print $4}' | awk '{print $1}')

    # Debug statements
    echo "MAC Address: $mac_address"
    echo "SSID: $ssid"

    # Define an array of SSIDs
    supported_ssids=("Wifi_Main_2Ghz" "Another_SSID")

    # Iterate through each SSID
    for supported_ssid in "${supported_ssids[@]}"; do
        if [ "$ssid" == "$supported_ssid" ]; then
            # Debug statement
            echo "Matching SSID found: $supported_ssid"

            # Run your custom script with the necessary parameters
            /path/to/your/custom/script.sh "$mac_address" "$ssid"
            break  # Exit the loop since we found a matching SSID
        fi
    done
else
    # Debug statement if log entry is empty
    echo "No relevant log entry found."
fi

Works Manually

root@OpenWrt:/etc/hotplug.d/dhcp# ./Wifi_Client.sh
Log entry: Sat Dec 16 12:55:45 2023 daemon.info hostapd: Wifi_Main_5Ghz: STA 00:00:00:00:00:00 WPA: pairwise key handshake completed (RSN)
MAC Address: daemon.info
SSID: Wifi_Main_5Ghz
Matching SSID found: Wifi_Main_5Ghz
I: WifiPrivate2Ghz, P: phy0
I: WifiPrivate5Ghz, P: phy1
I: Wifi_Main_2Ghz, P: phy0
I: Wifi_Main_5Ghz, P: phy1
STATIONS_PRESENT: true
WIFIPOWER: Device: phy0 set to: 2000
STATIONS_PRESENT: true
WIFIPOWER: Device: phy1 set to: 2100
root@OpenWrt:/etc/hotplug.d/dhcp#

hotplug not triggered automatically with wifi connections

logread | grep hotplug is empty

hotplug-call dhcp

root@OpenWrt:/etc/hotplug.d/dhcp# hotplug-call dhcp
Log entry: Sat Dec 16 12:59:08 2023 daemon.info hostapd: Wifi_Main_2Ghz: STA 00:00:00:00:00:00 WPA: pairwise key handshake completed (RSN)
/sbin/hotplug-call: /etc/hotplug.d/dhcp/Wifi_Client.sh: line 20: syntax error: unexpected "(" (expecting "fi")

placed hotplug script in /etc/hotplug.d/dhcp

hotplug status start enable

root@OpenWrt:/etc/hotplug.d/dhcp# /etc/init.d/hotplug status
-ash: /etc/init.d/hotplug: not found
root@OpenWrt:/etc/hotplug.d/dhcp# /etc/init.d/hotplug start
-ash: /etc/init.d/hotplug: not found
root@OpenWrt:/etc/hotplug.d/dhcp# /etc/init.d/hotplug enable
-ash: /etc/init.d/hotplug: not found
root@OpenWrt:/etc/hotplug.d/dhcp# /etc/init.d/procd restart
-ash: /etc/init.d/procd: not found

ps | grep procd

root@OpenWrt:~# ps | grep procd
    1 root      1980 S    /sbin/procd
14498 root      1376 S    grep procd

hotplug-call all

root@OpenWrt:~# hotplug-call all
root@OpenWrt:~#

logread | grep Wifi_Client.sh

root@OpenWrt:~# logread | grep Wifi_Client.sh
root@OpenWrt:~#

I think it is the above line, just set -x to debug or comment the line and then you will see it stops further in the script.

You are using arrays which are not available in the regular shell.
My theory: when the hotplug script is called it will spawn a shell (a)sh process

You really do not need bash arrays here.
When using a space delimited string you can easily loop through it.

For the record, posix compliant arrays are possible with some help and thanks to @antonk POSIX arrays implementation
But as said you really do not need it here

Yes a missing execute flag on the script file would be missing but there is no need to set -x within the script.
Just use sh -x your.sh from the cli to debug.

Ps: @RSHARM could you please fix the formatting of your initial post? Thanks.

1 Like

Thanks. hotplug-call dhcp triggers the script but it doesn't show in logs
http://192.168.1.1/cgi-bin/luci/admin/status/logs/syslog
http://192.168.1.1/cgi-bin/luci/admin/status/logs/dmesg

Is echo or other output on stout really written to syslog?

Use logger. It is dedicated to write to syslog. See https://openwrt.org/docs/guide-user/base-system/log.essentials

you do many random stuff and seems to ignore all error messages. i try to go through the list of problems here:

root@OpenWrt:/etc/hotplug.d/dhcp# hotplug-call dhcp
Log entry: Sat Dec 16 12:59:08 2023 daemon.info hostapd: Wifi_Main_2Ghz: STA 00:00:00:00:00:00 WPA: pairwise key handshake completed (RSN)
/sbin/hotplug-call: /etc/hotplug.d/dhcp/Wifi_Client.sh: line 20: syntax error: unexpected "(" (expecting "fi")

the source of your problem is manifested here clearly.
a) hotplug scripts naming convention is "number-scriptname" where number helps to define order. it is not mandatory but worth to follow.
b) hotplug scripts are not executed but sourced! so defining bash as interpreter will have no impact at all, your code runs in ash which does not support arrays. you should fix your code, or create a hotplug which calls out the script.

# cat /sbin/hotplug-call
#!/bin/sh
# Copyright (C) 2006-2016 OpenWrt.org

export HOTPLUG_TYPE="$1"

. /lib/functions.sh

PATH="/usr/sbin:/usr/bin:/sbin:/bin"
LOGNAME=root
USER=root
export PATH LOGNAME USER
export DEVICENAME="${DEVPATH##*/}"

if [ \! -z "$1" -a -d /etc/hotplug.d/$1 ]; then
        for script in $(ls /etc/hotplug.d/$1/* 2>&-); do (
                [ -f $script ] && . $script
        ); done
fi
root@OpenWrt:/etc/hotplug.d/dhcp# /etc/init.d/hotplug status
-ash: /etc/init.d/hotplug: not found
root@OpenWrt:/etc/hotplug.d/dhcp# /etc/init.d/hotplug start
-ash: /etc/init.d/hotplug: not found
root@OpenWrt:/etc/hotplug.d/dhcp# /etc/init.d/hotplug enable
-ash: /etc/init.d/hotplug: not found
root@OpenWrt:/etc/hotplug.d/dhcp# /etc/init.d/procd restart
-ash: /etc/init.d/procd: not found

the error message is very clear in stating such file /etc/init.d/hotplug or /etc/init.d/procd does not exist. why you try to call with start / enable parameters too ???

root@OpenWrt:~# logread | grep Wifi_Client.sh
root@OpenWrt:~#

if you want to see messages in system log you should use logger utility.

  1. Are scripts in /etc/hotplug.d/dhcp also triggered when dhcp disconnects ? If yes how to only run it for only when it connects ?
  2. Is there a folder for wifi too ?
  3. Unrelated to Hotplugs how to limit guest ssid devices from connecting-dis-connecting. Is there a option like If a device disconnects they can only connect to SSID after 15 secs.

At least hostapd is able to kick a client and set a ban time to prevent a station of connecting again within this timeframe.
Not sure about hotplug capabilities of station disconnects. Maybe there is something, maybe you have to implement your own watcher...

How to use that ?

Please try a search engine next time first.
First Google hit on Openwrt wpa kick station...

I assume this info is also present somewhere in the Openwrt wiki or in the forum here....