Script to connect to VPN

Hi,

I’m trying to write a script that will connect my VPN without me having to manually enter all the information. Normally I cd into the appropriate directory and run sudo openvpn and then the UDP I’d like to connect to. It asks me for my sudo password, then my Auth username, then my Auth password. I want to write a script that I run and it will handle all of this automatically.

However, I am getting error command not found “sudo”.

If I remove sudo from the script and run the command as sudo, I get error command not found “openvpn”. I am in the correct directory.

Also, I know that storing passwords in such a way is highly discouraged. This is mostly a learning process for me.

Thank you.

“Command not found” typically means one of:

  1. the program is not installed
  2. your path isn’t setup correctly (in that terminal)

Since you can’t also find sudo and it is so common to have it installed, I would guess it is (2).

Try adding the full path of the openvpn binary.

I use a primitive little script. OpenVPN requires sudo, so I’ve added my user to allow them to run openvpn without the password, something like ALL = NOPASS:/usr/sbin/openvpn. Then in the directory that holds my ovpn file I have a file creds

user
mypassword```

Then in my ovpn file I have the line

auth-user-pass creds

(The auth-user-pass line is already there.)

Then my little script uses tmux to connect so that once connected I can detach it.

#!/bin/bash
cd ~/vpndirectory
tmux new-session -s myvpn ‘sudo openvpn hydra.ovpn’


All this, of course, depends upon how secure the machine is. I wouldn't put this on a laptop that could be stolen but for my home workstation, if someone steals it I've got bigger problems.

With sudo when entering a specific command like that it is necessary that the command be invoked exactly as shown in the sudoers file.

Maybe try the command as
tmux new-session -s myvpn ‘sudo /usr/sbin/openvpn hydra.ovpn’
so it is found by sudo.
It may be necessary to use the full path for the ovpn file as well.

@fatka sudo is installed (1) and I can successfully run it in that terminal otherwise (2).

@tqcharm I’ve tried adding the full path but I still get the error which is command not found. If the line says “sudo openvpn” the command not found is sudo. If the line just says “openvpn” then the command not found is openvpn.

You could troubleshoot your issue:

  • either by creating a simple script and running it, first without sudo (e.g. cat /etc/fstab), then by using sudo (e.g. sudo -S cat /etc/gshadow).
  • and/or by creating a new wheel user, and running your script (so another bash profile).

You could also share your script here, redacting sensitive information.

This was going to be my next step. But I’m (kind of?) happy to share that I have (kind of?) found a solution. I was using Microsoft Copilot to help me with the script. I spent hours editing the script only to never find a successful solution. My solution was found by asking ChatGPT.

Using ChatGPT, it didn’t take me more than 20 minutes to get it to run successfully. My lesson learned is that ChatGPT is a superior product compared to Copilot.

The reason I am “kind of” happy about this is that I didn’t fully figure it out myself. I guess we can mark this as closed now (if that’s something we do here? I’m new…).

This is obviously a different discussion, but while we’re at it, and for the sake of conversation, what is everyone’s opinion and/or the consensus here on ChatGPT vs. Copilot?

Thank you everyone for your help.

1 Like

I’d have it as a systemd service.


NetworkManager can also do it:

nm-connection-editor
  • OpenVPN interface → VPN → Password → (icon) → Store the password for all users
  • Main interface → General → Automatically connect to VPN
1 Like

A good practice is for the OP to mark the most relevant post to the question as a solution. In case the solution didn’t come from someone else, but rather through OP’s findings not related to the answers in the thread (as it is in this case), then the OP can mark it’s own reply as solution.

That is an invalid analysis.
Using sudo, when the following command is not valid the error is simply passed back. This means that both circumstances you show are probably based on not being able to access the openvpn command.

In the script it also may be necessary (and is always good practice) to use full paths to the various commands since the script does not necessarily use your users environment and does not always know the paths the user has configured (including the path to the sudo command)

1 Like