Skip to content

Instantly share code, notes, and snippets.

@lexszero
Created April 9, 2015 23:49
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 lexszero/13030a81315290d5b6e4 to your computer and use it in GitHub Desktop.
Save lexszero/13030a81315290d5b6e4 to your computer and use it in GitHub Desktop.
Easily connect to WiFi network with just wpa_supplicant and Gentoo.
#!/bin/bash
iface='wlan0'
conf='/etc/wpa_supplicant/wpa_supplicant.conf'
fail() {
echo "Fuck you! $@"
exit 1
}
declare -a a
declare -A keytype
while read s; do
eval "$s"
[ -n "$ESSID" ] && {
keytype[$ESSID]="$key" # TODO: key type - wep/wpa/wpa2/wtfelse
comment="$qual, key: $key"
grep "^\s*ssid=\"$ESSID\"" "$conf" 2>&1 >/dev/null && comment="$comment *"
a[${#a[@]}]="$ESSID"
a[${#a[@]}]="$comment"
unset ESSID
}
done < <( iwlist $iface scan | sed -rn 's#^[^#]*(ESSID|key):"?([^"]*)"?.*$#\1="\2"#p; s#^\s*Quality=(\S*).*#qual=\1#p' )
exec 3>&1
ESSID=$(dialog --menu "Choose network" 20 80 15 "${a[@]}" 2>&1 1>&3) || fail
[ -n "$ESSID" ] || fail
if [[ "${keytype[$ESSID]}" == "on" ]]; then
passphrase=$(dialog --passwordbox "Enter password for $ESSID" 20 80 2>&1 1>&3)
res=$(wpa_passphrase "$ESSID" "$passphrase") || fail "$res"
else
res="
network={
ssid=\"$ESSID\"
key_mgmt=NONE
}
"
fi
echo "$res"
echo "$res" >> "$conf"
killall -HUP wpa_supplicant || /etc/init.d/net.$iface restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment