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.


Reference
http://blog.rackwire.com/2010/05/using-expect-and-autoexpect-to-automate-tasks-in-linux/