/sc_assets/244/logo.png

Linux Automatic SSH tunnel script

Last updated by Shayne M on May 22, 2013 10:29

If you want to automate the login process so you can establish a tunnel as part of a script, or on startup you can create a small script to fill in your password automatically using the "expect" scripting language.

Ubuntu users will need to run "sudo apt-get install expect" to install this first

Create the file "vpnsecure-ssh" with a text editor with the following contents:

#!/usr/bin/expect -f
set timeout -1
set username [lrange $argv 0 0]
set password [lrange $argv 1 1]
set server [lrange $argv 2 2]
set localport [lrange $argv 3 3]
set localip [lrange $argv 4 4]
spawn ssh -C -D$localip:$localport -p443 $username@$server
match_max 100000
# Look for password prompt
expect "*?assword:*"
# Send password aka $password
send -- "$password\r"
send -- "\r"
interact

Save it, and make it executable (chmod +x vpnsecure-ssh in terminal or right click > properties > permissions > tick execute for ubuntu/gnome users)

Run it with the following format

vpnsecure-ssh USERNAME PASSWORD SERVERADDRESS LOCALPORT LOCALIP

For user barry, with password 124ndsad51x to connect to our Australian server and start a local proxy on port 8080 he would use:

vpnsecure-ssh barry 124ndsad51x ssh-au1.vpnsecure.me 8080 127.0.0.1

Autoconnect on startup

Once you've worked out the right command above for your situation and preferences, you can then have it auto-run on system startup:

  • Open the file /etc/rc.local as root/administrator (sudo gedit /etc/rc.local)
  • Insert your connect command with the full path to the vpnsecure-ssh script just above the line that says "exit 0" and save

VPNSecure Provides Secure VPN servers in over 45+ Countries

If you are a looking for a versatile easy to use VPN service, checkout Today