Last active
August 21, 2018 08:47
-
-
Save cyphunk/35b8c25eab13b4e2a9c6db5948b98691 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# wifi: Linux absolutely sucks at network management. Want two devices up, | |
# forget it. Would use networkmanager from cli but gnome sucks so | |
# hard at integrating. | |
# We'd use wicd, which is much more stable, but it cannot support | |
# multiple interfaces at once. | |
# | |
# it is 2115 and linux still can't dynamicly select with multi interfaces. | |
# | |
# wifi list ssid's | |
# wifi auto connect to known networks | |
# wifi <ssid> connect to SSID. Use passwd in db else request passwd | |
# wifi <ssid> <passwd> | |
# | |
# Note: passwords are stored in a plaintext file | |
# sudo call integrity check: only root should be able to change script | |
l=($(ls -l `readlink -f $0`)) | |
[ ${l[0]:2:1} != "-" ] && [ "${l[2]}" != "root" ] || | |
[ ${l[0]:5:1} != "-" ] && [ "${l[3]}" != "root" ] || | |
[ ${l[0]:8:1} != "-" ] && { echo -e "only root should be able to modify\n${l[@]}"; exit 1;} | |
#ETH="wlp3s0" #AKA Love SystemD or die! | |
#ETH="wlan0" | |
ETH=`ls /sys/class/net/|grep wl|head -1` | |
# Wifi password db | |
WIFIPASSWDFILE="$HOME/.wifidb" | |
if [ -f "$WIFIPASSWDFILE" ]; then | |
source "$WIFIPASSWDFILE" | |
else | |
declare -A WIFIPASSWD | |
fi | |
if [ $# -eq 0 ] || [ "$1" == "list" ]; then | |
sudo ifconfig $ETH up | |
scan=$(sudo iwlist $ETH scanning) | |
while [ $? -ne 0 ]; do | |
sleep 1 | |
scan=$(sudo iwlist $ETH scanning) | |
done | |
echo "$scan" | egrep 'ESSID|Quality|Encryption key:' | xargs | sed 's/Quality=/\n/g' | sed 's/ESSID:\| *Signal level\|Encryption key://g' | sed 's/ on / on /g' | sort --field-separator="=" -k 1 | |
else | |
if [ "$1" = "auto" ]; then | |
echo "# Attempting to connect to known networks" | |
sudo ifconfig $ETH up || exit | |
oIFS=$IFS | |
IFS=$'\n' | |
scan=$(sudo iwlist $ETH scanning) | |
ssids=$(echo "$scan" | egrep 'ESSID' | sed 's/ *ESSID:"\|"//g') | |
list=$(echo "$scan" | egrep 'ESSID|Quality|Encryption key:' | xargs | sed 's/Quality=/\n/g' | sed 's/ESSID:\| *Signal level\|Encryption key://g' | sed 's/ on / on /g' | sort --field-separator="=" -k 1) | |
# in casese where there are 2+ known networks connect to most recent: | |
#for ssid in $ssids; do | |
# if [ "${WIFIPASSWD[$ssid]+ISSET}" == "ISSET" ]; then | |
# echo "Will connect to $ssid" | |
# set -- $ssid | |
# break | |
# fi | |
#done | |
# sadly this order by top entry doesnt work. Reading in hash reorders list | |
for knownssid in "${!WIFIPASSWD[@]}"; do | |
if [ "$(echo "$ssids" | grep "^$knownssid$")x" != "x" ]; then | |
echo "# Will connect to $knownssid" | |
set -- $knownssid | |
break | |
fi | |
done | |
IFS=$oIFS | |
if [ "$1" == "auto" ]; then | |
echo "# no known ssid found" | |
echo "$list" | |
exit | |
fi | |
elif [ "$1" = "status" ]; then | |
iwconfig $ETH | |
exit | |
fi | |
echo "# Connecting to SSID \"$1\"" | |
sudo killall wpa_supplicant | |
/bin/ps -C dhclient -f h | grep $ETH | awk '{print $2}' | xargs sudo kill 2>/dev/null | |
if [ $# -eq 2 ]; then | |
echo "# Setting password" | |
PASSWORD="$2" | |
elif [ "${WIFIPASSWD[$1]+ISSET}" == "ISSET" ]; then | |
echo "# Have password" | |
PASSWORD="${WIFIPASSWD["$1"]}" | |
else | |
read -s -p "Password for $1 (or none): " PASSWORD | |
fi | |
WIFIPASSWD["$1"]="$PASSWORD" | |
tmp=$(declare -p WIFIPASSWD | sed -e 's/\[/\n\[/g' ) | |
echo "$tmp" > "${WIFIPASSWDFILE}" | |
sudo chmod 770 "$WIFIPASSWDFILE" | |
if [ "$PASSWORD" == "" ]; then | |
echo -e "\n# Connecting without password" | |
sudo iwconfig $ETH essid "$1" key open || exit | |
if [ $? -ne 0 ]; then | |
echo "!! Error: couldn't connect" | |
exit | |
fi | |
else | |
echo "# Connecting with password " | |
tmpfile=$(mktemp) | |
#echo "# iwconfig with key" | |
#sudo nmcli dev wifi connect "$1" password "$PASSWORD" | |
#sudo iwconfig wlp3s0 essid "$1" key s:$PASSWORD | |
#sudo iwconfig wlp3s0 essid "$1" key "s:$PASSWORD" \ | |
wpa_passphrase "$1" "$PASSWORD" > $tmpfile || exit | |
wpafile=/tmp/wpa_supplicant.${ETH}.log | |
rm $wpafile 2>/dev/null | |
sudo wpa_supplicant -B -i $ETH -c $tmpfile -d -f $wpafile | |
# tail -f /tmp/wpa_supplicant.wlp3s0.log | egrep 'reason|SUCCESS|result' | |
while [ 1 ]; do | |
grep -q 'reason=WRONG_KEY' $wpafile && echo -e "\n# Bad password?" && exit | |
grep -q 'INTERFACE_DISABLED' $wpafile && echo -e "# Interface disabled" && exit | |
grep -q 'result=SUCCESS' $wpafile && echo -e "\n# Good password" && break | |
sleep 0.5 | |
echo -n "." | |
done | |
if [ $? -ne 0 ]; then | |
echo "!! Error: couldn't connect with password" | |
exit | |
fi | |
fi | |
sudo rm /tmp/dhclient.${ETH}.leases 2>/dev/null | |
# clear default gw first | |
# ip route del 0/0 | |
sudo route del default 2>/dev/null # error on no default exist | |
echo "initial-interval 1;" > /tmp/dhclient.conf | |
echo "backoff-cutoff 1;" >> /tmp/dhclient.conf | |
sudo dhclient -1 -v -cf /tmp/dhclient.conf -pf /run/dhclient.${ETH}.pid -lf /tmp/dhclient.${ETH}.leases $ETH 2>&1 | egrep 'DHCP|bound' | |
# all failed: | |
#echo "" > /tmp/dhclient.conf | |
##echo "send dhcp-requested-address 192.168.100.111, ;" >> /tmp/dhclient.conf | |
#echo "request;" >> /tmp/dhclient.conf | |
#echo "send option dhcp-client-identifier \"foo\";" >> /tmp/dhclient.conf | |
#echo "send option host-name \"foo\";" >> /tmp/dhclient.conf | |
#sudo dhclient -v -cf /tmp/dhclient.conf -pf /run/dhclient.${ETH}.pid -lf /tmp/dhclient.${ETH}.leases $ETH | |
fi | |
#elif [ "${!WIFIPASSWD[@]+$1}" ]; then | |
#elif echo "${!WIFIPASSWD[@]}" | grep -q "$1"; then | |
# Non NM but with root for bash -c | |
#sudo iwconfig wlp3s0 essid "$1" key "s:$PASSWORD" \ | |
# || bash -c "wpa_supplicant -B -i wlp3s0 -c <(wpa_passphrase \"$1\" \"$PASSWORD\")" | |
# Without bash -c | |
# if key is hex remove "s:" prefix: | |
# iwconfig wlp3s0 essid key <wireless key> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment