-
Notifications
You must be signed in to change notification settings - Fork 1
/
wifi_reconnect.sh
executable file
·76 lines (56 loc) · 1.77 KB
/
wifi_reconnect.sh
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
66
67
68
69
70
71
72
73
74
75
76
#!/bin/bash
# Automatic reconnect WiFi/WLAN on connection loss
#
# https://blog.spaps.de/workaround-for-raspberry-pi-automatic-wifi-wlan-reconnect/
#
# init 20230218
#
# disable power management
# sudo iwconfig wlan0 power off
# https://www.elektronik-kompendium.de/sites/raspberry-pi/1912231.htm
#
# sudo nano /etc/network/interfaces:
#
# Folgende Zeilen eintragen bzw. fehlende Zeilen ergänzen:
#
# allow-hotplug wlan0
# iface wlan0 inet manual
# post-up iw wlan0 set power_save off
# get system uptime in minutes:
# echo $(awk '{print $1}' /proc/uptime) / 60 | bc
#
# check, if servicex is running:
# systemctl is-active --quiet servicex && echo Servicex is running
uptimeminutes=$( echo $( awk '{print $1}' /proc/uptime ) / 60 | bc )
if [[ $uptimeminutes -lt 4 ]]; then
exit
fi
verbose=1
if [[ $1 = "-v" ]]; then verbose=1 ;fi
NOW=$(date +%Y%m%d-%H%M%S)
# The IP for the server you wish to ping (get default getway)
SERVER=$(/sbin/ip route | awk '/default/ { print $3 }')
if [[ $verbose = 1 ]] ; then
echo "$NOW Server: ${SERVER}" >>/var/log/wifi_reconnect
fi
# Specify wlan interface
WLANINTERFACE=wlan0
if [[ $verbose = 1 ]]; then
echo "$NOW Interface: ${WLANINTERFACE}" >>/var/log/wifi_reconnect
fi
# Only send two pings, sending output to /dev/null
ping -I ${WLANINTERFACE} -c2 ${SERVER} >/dev/null
# If the return code from ping ($?) is not 0 (meaning there was an error)
if [ $? != 0 ]; then
# systemctl is-active --quiet inetradio && echo "$NOW Restarted WiFi" >> /var/log/wifi_reconnect
# Restart the wireless interface
ip link set wlan0 down
ip link set wlan0 up
# /usr/sbin/iwconfig wlan0 power off
# Restart our internet radio
# systemctl restart inetradio
else
if [[ $verbose = 1 ]] ; then
echo "$NOW WiFi works. No restart" >> /var/log/wifi_reconnect
fi
fi