Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 12-16-2007, 08:09 PM   #1
LQ Newbie
Registered: Dec 2007
Posts: 3

Rep: Reputation: 0
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.

Old 12-17-2007, 12:16 AM   #2
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,406

Rep: Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396
Try the 'expect' utility; it's designed to do that sort of thing. eg
Old 12-17-2007, 12:35 AM   #3
Registered: May 2003
Location: Canberra, Australia
Distribution: openSUSE 11.3
Posts: 445

Rep: Reputation: 31
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
Old 12-17-2007, 02:35 AM   #4
LQ Newbie
Registered: Jun 2006
Location: Virginia
Distribution: Gentoo, Slackware, RHAS, MontaVista, Fedora, Ubuntu
Posts: 4

Rep: Reputation: 0
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}
Old 12-17-2007, 05:38 PM   #5
LQ Newbie
Registered: Dec 2007
Posts: 3

Original Poster
Rep: Reputation: 0
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.
Old 01-15-2008, 01:32 PM   #6
Senior Member
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
What did you do?

Maybe help others? -- Post your solution.

Last edited by archtoad6; 01-22-2008 at 09:31 AM. Reason: missing word :( :tisk:
Old 01-21-2008, 08:02 PM   #7
LQ Newbie
Registered: Dec 2007
Posts: 3

Original Poster
Rep: Reputation: 0
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.


expect, vpn, vpnc

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Run Bash Script @ login XaViaR Linux - General 5 01-26-2006 10:05 PM
bash login script bunnyknight13 Linux - Newbie 4 09-10-2004 08:02 PM
bash login script seanatis Programming 6 09-06-2004 08:00 AM
Run script at login instead of bash uzi4u Linux - General 2 04-28-2004 02:31 PM
Trying to execute bash script upon login?? mr-d-2003 Linux - Newbie 4 10-30-2003 03:15 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:08 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration