bin/wpa-connect
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
#!/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
# TODO: auto-read or allow changes
wint="wlp3s0"
# 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
# strips the '.conf'
# shellcheck disable=SC2012
profile_list="$(ls /etc/wpa_supplicant | sed 's/.conf//g' | sed 's/^/ /g')"
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%s\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
echo -ne "$(basename $0): -c, --connect takes an argument " \
"'profile_name'\n\n"
print_usage
else
sudo_validate
connect_to_profile # connect to existing profile
fi
shift
break ;;
*)
print_usage ; exit 1 ;;
esac
shift
done
|