connect (1603B)
1 #!/usr/bin/env zsh 2 3 # vars 4 WIFI_DEV=$(iw dev | awk '$1=="Interface"{print $2}') 5 CONNECTION_FILE_PATH="$HOME/lib/documents/wifi/" 6 CONNECTION_FILE="${CONNECTION_FILE_PATH}${1}" 7 CONNECTION=$(echo ${CONNECTION_FILE} | awk -F\/ '{print $NF}') 8 VPN_KEY_PATH="$HOME/lib/key" 9 VPN_KEY="${VPN_KEY_PATH}/$(hostname -s).ovpn" 10 11 if [ ! $1 ] ; then 12 echo "specify a connection file" 13 exit 1 14 elif [ ! -f ${CONNECTION_FILE} ] ; then 15 echo "${CONNECTION_FILE} does not exist" 16 exit 1 17 fi 18 19 # disconnect 20 echo "disconnecting any running connection" 21 $HOME/bin/disconnect 22 23 # bring dev up 24 if $(ip link show up | grep ${WIFI_DEV} >/dev/null) ; then 25 echo "${WIFI_DEV} is already up" 26 else 27 echo "bringing ${WIFI_DEV} up" 28 sudo ip link set ${WIFI_DEV} up >/dev/null 29 fi 30 31 # connect using specified config 32 if [ -f ${CONNECTION_FILE} ] ; then 33 echo "connecting to ${CONNECTION}" 34 sudo wpa_supplicant -B -q -i ${WIFI_DEV} -c ${CONNECTION_FILE} >/dev/null 2>&1 35 fi 36 37 # get an ip 38 echo "starting dhcpcd" 39 sudo dhcpcd -b -4 ${WIFI_DEV} >/dev/null 2>&1 40 41 function use_vpn() { 42 echo "not at home, starting vpn" 43 sleep 20 44 sudo openvpn --config ${VPN_KEY} --daemon >/dev/null 2>&1 45 sudo systemd-tty-ask-password-agent 46 echo "waiting for vpn..." 47 sleep 60 48 echo "nameserver 208.67.222.222 49 nameserver 208.67.220.220" | sudo resolvconf -a ${WIFI_DEV}.inet 50 sudo ip route add 192.168.0.0/24 via 10.8.0.13 dev tun0 51 while : 52 ping -q -c1 fortkickass >/dev/null 53 if [ $? -eq 0 ] ; then 54 echo "vpn is up" 55 break 56 else 57 echo "still waiting..." 58 sleep 2 59 fi 60 } 61 # when remote bring ip vpn 62 case "${CONNECTION}" in 63 home) ;; |work) ;; 64 *) use_vpn ;; 65 esac 66