-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-wifi
More file actions
executable file
·65 lines (57 loc) · 1.99 KB
/
check-wifi
File metadata and controls
executable file
·65 lines (57 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/bin/bash
#
# started by cron every five minutes
#
# check if the wifi connection is still alive
#
# if not than restart connection
#
# echo on
# set -x
# --- Configuration ---
PING_TARGET="192.168.1.1" # A reliable IP on the local network (e.g., router)
WIFI_INTERFACE="wlan0"
MAX_RETRIES=3
RETRY_DELAY=15 # Seconds to wait after bringing interface up
# --- Script ---
SCRIPT_NAME="${0##*/}"
# Detect if running in an interactive terminal.
# This allows us to suppress output for cron and prevent reboots during manual runs.
if [ -t 1 ]; then
IS_INTERACTIVE=true
else
IS_INTERACTIVE=false
fi
# check if there is Wifi
# does it have an address?
if ping -c 1 -W 1 "$PING_TARGET" &> /dev/null; then
logger -t "$SCRIPT_NAME" "Connection to $PING_TARGET is up."
if $IS_INTERACTIVE; then
echo "Connection is up."
fi
else
logger -t "$SCRIPT_NAME" "Connection down! Attempting reconnection..."
echo "$SCRIPT_NAME: Connection down! Attempting reconnection..."
for ((i=1; i<=MAX_RETRIES; i++)); do
logger -t "$SCRIPT_NAME" "Reconnection attempt $i of $MAX_RETRIES..."
echo "Reconnection attempt $i of $MAX_RETRIES..."
# Restart the interface
ip link set "$WIFI_INTERFACE" down
sleep 5
ip link set "$WIFI_INTERFACE" up
sleep "$RETRY_DELAY" # Wait for it to reconnect and get an IP
if ping -c 1 -W 1 "$PING_TARGET" &> /dev/null; then
logger -t "$SCRIPT_NAME" "Connection restored."
echo "Connection restored."
exit 0
fi
done
logger -t "$SCRIPT_NAME" "All reconnection attempts failed. Rebooting."
if $IS_INTERACTIVE; then
echo "All reconnection attempts failed. Would reboot if not in an interactive session."
else
echo "All reconnection attempts failed. Rebooting."
# This command requires root privileges.
/sbin/reboot
fi
fi