Archive for the ‘scripting’ Category

Automatic login for SSH

with 2 comments

When I’m out of office, I have to use VPN to connect to company internal network. But I don’t use VPN, instead I’m using SSH tunneling with socks proxy.

Problem is, I have to create a password with token software and input that password to my ssh process. Well I’m too lazy for this, so I create a script to automate the process with bash, awk and expect.

First I have to generate a password from token software (its a CLI one).

token=`echo "myPIN" | /path/to/token/software | awk blablabla`

You have to filter the output with awk so you get the password.

Ok, now I got a variable named $token which contain my token generated password, and looks like expect can’t get this variable. After googling around, I find out that I can use the variable if its exported to environment. So the next step is to export it to environment.

export $token

The last one is to automate ssh login.

expect -c 'spawn ssh -C -D 1080 ssh.server.com; expect assword; send "$env(token)\n";interact'

No, there is no typo in the command. “assword” is use because sometime ssh server return “Password” and sometime “password”. I use “-C” to compress all traffic.

So here is the complete script, named it whatever you want.

#!/bin/bash
token=`echo "myPIN" | /path/to/token/software
export $token
expect -c 'spawn ssh -C -D 1080 ssh.server.com; expect assword; send "$env(token)\n";interact'

Just execute the script and your SSH tunnel is created :)

Written by bagus

March 1, 2008 at 3:00 pm

Posted in brainware, linux, scripting

Tagged with , , , ,

Follow

Get every new post delivered to your Inbox.