Add auto connection
This commit is contained in:
parent
7f0d914e41
commit
070c1c6d0e
|
@ -0,0 +1,72 @@
|
|||
Script Breakdown:
|
||||
Check Internet: Pings Google's DNS (8.8.8.8) to verify the connection.
|
||||
Scan Wi-Fi: Scans available Wi-Fi networks using iw dev radio2 scan.
|
||||
Attempt Connections: Tries to connect to each SSID using its MAC address as the password.
|
||||
Notify on Success: If successful, sends a notification via NTFY with the SSID and the used MAC address as the password.
|
||||
Script:
|
||||
bash
|
||||
Copy code:
|
||||
---------
|
||||
|
||||
#!/bin/sh
|
||||
|
||||
# Check if there is an internet connection
|
||||
if ! ping -c 3 8.8.8.8 > /dev/null 2>&1; then
|
||||
# Scan for available Wi-Fi networks using radio2
|
||||
iw dev radio2 scan | grep "SSID\|BSSID" | while read -r line; do
|
||||
if echo "$line" | grep "SSID" > /dev/null; then
|
||||
ssid=$(echo "$line" | cut -d ":" -f2 | sed 's/^ *//g')
|
||||
elif echo "$line" | grep "BSSID" > /dev/null; then
|
||||
bssid=$(echo "$line" | cut -d ":" -f2- | sed 's/://g' | tr 'a-z' 'A-Z')
|
||||
# Try to connect to the SSID using the BSSID as password
|
||||
uci set wireless.@wifi-iface[1].ssid="$ssid"
|
||||
uci set wireless.@wifi-iface[1].key="$bssid"
|
||||
uci set wireless.@wifi-iface[1].encryption='psk2'
|
||||
uci commit wireless
|
||||
wifi down radio2 && wifi up radio2
|
||||
sleep 30 # Wait for connection
|
||||
|
||||
# Check if the router has internet access
|
||||
if ping -c 3 8.8.8.8 > /dev/null 2>&1; then
|
||||
# Send a notification via NTFY
|
||||
curl -d "Connected to SSID: $ssid with password: $bssid" https://ntfy.asadqi.eu.org/Router
|
||||
sleep 300 # Wait for 5 minutes before retrying
|
||||
exit 0
|
||||
fi
|
||||
fi
|
||||
done
|
||||
fi
|
||||
|
||||
|
||||
------------
|
||||
|
||||
Explanation:
|
||||
Internet Check: The script first pings 8.8.8.8. If it fails, it proceeds to scan for available SSIDs.
|
||||
Wi-Fi Scan: It uses iw dev radio2 scan to list all available SSIDs and BSSIDs (MAC addresses).
|
||||
Connect Attempt: For each SSID, it sets the SSID and uses the MAC address (without : and in uppercase) as the password. It commits these settings, brings down the radio, and brings it back up to apply the new settings.
|
||||
Wait and Check: After 30 seconds, it checks if the connection is successful by pinging 8.8.8.8 again. If successful, it sends a notification with the SSID and password via NTFY.
|
||||
Retry: If the connection fails, it continues with the next SSID.
|
||||
Cron Job to Run Every 30 Minutes:
|
||||
To schedule this script to run every 30 minutes, add the following line to your Scheduled Tasks in the OpenWRT GUI:
|
||||
|
||||
bash
|
||||
Copy code:
|
||||
---------
|
||||
|
||||
*/30 * * * * /usr/bin/wifi_connect.sh
|
||||
|
||||
------------
|
||||
|
||||
How to Apply:
|
||||
Save the Script: Save the above script as /usr/bin/wifi_connect.sh:
|
||||
|
||||
bash
|
||||
Copy code
|
||||
nano /usr/bin/wifi_connect.sh
|
||||
chmod +x /usr/bin/wifi_connect.sh
|
||||
Set the Cron Job:
|
||||
|
||||
Go to System -> Scheduled Tasks in the OpenWRT GUI.
|
||||
Add the cron line: */30 * * * * /usr/bin/wifi_connect.sh
|
||||
Click Save & Apply.
|
||||
This setup will run the script every 30 minutes to check for internet, scan SSIDs, and attempt to connect using the MAC address as the password.
|
Loading…
Reference in New Issue