-   Programming (
-   -   Bash script for VPN login (

oopicwow 12-16-2007 08:09 PM

Bash script for VPN login
Hello, I am trying to write a bash script that will log me into my VPN automatically without me having to enter the same data over when I want to log in.

I found this script but does not seem to do exactly what I need.

sudo vpnc-connect
read -t 1 incoming

I use the "vpnc-connect" but it runs that till it is done then will execute the rest. I was looking for a command like waitfor or something that will look for the follow text and then send the right data based on the incoming text. The questions look like the following:

Enter IPSec gateway address:
Enter IPSec ID for :
Enter IPSec secret for @:
Enter username for :
Enter password for @:

There is probably a program that does this, but I am new to linux and would like to learn how to do this. I have searched the web but unable to find exactly what I need.


chrism01 12-17-2007 12:16 AM

Try the 'expect' utility; it's designed to do that sort of thing. eg

geoff_f 12-17-2007 12:35 AM

It's likely that vpnc-connect is a bash script that has been designed to gather the required connection information for the vpn client. The best way to achieve what you want is to modify the script to enter the information directly, instead of asking the questions about the connection data. To do this, use a text editor to view the script, and look for the areas where the questions are asked. (You will most likely need to open the text editor as root, as this file is probably a root-owned file.) They should look typically like:

echo "Enter IPSec gateway address:"
You should see a read command following that which puts the answer into a variable. What you need to do is to allocate the answer directly to the variable. For example, if the statements concerning the first question are:

echo "Enter IPSec gateway address:"
read IPSec_gateway_address
then comment those lines out and add the direct variable assignment in the following line, like this:

# echo "Enter IPSec gateway address:"
# read IPSec_gateway_address
The example uses a dummy IP Address and takes a guess at what the variable may be called; naturally, use whatever address is applicable to your system, and whatever variable name your vpnc-connect script uses. Continue on to complete the remainder of the questions, then you should be OK to use the script without user input. I would make sure the script is given strict permissions once the connection information is embedded in it. I would suggest:

chmod 700 /usr/sbin/vpnc-connect
(assuming it is located in /usr/sbin on your system).

An excellent source of information on bash scripts, to help you with understanding how vpnc-connect works is

BlisteringSh33p 12-17-2007 02:35 AM

I know this doesn't directly address your question, but it may solve what you're trying to do.

Do you log in to different VPNs? I only have to use one VPN, and connect with vpnc ( I created /etc/vpnc/default.conf with all of the values stored so I never have to interact with it at all.

Contents of /etc/vpnc/default.conf (replace the values in ${}):

IPSec gateway ${}
IPSec obfuscated secret ${ReallyLongHexCode}
Xauth username ${MyName}
Xauth password ${MyPlaintextPassword}

oopicwow 12-17-2007 05:38 PM

Thanks for the feedback. I will go though your responses. If I can do it in a conf file that will also work. I will only be connecting to one VPN.

Thanks again.

archtoad6 01-15-2008 01:32 PM

What did you do?

Maybe help others? -- Post your solution.

oopicwow 01-21-2008 08:02 PM

I did exactly what BlisteringSh33p said to do.

"I created /etc/vpnc/default.conf with all of the values stored so I never have to interact with it at all."

It did what I needed to do. I just added the vpnc-connect to a script I use to connect to the office so that it is all done in one script. When I close the ssh connection, it will automatically disconnect from the VPN connection.

sudo vpnc-connect
echo Connecting to work
ssh [work server]
sudo vpnc-disconnect

I found out I really didn't need to do anything fancy.

Thanks again.

All times are GMT -5. The time now is 02:50 AM.