Solved

Bash/VPN expert needed

Posted on 2008-10-08
16
873 Views
Last Modified: 2013-12-26
Mac OS X 10.5.5
I used Network in System Preferences to configure a VPN connection.
After which it displays in the menu on the top of my screen with my clock.
I click on it and then click 'Connect XXXConnection'
Then, because the connection doesn't add where to go to the routing table, I need to run the following script:

ifconfig ppp0 | grep inet  | awk '{printf("route add 10.0.0.0/24 -netmask 255.0.0.0 %s", $2);}' | sh

I kind of understand that...

My question is:  Is there a way to connect to my VPN using a script that I can add to the script I use to add the VPN IP to the routing table?  That way, I will only need to run the script.  It will also save me a click (actually 2 clicks).  Plus, the added benefit, I'd learn something.

I'm not looking to run the existing connection 'XXXConnection' from the command-line.  Although, if that is one solution, I'd like to know that too.  But what I'm looking for is a way to create a VPN connection using a script.

Did I just repeat myself?  Sorry.  Looking for help.  Also, not looking for third-party tools.  Just what is native to OS X.

Thanks.
0
Comment
Question by:famoso
  • 10
  • 6
16 Comments
 
LVL 2

Expert Comment

by:dscl
ID: 22674996
Check out this AppleScript I attached.  Bring it into Script Editor change the connection name, compile, and save.

You can then run that from the scripts menu or save as an application and launch it however you like.
vpn-connect.txt
0
 

Author Comment

by:famoso
ID: 22675067
I appreciate your help.
Please explain a bit.  I know less of AppleScript than I do Shell.  I'm a C# developer.
Where is the Script Editor?
Do I have to change the extension of vpn-connect from .txt to something else?
After compiling, where does one usually place the compiled file, or is it placed somewhere?  Where?
Is this a full program?
If I save it as an application, does it get an .app extension?
AppleScript looks so much like English that it doesn't look like it's supposed to work.
Is, 'tell current location of network preferences' actual syntax?
If my connectionName is 'XXXConnectionName'
Would I then do:

-- Declare your connection name here
set connectionName to "XXXConnectionName"

tell application "System Events"
      tell current location of network preferences
            set VPNservice to service connectionName
            if exists VPNservice then connect VPNservice
            do shell script "ifconfig ppp0 | grep inet  | awk '{printf(\"route add 10.0.0.0/24 -netmask 255.0.0.0 %s\", $2);}' | sh"
      end tell
end tell
0
 
LVL 2

Expert Comment

by:dscl
ID: 22675146
Okay lets run down the answers to some questions here for you :D

