Last active
February 24, 2024 10:00
-
-
Save giu1io/d8d4695325a8d5cc429f to your computer and use it in GitHub Desktop.
Shell script that loads the WPA_supplicant configuration and use it to connect to available networks, if none is available it an AP is created. Include Wi-Fi watchdog service that checks that the connection is always working.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
WPA_SUPPLICANT_CONF="/etc/wpa_supplicant/wpa_supplicant.conf" | |
# this funcion is called once the connection is established, | |
# in this case a boot sound will be played to notify the user that everything is ready. | |
function connected { | |
aplay /root/Windows3.1.wav 2>&1 >/dev/null & | |
} | |
# function that tries to establish a connection, get a network in $temp from the WPA file | |
# and then goes through all available networks ($essid) to find a match. | |
function testConnect { | |
while [ $x -lt ${#essid[@]} ]; do | |
if [ "${temp[ssid]}" = "${essid[$x]}" ] | |
then | |
echo "Trying to connect to ${temp[ssid]}" | |
ssid="${temp[ssid]}" | |
ssid="${ssid%\"}" | |
ssid="${ssid#\"}" | |
iwconfig wlan0 essid "$ssid" | |
dhclient wlan0 | |
connected | |
exit | |
fi | |
(( x++ )) | |
done | |
} | |
a=0 | |
b=0 | |
x=0 | |
declare -a essid | |
declare -a address | |
# load in $essid and $address all available networks | |
while read line | |
do | |
case $line in | |
*ESSID* ) | |
line=${line#*ESSID:} | |
essid[$a]="$line" | |
a=$((a + 1)) | |
;; | |
*Address*) | |
line=${line#*Address:} | |
address[$b]="$line" | |
b=$((b + 1)) | |
;; | |
esac | |
done < <(iwlist wlan0 scanning) | |
#load in $temp a network at time from the WPA supplicant file | |
declare -A temp | |
i=0 | |
flag=0 | |
while read line | |
do | |
case "$line" in | |
"network={") | |
flag=1 | |
;; | |
"}") | |
#echo "${temp[*]}" | |
testConnect temp[@] | |
((i++)) | |
flag=0 | |
;; | |
*) | |
if [ "$flag" -eq 1 ]; then | |
IFS='=' read -a array <<< "$line" | |
key="${array[0]}" | |
temp[$key]="${array[1]}" | |
fi | |
esac | |
done < $WPA_SUPPLICANT_CONF | |
# if a connection hasn't been established create an hotspot | |
if ifconfig wlan0 | grep -q "inet addr:" ; then | |
echo "Already Connected" | |
# start the watchdog service | |
service wifi_watchdog start | |
connected | |
else | |
#we don't know if the watchdog service is running so stop it 'cause I haven't test it yet | |
service wifi_watchdog stop | |
echo "Network connection down! Inizializing hotspot:" | |
# set a static IP | |
ifconfig wlan0 192.168.0.1 netmask 255.255.255.0 | |
# inizialize hotspot | |
hostapd -B -dd /etc/hostapd/hostapd.conf | |
# start dhcp | |
service udhcpd start | |
connected | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# source: http://raspberrypi.stackexchange.com/a/5121 | |
# make sure we aren't running already | |
what=`basename $0` | |
for p in `ps h -o pid -C $what`; do | |
if [ $p != $$ ]; then | |
exit 0 | |
fi | |
done | |
# source configuration | |
wlan=wlan0 | |
#set router ip = gateway | |
router_ip=$(route -n | grep 'wlan0$' | grep '^0\.0\.0\.0' | awk '{print $2}') | |
log=/var/log/wifi.log | |
check_interval=120 | |
exec 1> /dev/null | |
exec 2>> $log | |
echo $(date) > $log | |
# without check_interval set, we risk a 0 sleep = busy loop | |
if [ ! "$check_interval" ]; then | |
echo "No check interval set!" >> $log | |
exit 1 | |
else sleep $check_interval | |
fi | |
restartWifi () { | |
dhclient -v -r | |
# make really sure | |
killall dhclient | |
ifconfig $wlan down | |
ifconfig $wlan up | |
#launch script to connect wifi | |
/root/load_wifi.sh & | |
} | |
while [ 1 ]; do | |
ping -c 1 $router_ip & wait $! | |
if [ $? != 0 ]; then | |
echo $(date)" attempting restart..." >> $log | |
restartWifi | |
fi | |
sleep $check_interval | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/sh | |
### BEGIN INIT INFO | |
# Provides: wifi_watchdog | |
# Required-Start: $remote_fs $syslog | |
# Required-Stop: $remote_fs $syslog | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Short-Description: Watchdog service for Wi-Fi | |
# Description: This script ping the router every N seconds and if it fails | |
# tries to restore the network connection. | |
### END INIT INFO | |
# this file goes in /etc/init.d/ | |
# Author: Giulio Montagner | |
# Do NOT "set -e" | |
# PATH should only include /usr/* if it runs after the mountnfs.sh script | |
DESC="Description of the service" | |
NAME=wifi_watchdog | |
DAEMON=/root/$NAME.sh | |
DAEMON_PS=/[r]oot/$NAME.sh | |
DAEMON_ARGS="--options args" | |
PIDFILE=/var/run/$NAME.pid | |
SCRIPTNAME=/etc/init.d/$NAME | |
#Include functions | |
. /lib/lsb/init-functions | |
# | |
# Function that starts the daemon/service | |
# | |
do_start() | |
{ | |
# Return | |
# 0 if daemon has been started | |
# 1 if daemon was already running | |
# 2 if daemon could not be started | |
# make sure we aren't running already | |
what=$(basename $DAEMON) | |
for p in `ps h -o pid -C $what`; do | |
if [ $p != $$ ]; then | |
return 1 | |
fi | |
done | |
$DAEMON & | |
return 0 | |
} | |
# | |
# Function that stops the daemon/service | |
# | |
do_stop() | |
{ | |
# Return | |
# 0 if daemon has been stopped | |
# 1 if daemon was already stopped | |
# 2 if daemon could not be stopped | |
# other if a failure occurred | |
kill -9 $(ps aux | grep "$DAEMON_PS" | awk '{print $2}') | |
} | |
# | |
# Function that sends a SIGHUP to the daemon/service | |
# | |
do_reload() { | |
# | |
# If the daemon can reload its configuration without | |
# restarting (for example, when it is sent a SIGHUP), | |
# then implement that here. | |
# | |
return 0 | |
} | |
case "$1" in | |
start) | |
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME" | |
do_start | |
case "$?" in | |
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; | |
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; | |
esac | |
;; | |
stop) | |
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME" | |
do_stop | |
case "$?" in | |
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; | |
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; | |
esac | |
;; | |
status) | |
ps aux | grep "$DAEMON_PS" | |
;; | |
#reload|force-reload) | |
# | |
# If do_reload() is not implemented then leave this commented out | |
# and leave 'force-reload' as an alias for 'restart'. | |
# | |
#log_daemon_msg "Reloading $DESC" "$NAME" | |
#do_reload | |
#log_end_msg $? | |
#;; | |
restart|force-reload) | |
# | |
# If the "reload" option is implemented then remove the | |
# 'force-reload' alias | |
# | |
log_daemon_msg "Restarting $DESC" "$NAME" | |
do_stop | |
case "$?" in | |
0|1) | |
do_start | |
case "$?" in | |
0) log_end_msg 0 ;; | |
1) log_end_msg 1 ;; # Old process is still running | |
*) log_end_msg 1 ;; # Failed to start | |
esac | |
;; | |
*) | |
# Failed to stop | |
log_end_msg 1 | |
;; | |
esac | |
;; | |
*) | |
#echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2 | |
echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2 | |
exit 3 | |
;; | |
esac | |
: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment