View Single Post
Old 09-13-2011, 04: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
enter password, etc.

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

Create an executable file in /usr/local/bin/ called (if you name your equipment in DNS, use the name. If not, use your favorite name for the equipment (ie: - 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 "*"

exec basename $argv0 is the best part of this. Instead of telling the script to use set ipaddr 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,,, etc. I don't always remember the site name so tab completion works too.
It might actually look like this:

ls *.ntwk

From my command prompt I'd just type:

ETC. Tab completion and direct connect.
Reply With Quote