Automated SSH Login with Password and Additional Commands

The normal answer to most ssh related problems is: “use public/private keys”
but sometimes this does not work…

What i did regularly was this:

  • ssh to xxx@asdads.com
  • enter password
  • sudo su admin
  • cd /apps/hosts/some_project

Thats not very complicated, but its frustrating. What I do now is: “sshxx”, which executes this excpect script in ~/bin/sshxx

#!/usr/bin/expect
set pass [lindex $argv 0] #get first argument
spawn ssh xxx@asdads.com
expect "assword" #matches Password and password
send "$pass\r"
expect "xxx@" #wait for the prompt
send "sudo su admin\r"
send "cd /apps/hosts/some_project\r"
send "clear\r" #clean up the mess
interact

(For Ubuntu you need to “sudo apt get install expect” first)