Automating interactive ssh commands in Linux

I ofter encountered problem while doing SSH to a remote server manually and giving the password when I am prompted for.

I found a way to automate this process or any other interactive shell operation using expect utility.This can also be used while doing FTP to a remote server when you want to automate all the repeatable operations .

install expect and autoexpect from your in your linux machine (Ubuntu)

sudo apt-get install expect

sudo apt-get install expect-dev

Issue the usual command prefixed with autoexpect to generate myexp.exp file

autoexpect -f myexp.exp ssh -l <username> <hostname>

Exit the shell immediately to stop recording and  open the file myexp.exp and remove everything after send command

send -- "<password>r"
Append  "interact" at the end so that interactive shell continues

Now issue the following command to automate the login process without being prompted for password

expect myexp.exp

one can also put it in .cshrc to set it as an alias to the login script.



  1. Excellent utility to automate scripts which require interaction with shell. It would be more appropriate for making an ftp connection to take backups on a regular basis. However it is a little risky to use this for ssh as the password would be stored in plaintext.

  2. Jesper Nexø JørgensenMay 25, 2012 at 5:19 PM

    Bad & unsecure way to automate logins!


Post a Comment

Popular posts from this blog

Bengali English word definition in Google Dictionary

Trace India Mobile Number

Load Testing Using Gatling