2021-04-15 22:45:15 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# wpa-connect.sh
|
|
|
|
# Dumb wpa_supplicant wrapper to connect to wifi or create a new wpa profile
|
|
|
|
# Depends on wpa_supplicant and iw
|
|
|
|
#
|
|
|
|
# by x1phosura
|
|
|
|
|
|
|
|
# If the script fails, you can always run the following AS ROOT:
|
|
|
|
# wpa_supplicant -B -i wlp3s0 -c <(wpa_passphrase profile_name passphrase) # && dhcpcd
|
|
|
|
|
|
|
|
# wireless networking interface
|
|
|
|
wint="wlp4s0"
|
|
|
|
|
|
|
|
# where wpa_supplicant config files are stored on the system
|
|
|
|
wpa_conf_path="/etc/wpa_supplicant"
|
|
|
|
# list of profiles in the wpa_supplicant config path
|
|
|
|
# shellcheck disable=SC2012
|
|
|
|
profile_list="$(ls /etc/wpa_supplicant | sed 's/.conf//g')" # strips the conf
|
|
|
|
|
|
|
|
default="# to get the psk, do wpa_passphrase \\\"profile_name\\\" \\\"passphrase\\\"\n"
|
|
|
|
|
|
|
|
sudo_validate() {
|
|
|
|
if [[ $(id -u) != 0 ]]; then # if not already root
|
|
|
|
sudo -v
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
print_usage() {
|
|
|
|
echo -ne \
|
|
|
|
"Usage: "$(basename $0)" [ -dshln ] [ -c profile_name ]\n\n" \
|
|
|
|
"Options:\n" \
|
|
|
|
"-d --disconnect\n" \
|
|
|
|
" Kill current wpa_supplicant process and disconnect\n" \
|
|
|
|
"-s --scan\n" \
|
|
|
|
" Scan for and list nearby SSIDS\n" \
|
|
|
|
"-h --help\n" \
|
|
|
|
" Displau this message\n" \
|
|
|
|
"-l --list --list-profiles\n" \
|
|
|
|
" List current available WPA/2 profiles\n" \
|
|
|
|
"-n --new\n" \
|
|
|
|
" Create a new wpa_supplicant profile (BETA feature)\n" \
|
|
|
|
"-c --connect [profile_name]\n" \
|
|
|
|
" Connect to the provided WPA/2 profile 'profile_name'\n"
|
|
|
|
}
|
|
|
|
|
|
|
|
make_new_wpa_profile() {
|
|
|
|
printf "BETA feature!: Currently assumes a basic home WPA/2 setup (just "
|
|
|
|
printf "requires a password to log in).\n"
|
|
|
|
echo "Enter the config file's name (usually NetworkName.conf): "
|
|
|
|
read wpa_profile_name
|
|
|
|
echo "Enter the network's name (SSID) unescaped: "
|
|
|
|
read ssid
|
|
|
|
echo "Does the network need a password (PSK)? [y/N]: "
|
|
|
|
read has_psk
|
|
|
|
|
|
|
|
sudo_validate
|
|
|
|
sudo touch "$wpa_conf_path"/"$wpa_profile_name"
|
|
|
|
|
|
|
|
if [[ "$has_psk" != "" && $(printf "${has_psk:0:1}" | tr Y y) = "y" ]] ; then
|
|
|
|
echo "Enter the network's password (psk): "
|
|
|
|
read pass
|
|
|
|
sudo sh -c "echo -e \"$default\" > \"$wpa_conf_path\"/\"$wpa_profile_name\""
|
|
|
|
sudo sh -c "wpa_passphrase \"$ssid\" \"$pass\" >> \"$wpa_conf_path\"/\"$wpa_profile_name\""
|
|
|
|
sudo sh -c "chmod 660 \"$wpa_conf_path\"/\"$wpa_profile_name\""
|
|
|
|
else # else unsecured network, connect with no password
|
|
|
|
sudo sh -c "cat > \"$wpa_conf_path\"/\"$wpa_profile_name\" << EOF
|
|
|
|
network={
|
|
|
|
ssid=\"$ssid\"
|
|
|
|
key_mgmt=NONE
|
|
|
|
}
|
|
|
|
EOF"
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Profile made. Would you like to connect to your new profile? [y/N]"
|
|
|
|
read connect_new
|
|
|
|
|
|
|
|
if [[ "$connect_new" != "" && $(printf "${connect_new:0:1}" | tr Y y) = "y" ]]
|
|
|
|
then
|
|
|
|
profile=$(printf "$wpa_profile_name" | sed 's/.conf//g') # get profile
|
|
|
|
connect_to_profile
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
connect_to_profile() {
|
|
|
|
sudo_validate
|
|
|
|
|
|
|
|
[ "$(pgrep wpa_supplicant)" != "" ] && sudo killall wpa_supplicant
|
|
|
|
sleep 1 # necessary for some reason, I forget why
|
|
|
|
|
|
|
|
#systemctl stop {wicd,netctl} && \
|
|
|
|
sudo wpa_supplicant -B -i "$wint" -c /etc/wpa_supplicant/"$profile".conf
|
|
|
|
# sudo dhcpcd "$wint" # needed if systemd dhcpcd service not used
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
profile=""
|
|
|
|
|
|
|
|
if [ $# -lt 1 ] ; then # if less than 1 argument (no args) provided...
|
|
|
|
print_usage ; exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
while [[ "$#" -gt 0 ]]; do
|
|
|
|
case $1 in
|
|
|
|
-d|--disconnect)
|
|
|
|
sudo_validate
|
|
|
|
sudo killall wpa_supplicant ; break ;;
|
|
|
|
-s|--scan)
|
|
|
|
sudo_validate
|
|
|
|
# Note: `iw dev $wint scan | less` gives more verbose info in output
|
|
|
|
sudo iw dev $wint scan | grep 'SSID: ' \
|
|
|
|
| sed -E 's/[[:space:]]+SSID: //g'
|
|
|
|
break ;;
|
|
|
|
-h|--help)
|
|
|
|
print_usage ; break ;;
|
|
|
|
-l|--list|--list-profiles)
|
|
|
|
printf "%s\n\n%s\n\n" \
|
|
|
|
"Current profiles to choose from:" \
|
|
|
|
"$profile_list " ; break ;;
|
|
|
|
-n|--new)
|
|
|
|
sudo_validate
|
|
|
|
make_new_wpa_profile # create a new wpa_supplicant profile
|
|
|
|
break ;;
|
|
|
|
-c|--connect)
|
|
|
|
profile="$2"
|
|
|
|
if [ "$profile" = "" ]; then
|
2021-07-03 01:39:37 +00:00
|
|
|
echo -ne "$(basename $0): -c, --connect takes an argument " \
|
|
|
|
"'profile_name'\n\n"
|
2021-04-15 22:45:15 +00:00
|
|
|
print_usage
|
|
|
|
else
|
|
|
|
sudo_validate
|
|
|
|
connect_to_profile # connect to existing profile
|
|
|
|
fi
|
|
|
|
shift
|
|
|
|
break ;;
|
|
|
|
*)
|
|
|
|
print_usage ; exit 1 ;;
|
|
|
|
esac
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
|