archiso/iwd-connect

82 lines
1.7 KiB
Text
Raw Permalink Normal View History

2024-06-14 07:14:44 +00:00
#!/usr/bin/env bash
iwd_station=$(iwctl station list | grep connected | awk '{print $(NF-1)}')
[ ${iwd_station} ] || exit 1
clear
iwctl station ${iwd_station} get-networks
while true
do
unset hidden_yes_no
if [ ${1} ]
then
ssid=${1}
else
read -r -p 'Network SSID: ' ssid
fi
if iwctl known-networks list | grep -q -w ${ssid}
then
echo
break
elif iwctl station ${iwd_station} get-networks | grep -q -w ${ssid}
then
echo
break
else
until [[ ${hidden_yes_no} = [yYnN] ]]
do
read -n 1 -r -p 'Is network hidden (y/n): ' hidden_yes_no
[[ ${hidden_yes_no} = [yYnN] ]] || echo -e '\n\n\e[1;31mNot a valid answer, type "y" or "n"\e[0m'
done
echo -e '\n'
if [[ ${hidden_yes_no} = [yY] ]]
then
hidden_network='-hidden'
break
fi
fi
done
while true
do
if iwctl known-networks list | grep -q -w ${ssid}
then
if ! iwctl station ${iwd_station} connect ${ssid} "$2"
then
iwctl station ${iwd_station} connect-hidden ${ssid} "$2"
fi
else
echo -e -n "Enter wifi passphrase for ${ssid} (or leave blank if there is none): "
read -r -p '' wifipass
if [ -z ${wifipass} ]
then
wifipass='""'
fi
iwctl --passphrase ${wifipass} station ${iwd_station} connect${hidden_network} ${ssid} "$2"
fi
if iwctl station ${iwd_station} show | grep -w 'Connected network' | grep -q ${ssid}
then
unset wifipass
clear
iwctl station ${iwd_station} show
until iwctl station ${iwd_station} show | grep -q 'IPv4 address'
do
sleep 3
done
clear
iwctl station ${iwd_station} show
echo -e '\e[1;32mSuccess!\e[0m\n'
exit 0
else
echo -e '\n\e[1;31mInvalid passphrase, try again\e[0m'
fi
done