2022-02-24 15:46:22 +00:00
|
|
|
#!/bin/sh
|
|
|
|
# choose a wifi signal in range and try to connect to it
|
|
|
|
|
|
|
|
verb=0
|
2022-03-11 12:43:21 +00:00
|
|
|
action=start
|
|
|
|
|
|
|
|
while true ; do
|
|
|
|
case "$1" in
|
|
|
|
"-v")
|
|
|
|
verb=1
|
|
|
|
;;
|
|
|
|
"stop")
|
|
|
|
action=stop
|
|
|
|
;;
|
|
|
|
"start") ;;
|
|
|
|
"") break ;;
|
|
|
|
*)
|
|
|
|
echo "Invalid parameter '$1'"
|
|
|
|
break;;
|
|
|
|
esac
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
# Remaining args are garbage
|
|
|
|
if [ "$#" -ne 0 ] ; then
|
|
|
|
echo "Usage: $0 [-v] [start|stop]"
|
|
|
|
echo "-v verbose output"
|
|
|
|
echo "start or stop the wifi"
|
|
|
|
exit -1
|
|
|
|
fi
|
|
|
|
|
2022-02-24 15:46:22 +00:00
|
|
|
|
|
|
|
title="Wifi Selector"
|
|
|
|
id=""
|
|
|
|
notify () {
|
|
|
|
if [ -z "$id" ] ; then
|
|
|
|
id="$(dunstify -p "$title" "$@")"
|
|
|
|
else
|
|
|
|
dunstify -r "$id" "$title" "$@"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2022-03-11 12:43:21 +00:00
|
|
|
# Stop the wifi if asked
|
|
|
|
if [ "$action" == "stop" ] ; then
|
|
|
|
nmcli radio wifi off
|
|
|
|
notify "Wifi stopped"
|
|
|
|
exit "$?"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Else, start it if needed
|
2022-02-24 15:46:22 +00:00
|
|
|
if ( nmcli radio wifi | grep disabled > /dev/null ) ; then
|
|
|
|
notify "Turning on"
|
|
|
|
nmcli radio wifi on
|
2022-03-11 12:43:21 +00:00
|
|
|
sleep 2
|
2022-02-24 15:46:22 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
notify "Scanning networks in range..."
|
|
|
|
nmcli device wifi rescan
|
|
|
|
|
2022-02-25 12:34:57 +00:00
|
|
|
ssid="$(nmcli -f IN-USE,SSID,BARS d wifi list | dmenu -l 8)"
|
|
|
|
ssid="$(echo "${ssid:2:-5}" | sed 's/^ *//g' | sed 's/ *$//g')"
|
2022-02-24 15:46:22 +00:00
|
|
|
|
2022-02-25 12:34:57 +00:00
|
|
|
# We get a clean version of SSIDs. It is a fix to connect to wifis that begin or end with spaces
|
2023-01-24 22:56:39 +00:00
|
|
|
ssid="$(nmcli -f SSID -m multiline -t -c no d wifi list | grep -F -- "$ssid")"
|
2022-02-25 12:34:57 +00:00
|
|
|
ssid="${ssid:5}"
|
|
|
|
|
|
|
|
[ $verb -gt 0 ] && echo "ssid: $ssid"
|
2022-02-24 15:46:22 +00:00
|
|
|
[ -z "$ssid" ] && notify "No SSID selected" && exit 1
|
|
|
|
|
|
|
|
notify "Connecting to $ssid"
|
2023-01-24 22:56:39 +00:00
|
|
|
#out="$(nmcli d wifi connect "$ssid" 2>&1 | grep -i error)"
|
|
|
|
out="$(nmcli con up "$ssid" 2>&1 | grep -i error)"
|
2022-02-24 15:46:22 +00:00
|
|
|
res="$?"
|
2023-01-24 22:56:39 +00:00
|
|
|
# 10 If the connexion does not exist already #
|
|
|
|
# 4 If the SSID got multiple bad connexion names #
|
|
|
|
if [ "$res" -ne 0 ] ; then
|
|
|
|
out2="$out"
|
|
|
|
out="$(nmcli device wifi connect "$ssid" 2>&1 | grep -i error)"
|
|
|
|
res="$?"
|
|
|
|
fi
|
|
|
|
if [ -n "$(echo $out | grep -i 'secrets were required')" ] ; then
|
|
|
|
out2="$out2$out"
|
|
|
|
notify "Need password"
|
|
|
|
pass="$(zenity --password)"
|
|
|
|
out="$(nmcli device wifi connect "$ssid" --password "$pass" 2>&1 | grep -i error)"
|
|
|
|
res="$?"
|
|
|
|
fi
|
|
|
|
|
|
|
|
[ -z "$out" ] && notify "Connected :)" || notify "Error connecting :(\n$out\n$out2"
|