Inside the Applications folder open the AppleScript folder.  Inside there run the AppleScript Utility.  Now check the boxes for "Show Scripts men in menu bar" and "Show Computer Scripts".   Now you will notice a new menu in the top menu bar (near where you would connect to VPN.

Next launch the Script Editor application that is in the formerly mentioned AppleScript folder.

Now copy&paste the contents of the text file I had attached into the script editor.  

Now change the connectionName variable to equal your connection name.  To determine what this is go to the Network preference pane in System Preferences.  You want to use the name as it appears in the table view to the left in the preference pane.

Now save the script as a Script.  This will create a .scpt file and will force it to compile.  If you do a save as a application it will create a .app.  If you want to run this as an application or from the dock save it as an application.  You can also run it as a script from the script menu in the menu bar at the top of the finder.

If you want to run it from the scripts menu save it to the following path  /Library/Scripts

And that my friend is all you do.  AppleScript is fairly english-like and once you get used to it you'ld be amazed at what it can do.

Hope that helps!

 
0
 

Author Comment

by:famoso
ID: 22675296
That's pretty cool.  It runs the VPN Connection but not the script.  I usually run my script as sudo.
Any suggestions on how to run it as sudo or su?

-- Declare your connection name here
set connectionName to "INOC"

tell application "System Events"
      tell current location of network preferences
            set VPNservice to service connectionName
            if exists VPNservice then connect VPNservice
            do shell script "ifconfig ppp0 | grep inet  | awk '{printf(\"route add 10.0.0.0/24 -netmask 255.0.0.0 %s\", $2);}' | sh"
      end tell
end tell
0
 

Author Comment

by:famoso
ID: 22675302
It's also brings down my Internet access while the VPN is running.  It doesn't usually do that when I connect 'manually' and run the script 'manually'.  Could that be because of the script not executing?
0
 
LVL 2

Expert Comment

by:dscl
ID: 22675334
First try changing the script to look like this

tell application "System Events"
      tell current location of network preferences
            set VPNservice to service connectionName
            if exists VPNservice then connect VPNservice
            do shell script "ifconfig ppp0 | grep inet  | awk '{printf(\"route add 10.0.0.0/24 -netmask 255.0.0.0 %s\", $2);}' | sh" user name "me" password "mypassword" with administrator privileges
      end tell
end tell

Obviously change the username and password to be appropriate for your environment.  IF that doesn't work there is another way to do this that more simulates what you have been doing, but this should work.
0
 

Author Comment

by:famoso
ID: 22675364
Not yet - AppleScript Error:  System Events got an error: A privilege violation occurred.
0
 
LVL 2

Expert Comment

by:dscl
ID: 22675376
Interesting, pull the do shell out of the system events tell like so&


tell application "System Events"
      tell current location of network preferences
            set VPNservice to service connectionName
            if exists VPNservice then connect VPNservice
      end tell
end tell
do shell script "ifconfig ppp0 | grep inet  | awk '{printf(\"route add 10.0.0.0/24 -netmask 255.0.0.0 %s\", $2);}' | sh" user name "me" password "mypassword" with administrator privileges

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:famoso
ID: 22675390
So close (i think)  I got past the authentication with this but I still can't connect to the server I usually do:

-- Declare your connection name here
set connectionName to "INOC"

tell application "System Events"
      tell current location of network preferences
            set VPNservice to service connectionName
            if exists VPNservice then connect VPNservice
      end tell
end tell
do shell script "ifconfig ppp0 | grep inet  | awk '{printf(\"route add 10.0.0.0/24 -netmask 255.0.0.0 %s\", $2);}' | sh" user name "IChangedThis" password "AndThis" with administrator privileges

0
 
LVL 2

Expert Comment

by:dscl
ID: 22675412
For testing purposes try this please.  Connect to the VPN manually like you normally would and then create and run an applescript that is just this line

do shell script "ifconfig ppp0 | grep inet  | awk '{printf(\"route add 10.0.0.0/24 -netmask 255.0.0.0 %s\", $2);}' | sh" user name "me" password "mypassword" with administrator privileges

After running it does your connection work like you expect it too?  Just trying to determine where exactly we are failing at.

FYI I may not be able to check back in on this topic until tomorrow morning.
0
 

Author Comment

by:famoso
ID: 22675418
That works

0
 

Author Comment

by:famoso
ID: 22675425
So now can we call one AppleScript from another?  Or will there be a way to merge them somehow?
0
 

Author Comment

by:famoso
ID: 22675430
Maybe 'wait until the connection is connected' :)
0
 
LVL 2

Accepted Solution

by:
dscl earned 500 total points
ID: 22675438
LOL, yeah waiting might help.  See this is where it would help if I actually used the built-in VPN client and could test.

Try this

tell application "System Events"
      tell current location of network preferences
            set VPNservice to service connectionName
            if exists VPNservice then connect VPNservice
      end tell
end tell
delay 5
do shell script "ifconfig ppp0 | grep inet  | awk '{printf(\"route add 10.0.0.0/24 -netmask 255.0.0.0 %s\", $2);}' | sh" user name "IChangedThis" password "AndThis" with administrator privileges

And play with the delay time (measured in seconds) to give the system time to connect first :D
0
 

Author Comment

by:famoso
ID: 22675458
THAT, as they say, is the money shot!  That did it.  Thanks.  Still would have liked a pure bash solution but your worth the points.
0
 

Author Comment

by:famoso
ID: 22922544
Just FYI, this is my final outcome:

-- Declare your connection name here
set connectionName to "INOC"

tell application "System Events"
      tell current location of network preferences
            set VPNservice to service connectionName
            if exists VPNservice then connect VPNservice
            delay 10
      end tell
end tell

do shell script "ifconfig ppp0 | grep inet  | awk '{printf(\"route add 10.0.0.0/24 -netmask 255.0.0.0 %s\", $2);}' | sh" user name "myUserName" password "myPasswd" with administrator privileges
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Deploystudio is a system which can be used to deploy OSX clients and servers within the small/medium or large business environments. The system is built onto of the OSX Server NetBoot system and uses images & workflows as its core assets. While work…
The error "There was an error performing the update" occurred on a Mac OS X client workstation running  Symantec AntiVirus for Mac (http://www.symantec.com/business/products/purchasing.jsp?pcid=pcat_security&pvid=825_1) - the Enterprise product vers…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now