0

I am trying to write a script that logs me in to OpenVPN configs automatically. I have succeeded in automating a single login, like this:

#!/bin/bash
cd filepath/
username= "username"
password= "password"
read -sp "Enter Sudo Password: " sudopassword

/usr/bin/expect << EOF

spawn sudo openvpn config-file.ovpn
expect "password for $USER: "
send "$sudopassword\r"
expect "Enter Auth Username: "
send "$username\r"
expect "Enter Auth Password: "
send "$password\r"
expect "$ "

EOF

I have this saved as a single shell script. Then I am simply running:

./killvpn

From a seperate .sh file to ensure that the previous VPN connection is terminated:

#!/bin/bash
sudo pkill vpn

My script so far starts in a seperate directory, and calls these scripts in to action like this:

## Project Folder is called: VPN ##
## Filepath: VPN/                ##

#!/bin/bash
./login1.sh
sleep 30m
./killvpn
echo " "
echo "Logging in to VPN2"
echo " "
./login2.sh
sleep 30m           

And so forth. My ./login2.sh file is identical to ./login1.sh , However I have removed the following sections:

read -sp "Enter Sudo Password: " sudopassword

and:

expect "password for $USER: "
send "$sudopassword\r"

However, ./login2.sh is still prompting me for a sudo password when it is launched, despite the main script all ready having been given root privileges. This makes the script unuseable as my VPN connection would just turn off if I wasn't monitoring it in an open terminal.

Could someone be kind enough to review this code, and tell me how to run ./login2.sh without a sudo prompt, and help me identify where I went wrong please?

0

The easiest way to remove the password requirement when using sudo (without changing your script) is to define your user privileges with "nopasswd" in /etc/sudoers :

myuser    ALL=(ALL:ALL) NOPASSWD:ALL

This would allow user "myuser" to run any command as root without prompting for password.

(Remember to use the 'visudo' command to change the sudoers file, and not edit it directly).

| improve this answer | |
  • I think that this layout will work, however I am concerned about the security implications. Although this script contains a username and password for the .ovpn file, for its final implementation I will encrypt the bash script and keep the unencrypted scripts backed up on a USB so its isolated from the machine. I would not be happy having create a new user to use my script or with turning off my sudopassword. – user295924 Jun 18 '18 at 3:00
0

Would it be easier to just run sudo ./yourScript.sh and not keep prompting for sudo passwords?

| improve this answer | |
  • I tried this without the sudopassword variable, however I could not get the script to work with those sections removed. For some reason it would no longer input the username and password supplied in the login script, and the result was twice as bad. I can not find any other way round the problem. – user295924 Jun 18 '18 at 2:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy