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:
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
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?