LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Bash script for VPN login (http://www.linuxquestions.org/questions/programming-9/bash-script-for-vpn-login-607208/)

oopicwow 12-16-2007 09: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.

Thanks!

chrism01 12-17-2007 01:16 AM

Try the 'expect' utility; it's designed to do that sort of thing. eg http://www.ibm.com/developerworks/linux/library/l-sc1/

geoff_f 12-17-2007 01: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:
Quote:

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

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

# echo "Enter IPSec gateway address:"
# read IPSec_gateway_address
IPSec_gateway_address="192.168.1.15"
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:
Quote:

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 http://www.tldp.org/LDP/abs/html/index.html.

BlisteringSh33p 12-17-2007 03: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 (http://www.unix-ag.uni-kl.de/~massar/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 ${vpn.example.com}
IPSec ID ${IPSecID}
IPSec obfuscated secret ${ReallyLongHexCode}
Xauth username ${MyName}
Xauth password ${MyPlaintextPassword}

oopicwow 12-17-2007 06: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 02:32 PM

What did you do?

Maybe help others? -- Post your solution.

oopicwow 01-21-2008 09: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 08:54 PM.