Auto SSH login using ‘expect’ tool
In Category Shell Scripting
The “expect” tool can be used to achieve auto login for interactive commands like telnet, ftp, ssh and others. Automatic login is useful to avoid unnecessary delays in work and also useful in automation. However this method requires your password to be stored in the script in plain text. But you can set the file permissions so that you only can access it.
Using “expect” tool, we can spawn a command and expect certain messages from server and send user-defined strings to automate interactive prompts. So the script will be typically specific to a server because other server may prompt for password using different message.
Let us take the following example ssh session where I am trying to open a ssh shell to core techpulp server from my pc with host name “lizpc”. First let us look at general usage to identify messages that we should expect from server before sending password.
[liz@lizpc ~]$ ssh liz@techpulp.com liz@techpulp.com's password: Last login: Sat Feb 28 11:20:45 2009 from lizpc.techpulp.com [liz@techpulp ~]$
From the above interactive session, we can identify that server is printing “liz@techpulp.com’s password:” as password prompt. So in the auto login script, we can expect this message from server before sending password.
The following expect script (myshell.exp) automates the above process.
#!/usr/bin/expect spawn ssh liz@techpulp.com expect "password:" send "lizpassword\n" interact
Note that the above script is set to run with “/usr/bin/expect” instead of standard shell. In the second line of the above script, we are informing “expect” tool to spawn the ssh command. As we know that server sends password prompt, in the third line, we inform “expect” tool to wait until it reads “password:“. Once the password prompt is seen from the server, in the fourth line, our password is sent along with new line character “\n” that is equivalent to pressing ENTER key. With that login process is completed and we inform “expect” tool to resort to normal interactive mode using “interact“.
Let us execute the script.
[liz@lizpc ~]$ chmod +x mylogin.exp [liz@lizpc ~]$ ./mylogin.exp spawn ssh liz@techpulp.com liz@techpulp.com's password: Last login: Sat Feb 28 11:23:41 2009 from lizpc.techpulp.com [liz@techpulp ~]$
The same script can be enhanced for fully automated ssh session. For example if you want to login to a server and create a zip file there and close the session, you can use above script to login and instead of using “interact”, you can do the following operations.
- expect the shell prompt
- send your tar command with new line “\n”
- expect shell prompt
- send exit
There are lot many uses of “expect” tool and some of them can be:
- Automation of modem commands to connect to ISP
- Automation of initiating backup jobs in remote server
- Automation of running command line interface (CLI) commands in a network appliance
Excellent script Liz, and u have presented it in a good way. Works perfect, I customized it to suit my requirement