View Single Post
  #1  
Old 09-13-2011, 05:27 PM
mayler mayler is offline
Hot Shot
 
Join Date: Jul 2011
Posts: 23
mayler has 10 reputation points
Default Automating SSH Login using Expect

Figured I'd share this with the community. It works for all devices supporting SSH.
Requires expect

Instead of this:
pounding keyboard... ouch ah man, this sucks...everyday all the time, bla
ssh Manager@ip.add.re.ss
enter password, etc.

You'll tab complete devices and automatically login. Use the following script.

Create an executable file in /usr/local/bin/ called router-street-town.city.state (if you name your equipment in DNS, use the name. If not, use your favorite name for the equipment (ie: pos.network - but requires a change to ipaddr "")

#!/usr/bin/expect -f
set force_conservative 0 ;# set to 1 to force conservative mode even if
;# script wasn't run conservatively originally
if {$force_conservative} {
set send_slow {1 .1}
proc send {ignore arg} {
sleep .1
exp_send -s -- $arg
}
}

set ipaddr [exec basename $argv0]
set pword "yourpassword"
set timeout 10
spawn $env(SHELL)
match_max 100000
send -- "ssh -o StrictHostKeyChecking=no Manager@$ipaddr\r"
expect "Manager@$ipaddr's password:" { send "$pword\r" }
expect "*"
interact


exec basename $argv0 is the best part of this. Instead of telling the script to use set ipaddr router-street-town.city.state in the file, you are using the filename instead. This allows you to simply change the filename, or copy the file to a new filename
So I have 100+ devices I have to remote into once and a while, router-street-town.city.state, edge-street-town.city.state, etc. I don't always remember the site name so tab completion works too.
It might actually look like this:

ls *.ntwk
gw-rtr-e0-johnson.hos.ntwk
edge-1a-johnson.hos.ntwk
edge-2a-johnson.hos.ntwk
edge-2b-johnson.hos.ntwk

From my command prompt I'd just type:

edge"tab"
output:
edge-1a-johnson.hos.ntwk
edge-2a-johnson.hos.ntwk
edge-2b-johnson.hos.ntwk
edge
ETC. Tab completion and direct connect.
Reply With Quote