From 2f115da8416a1b057a2c182cce9b01d1276d36fb Mon Sep 17 00:00:00 2001 From: GADILA SHASHANK REDDY Date: Thu, 4 Jan 2018 20:04:34 +0530 Subject: [PATCH 1/9] Combined UI interface added for both scripts & minor bug fixes --- AutoVPN.sh | 190 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 190 insertions(+) create mode 100644 AutoVPN.sh diff --git a/AutoVPN.sh b/AutoVPN.sh new file mode 100644 index 0000000..769b671 --- /dev/null +++ b/AutoVPN.sh @@ -0,0 +1,190 @@ +#!/usr/bin/env bash + +#Author: Bakhtiyar Syed +#Institution: IIIT Hyderabad +#Date: 28 November 2016 + +#interact + +chmod +x $0 + +usr=$1; +passwd=$2; + +# Run in root if not already running in it +if [[ $(whoami) != "root" ]]; then + xhost +SI:localuser:root + sudo "$0" "$@" + xhost -SI:localuser:root + exit +fi + + +command -v zenity >/dev/null 2>&1 || { +if command -v apt-get 2&>1; then + apt-get update; apt-get install -y zenity; +elif command -v dnf 2&>1; then + dnf install -y zenity +fi +} + +# Ask user to connect/disconnect + +job=`zenity --entry --height=160 --width=400 --text="Enter to connect (C) or disconnect (D)" --title="Welcome to AutoVPN"` +# if connect + +if [ $job == "connect" -o $job == "c" -o $job == "C" ] +then + #[ -z $usr ] && read -p "Enter username: " -a usr + #[ -z $passwd ] && read -s -p "Enter password: " -a passwd + + + [ -z $usr ] && usr="$(zenity --entry --height=160 --width=400 --text="Enter your IIIT-H email ID" --title=Authentication)" + + # Escape dollars and remove new line characters in usr + + usr=$(echo "$usr"| sed 's/\$/\\\$/g') + usr="${usr//$'\\n'/}" + + if [ -z $usr ] + then + `zenity --error --text="Username cannot be blank/Cancelled"` + exit + fi + [ -z $passwd ] && passwd="$(zenity --password --height=160 --width=400 --text="Please enter your password" --title=Authentication)" + cd; + + # Escape dollars and remove new line characters in passwd + + passwd=$(echo "$passwd"| sed 's/\$/\\\$/g') + passwd="${passwd//$'\\n'/}" + + if [ -z $passwd ] + then + # echo Password cannot be blank! + `zenity --error --text="Password cannot be blank/Cancelled"` + exit + fi + command -v openvpn >/dev/null 2>&1 || { + if command -v apt-get 2&>1; then # Ubuntu based distros + apt-get update; apt-get install -y openvpn; + elif command -v dnf 2&>1; then # Fedora based distros + dnf install -y openvpn + fi +} +command -v expect >/dev/null 2>&1 || { +if command -v apt-get 2&>1; then # Ubuntu based distros + apt-get update; apt-get install -y expect; +elif command -v dnf 2&>1; then # Fedora based distros + dnf install -y expect +fi +} + +if grep -q "nameserver 10.4.20.204" "/etc/resolv.conf"; +then + sed -i '/nameserver 10.4.20.204/d' /etc/resolv.conf +fi +#apt-get update; + +cd /etc/openvpn; +#rm -rf * +if [ -e "ca.crt" ]; +then + rm -f ca.crt +fi +wget https://vpn.iiit.ac.in/ca.crt +if [ -e "all.iiit.ac.in.crt" ] || [ -e "all.iiit.ac.in.crt.*" ]; +then + rm -f all.iiit.ac.in.crt; + rm -f all.iiit.ac.in.crt.*; +fi +wget https://vpn.iiit.ac.in/all.iiit.ac.in.crt +if [ -e "all.iiit.ac.in.key" ]; +then + rm all.iiit.ac.in.key +fi + +curl -O --user "$usr":"$passwd" https://vpn.iiit.ac.in/secure/all.iiit.ac.in.key + +#fi + +chmod 600 all.iiit.ac.in.key; +if [ -e "linux_client.conf" ]; +then + rm -f linux_client.conf; +fi + +wget https://vpn.iiit.ac.in/linux_client.conf + +# Escape dollars in usr and passwd for expect's send +#usr=$(echo "$usr"| sed 's/\$/\\\$/g') +#usr="${usr//$'\\n'/}" +#passwd=$(echo "$passwd"| sed 's/\$/\\\$/g') +#passwd="${passwd//$'\\n'/}" + +#Remove newline chars +#dt=${dt//$'\n'/} +#dt=${dt//$'\n'/} + +#usr="${usr//$'\\n'/}" +#passwd="${passwd//$'\\n'/}" + +#passwd="$(echo "$passwd" | sed -e 's/\n//g')" + + + +#echo $passwd +expect <<- DONE + spawn openvpn --config linux_client.conf; + expect "Enter Auth Username:" { send "$usr\r" } + expect "Enter Auth Password:" { send "$passwd\r" } + expect "Initialization Sequence Completed" + interact; +DONE + +sleep 12; +if grep -q "nameserver 10.4.20.204" "/etc/resolv.conf"; +then + echo "Nameserver already set. No need for further setting up"; +else + sed -i '1i\'"nameserver 10.4.20.204" /etc/resolv.conf; +fi + +if ! ping -c1 moodle.iiit.ac.in &>/dev/null +then + zenity --error --height=100 --width=400 --title="An Error Occurred" --text="VPN failed to start. Contact the administrator or see troubleshooting on github.com/flyingcharge/AutoVPN" +else + zenity --info --title="SUCCESS" --text="VPN SUCCESSFULLY RUNNING!" +fi +sleep 3; +#num=`history | tail -n 2 | head -n 1 | tr -s ' ' | cut -d ' ' -f 2`; + +#history -c; + +#export HISTFILE=/dev/null +# if disconnect + +elif [ $job == "disconnect" -o $job == "d" -o $job == "D" ] +then + +# AUTHOR : MEGH PARIKH +# INSTITUTION : IIIT HYDERABAD +# DATE : 3 December 2017 (commit date on github) + + if [[ $(whoami) != "root" ]]; then + sudo "$0" "$@" + exit + fi + + pkill openvpn + sed -i '/nameserver 10.4.20.204/d' /etc/resolv.conf +# echo Disconnected successfully! + `zenity --info --text="DISCONNECTED SUCCESSFULLY!!" --title="SUCCESS!"` + + # for anything else + +else + + `zenity --width=200 --height=160 --error --text="Enter C/D only "` + +fi From a408d792ed18f2a5ae223bd29d990417b50e22cd Mon Sep 17 00:00:00 2001 From: GADILA SHASHANK REDDY Date: Thu, 4 Jan 2018 20:10:04 +0530 Subject: [PATCH 2/9] Rename autovpn.sh to autovpn.sh_original --- autovpn.sh => autovpn.sh_original | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename autovpn.sh => autovpn.sh_original (100%) diff --git a/autovpn.sh b/autovpn.sh_original similarity index 100% rename from autovpn.sh rename to autovpn.sh_original From a34266c280b73f65994d7b96dbf84f861f63ba38 Mon Sep 17 00:00:00 2001 From: GADILA SHASHANK REDDY Date: Thu, 4 Jan 2018 20:10:56 +0530 Subject: [PATCH 3/9] Rename AutoVPN.sh to autovpn.sh --- AutoVPN.sh => autovpn.sh | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename AutoVPN.sh => autovpn.sh (100%) diff --git a/AutoVPN.sh b/autovpn.sh similarity index 100% rename from AutoVPN.sh rename to autovpn.sh From 50e1d9750d44cb79f52399a7469d4f00b80d0e2e Mon Sep 17 00:00:00 2001 From: GADILA SHASHANK REDDY Date: Thu, 4 Jan 2018 20:17:12 +0530 Subject: [PATCH 4/9] Delete killvpn.sh --- killvpn.sh | 9 --------- 1 file changed, 9 deletions(-) delete mode 100755 killvpn.sh diff --git a/killvpn.sh b/killvpn.sh deleted file mode 100755 index fb402e4..0000000 --- a/killvpn.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/usr/bin/env bash - -if [[ $(whoami) != "root" ]]; then - sudo "$0" "$@" - exit -fi - -pkill openvpn -sed -i '/nameserver 10.4.20.204/d' /etc/resolv.conf From 6758eb4b0979dd17e166b602ef6a2de772cee7ed Mon Sep 17 00:00:00 2001 From: GADILA SHASHANK REDDY Date: Thu, 4 Jan 2018 20:17:57 +0530 Subject: [PATCH 5/9] Delete killvpn.sh --- killvpn.sh | 9 --------- 1 file changed, 9 deletions(-) delete mode 100755 killvpn.sh diff --git a/killvpn.sh b/killvpn.sh deleted file mode 100755 index fb402e4..0000000 --- a/killvpn.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/usr/bin/env bash - -if [[ $(whoami) != "root" ]]; then - sudo "$0" "$@" - exit -fi - -pkill openvpn -sed -i '/nameserver 10.4.20.204/d' /etc/resolv.conf From 3399208435a4de977be2930b0c8015b2e7ac872b Mon Sep 17 00:00:00 2001 From: GADILA SHASHANK REDDY Date: Thu, 4 Jan 2018 20:18:31 +0530 Subject: [PATCH 6/9] Delete autovpn.sh_original --- autovpn.sh_original | 142 -------------------------------------------- 1 file changed, 142 deletions(-) delete mode 100755 autovpn.sh_original diff --git a/autovpn.sh_original b/autovpn.sh_original deleted file mode 100755 index bdce2a4..0000000 --- a/autovpn.sh_original +++ /dev/null @@ -1,142 +0,0 @@ -#!/usr/bin/env bash - -#Author: Bakhtiyar Syed -#Institution: IIIT Hyderabad -#Date: 28 November 2016 - -#interact - -chmod +x $0 - -usr=$1; -passwd=$2; - -# Run in root if not already running in it -if [[ $(whoami) != "root" ]]; then - xhost +SI:localuser:root - sudo "$0" "$@" - xhost -SI:localuser:root - exit -fi - - -command -v zenity >/dev/null 2>&1 || { - if command -v apt-get 2&>1; then - apt-get update; apt-get install -y zenity; - elif command -v dnf 2&>1; then - dnf install -y zenity - fi -} - - - - -#[ -z $usr ] && read -p "Enter username: " -a usr -#[ -z $passwd ] && read -s -p "Enter password: " -a passwd - - -[ -z $usr ] && usr="$(zenity --entry --height=160 --width=400 --text="Enter your IIIT-H email ID" --title=Authentication)" -[ -z $passwd ] && passwd="$(zenity --password --height=160 --width=400 --text="Please enter your password" --title=Authentication)" -cd; - - -command -v openvpn >/dev/null 2>&1 || { - if command -v apt-get 2&>1; then # Ubuntu based distros - apt-get update; apt-get install -y openvpn; - elif command -v dnf 2&>1; then # Fedora based distros - dnf install -y openvpn - fi -} -command -v expect >/dev/null 2>&1 || { - if command -v apt-get 2&>1; then # Ubuntu based distros - apt-get update; apt-get install -y expect; - elif command -v dnf 2&>1; then # Fedora based distros - dnf install -y expect - fi -} - -if grep -q "nameserver 10.4.20.204" "/etc/resolv.conf"; -then -sed -i '/nameserver 10.4.20.204/d' /etc/resolv.conf -fi -#apt-get update; - - - -cd /etc/openvpn; -#rm -rf * -if [ -e "ca.crt" ]; -then - rm -f ca.crt -fi -wget https://vpn.iiit.ac.in/ca.crt -if [ -e "all.iiit.ac.in.crt" ] || [ -e "all.iiit.ac.in.crt.*" ]; -then - rm -f all.iiit.ac.in.crt; - rm -f all.iiit.ac.in.crt.*; -fi -wget https://vpn.iiit.ac.in/all.iiit.ac.in.crt -if [ -e "all.iiit.ac.in.key" ]; -then - rm all.iiit.ac.in.key -fi - -curl -O --user "$usr":"$passwd" https://vpn.iiit.ac.in/secure/all.iiit.ac.in.key - -#fi - -chmod 600 all.iiit.ac.in.key; -if [ -e "linux_client.conf" ]; -then - rm -f linux_client.conf; -fi - -wget https://vpn.iiit.ac.in/linux_client.conf - -# Escape dollars in usr and passwd for expect's send -usr=$(echo "$usr"| sed 's/\$/\\\$/g') -passwd=$(echo "$passwd"| sed 's/\$/\\\$/g') - -#Remove newline chars -#dt=${dt//$'\n'/} -#dt=${dt//$'\n'/} -usr="${usr//$'\\n'/}" -passwd="${passwd//$'\\n'/}" -#passwd="$(echo "$passwd" | sed -e 's/\n//g')" - - - -#echo $passwd -expect <<- DONE - - spawn openvpn --config linux_client.conf; - - expect "Enter Auth Username:" { send "$usr\r" } - - expect "Enter Auth Password:" { send "$passwd\r" } - - expect "Initialization Sequence Completed" - - interact; -DONE - -sleep 12; -if grep -q "nameserver 10.4.20.204" "/etc/resolv.conf"; -then -echo "Nameserver already set. No need for further setting up"; -else -sed -i '1i\'"nameserver 10.4.20.204" /etc/resolv.conf; -fi - -if ! ping -c1 moodle.iiit.ac.in &>/dev/null -then -zenity --error --height=100 --width=400 --title="An Error Occurred" --text="VPN failed to start. Contact the administrator or see troubleshooting on github.com/flyingcharge/AutoVPN" -else -zenity --info --title="SUCCESS" --text="VPN SUCCESSFULLY RUNNING!" -fi -sleep 3; -#num=`history | tail -n 2 | head -n 1 | tr -s ' ' | cut -d ' ' -f 2`; - -#history -c; - -#export HISTFILE=/dev/null From 35cf3c3355559788d2f0189f2aa0e3d898d51a01 Mon Sep 17 00:00:00 2001 From: GADILA SHASHANK REDDY Date: Thu, 4 Jan 2018 20:26:41 +0530 Subject: [PATCH 7/9] Create CHANGELOG --- CHANGELOG | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 CHANGELOG diff --git a/CHANGELOG b/CHANGELOG new file mode 100644 index 0000000..3e679fc --- /dev/null +++ b/CHANGELOG @@ -0,0 +1,3 @@ +Combined autovpn.sh and killvpn.sh into a single script with appropriate GUI prompts. +Fixed a bug where script attempts to connect to VPN service with blank username and/or password +Added appropriate author credits in the script(since the script was combined) From b10b47f3666ac8774928321a3d8746644ddeef0d Mon Sep 17 00:00:00 2001 From: GADILA SHASHANK REDDY Date: Sat, 6 Jan 2018 17:02:26 +0530 Subject: [PATCH 8/9] Update autovpn.sh --- autovpn.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autovpn.sh b/autovpn.sh index 769b671..3d9c889 100644 --- a/autovpn.sh +++ b/autovpn.sh @@ -185,6 +185,6 @@ then else - `zenity --width=200 --height=160 --error --text="Enter C/D only "` + `zenity --width=200 --height=160 --error --text="Enter C/D only or Operation Cancelled"` fi From 38ecbdf2e6ba36900233f2f18f2f1ab2c9ecc31d Mon Sep 17 00:00:00 2001 From: GADILA SHASHANK REDDY Date: Sun, 14 Jan 2018 17:41:13 +0530 Subject: [PATCH 9/9] Update autovpn.sh Added the word bash to run the script as root if user is not root. --- autovpn.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/autovpn.sh b/autovpn.sh index 3d9c889..4c4607b 100644 --- a/autovpn.sh +++ b/autovpn.sh @@ -14,7 +14,8 @@ passwd=$2; # Run in root if not already running in it if [[ $(whoami) != "root" ]]; then xhost +SI:localuser:root - sudo "$0" "$@" + #sudo "$0" "$@" + sudo bash "$0" "$@" xhost -SI:localuser:root exit fi