Skip to content

Instantly share code, notes, and snippets.

@markizano
Created November 1, 2023 02:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markizano/ddd3c7697bf42ca22b69a65603090c39 to your computer and use it in GitHub Desktop.
Save markizano/ddd3c7697bf42ca22b69a65603090c39 to your computer and use it in GitHub Desktop.
Connect to Wi-Fi service for sysv-init.
# Set NETFILE to the location containing the wpa_supplicant network configuration.
# Uncomment the line that is the active network.
export NETFILE=/etc/wifi/My-NET.wifi
#export NETFILE=/etc/wifi/Work-NET.wifi
#export NETFILE=/etc/wifi/Public.wifi
#!/bin/bash
### BEGIN INIT INFO
# Provides: rtlwifi
# Required-Start: $local_fs $syslog
# Required-Stop: $local_fs $syslog
# Default-Start: 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start/stop wifi connection to home.
### END INIT INFO
export WPA_PIDFILE=/run/wifi/wpa_supplicant.pid
export DC_PIDFILE=/run/wifi/dhclient.pid
export IW_IFACE=wlan0
[ $UID -eq 0 ] || { echo "I'd prefer it if you had some authority..."; exit 1; }
. /lib/lsb/init-functions
[ -f /etc/default/wifi ] && . /etc/default/wifi
[ -d /run/wifi ] || mkdir /run/wifi
. common.sh
##### START ####
function start_wpa {
ifconfig $IW_IFACE up 0.0.0.0
wpa_supplicant -B -s -i $IW_IFACE -c "$NETFILE" -P "$WPA_PIDFILE" -Dwext || {
log_failure_msg "Problems starting WPA Supplicant"
return $RETVAL
}
log_success_msg "WPA Supplicant started. Pid: $(<$WPA_PIDFILE)"
return $RETVAL
}
function start_dhclient {
# ifconfig $IW_IFACE up $WIFI_IP_HOME_BASE
dhclient -pf "$DC_PIDFILE" -v $IW_IFACE |& NO_STDERR=1 log dhclient || {
log_failure_msg "Problems starting dhclient"
return $RETVAL
}
if [ -s "$DC_PIDFILE" ]; then
log_success_msg "dhclient started. Pid: $(<$DC_PIDFILE)"
return $RETVAL
else
log_warn_msg "dhclient started, but no PID file??"
fi
}
function start_wifi {
if [ -f "$WPA_PIDFILE" -a -f "$DC_PIDFILE" ]; then
log_success_msg "Wifi already connected."
return 1
fi
start_wpa || exit;
sleep 2s
start_dhclient || exit;
return $?
}
##### STOP #####
function stop_wpa {
if [ ! -f "$WPA_PIDFILE" ]; then
log_success_msg 'Wifi not associated'
return 0
fi
local PID="$(<$WPA_PIDFILE)"
if ! kill -15 $PID; then
log_failure_msg "SIGTERM on $PID failed. Demanding halt by SIGKILL"
kill -9 $PID || log_failure_msg 'You will want to verify WPA Supplicant was stopped cleanly.'
fi
rm $WPA_PIDFILE
return 1
}
function stop_dhclient {
if [ ! -f $DC_PIDFILE ]; then
log_success_msg 'dhclient not running'
return 0
fi
local PID="$(<$DC_PIDFILE)"
if ! kill -15 $PID; then
log_failure_msg "SIGTERM on $PID failed. Demanding halt by SIGKILL"
kill -9 $PID || log_failure_msg 'You will want to verify WPA Supplicant was stopped cleanly.'
fi
rm $DC_PIDFILE
return 1
}
function stop_wifi {
stop_dhclient
stop_wpa
ifconfig $IW_IFACE down
return $?
}
##### STATUS #####
function running_since {
mtime="@`stat -c %Y /proc/$2`"
printf 'PID of %s(%s), Running since %s\n' "$1" "$2" "`date +'%F %T' --date $mtime`"
}
# Return 0 for off and 1 for running
function wifi_status {
if [ -f $WPA_PIDFILE ]; then
log_success_msg 'Wifi appears to be associated.'
if [ -f $DC_PIDFILE ]; then
log_success_msg 'dhclient appears to be running.'
iwconfig $IW_IFACE
ifconfig $IW_IFACE
[ -f $WPA_PIDFILE ] && [ -d /proc/$(<$WPA_PIDFILE) ] && running_since WPA\ Supplicant $(<$WPA_PIDFILE)
[ -f $DC_PIDFILE ] && [ -d /proc/$(<$DC_PIDFILE) ] && running_since dhclient $(<$DC_PIDFILE)
return 1
fi
log_failure_msg 'dhclient appears to be off.'
return 0
fi
log_failure_msg 'Wifi appears to be unassociated.'
return 0
}
case "$1" in
start)
start_wifi
;;
stop)
stop_wifi
;;
restart)
$0 stop
$0 start
;;
status)
wifi_status
;;
*)
echo "Usage $0 [start|stop|restart|status]" > /dev/stderr
exit 0
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment