Automatic SSH Login Script

Posted on June 7, 2013

If you are like me, you are rarely working locally on your own system. I am almost always working on another server to run and test code. Connecting to an external server is as simple as jumping into your terminal and connecting via ssh (secure shell). This is simple and quick if you are just testing a quick piece of code occasionally. But, if you are running larger programs or need multiple windows all connected to the same server, it can quickly get very tedious to have to connect and reconnect multiple ssh sessions during your time coding. Also, if you aren’t directly connected to the same network that your server is on, you will need to be on a VPN (virtual private network) before you can establish a valid ssh session. I wrote a simple script that can take care of this for you, regardless of where you are.

NOTE: Everything in this post is mac specific.

In order for this to work, you must first setup a few things on your system.

  1. You must have a VPN setup in your network settings. You don’t HAVE to, but half of what makes this script so handy is that it connects you regardless of where you are and what network you are connected to at the time.
  2. You must setup secure ssh login with private and pubic keys. This allows you to ssh without typing your password every time. This article explains how to set this up.

 

Now, on to the code!

I will break this down into sections, and then compile it all below.

First, we need to know what network we are currently connected to before we can connect to a server. If we are not connected to the right network, we will need to connect via VPN first.

This saves the current connected network to the variable curNetwork

--save name of current connected network
set curNetwork to do shell script "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I|grep ' SSID: '|cut -c 18-"

Now, we need a function to connect to a VPN network. This is easily established with applescript. Here, “VPN NAME” is the name of your VPN network that can be found in your network settings.

on connectVPN()
	tell application "System Events"
		tell current location of network preferences
			set VPNservice to service "VPN NAME"
			set isConnected to connected of current configuration of VPNservice
			if not isConnected then
				connect VPNservice
			end if
		end tell
	end tell
end connectVPN

This creates an applescript function connectVPN that simply connects to the designated VPN service. Now all we need is a few more lines of code.

--see if I am on the right network or not
if curNetwork is not "serverNetwork" and curNetwork is not "serverNetwork2" then
	--I am not on the right network, start VPN
	connectVPN()
	--let the VPN connect
	delay 2
end if

The above lines of code simply checks to see if the network I am currently connected to is the same as the network that the server is currently on. These need to be predefined and filled into the “serverNetwork” and “serverNetwork2” spaces. If the network is not the same, we call our connectVPN() function from above to connect on the virtual private network.
Then all we need to do is open a terminal and connect! These lines take care of that quickly.

--now open terminal and ssh into the server
tell application "Terminal"
	do script "ssh [email protected]"
	delay 5
end tell

And that’s it!

Here is all the code together, let me know if you have any questions!

This code is also provided as a gist.

--save name of current connected network
set curNetwork to do shell script "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I|grep ' SSID: '|cut -c 18-"

on connectVPN()
  tell application "System Events"
		tell current location of network preferences
			set VPNservice to service "VPN NAME"
			set isConnected to connected of current configuration of VPNservice
			if not isConnected then
				connect VPNservice
			end if
		end tell
	end tell
end connectVPN

--see if I am on the right network or not
if curNetwork is not "serverNetwork" and curNetwork is not "serverNetwork2" then
	--I am not on the right network, start VPN
	connectVPN()
	--let the VPN connect
	delay 2
end if

--now open terminal and ssh into the server
tell application "Terminal"
	do script "ssh [email protected]"
	delay 5
end tell