Expect Script SSH Example Tutorial

Expect script is a great linux/unix utility. I have to deal with a lot of unix servers in my daily life, whether it’s at work or it’s my hosting server. So I need to remember a lot of SSH users, their passwords and then SU users and passwords. It’s kind of messy when the number of servers are huge. So I thought of writing a script that will automatically login me to the server.

When I first started working on this, I got stuck as how to enter the SSH password because normal unix shells don’t have any way to send the password when it gets prompted for login. Then I come to know about expect script that allows to automate the interaction with programs that opens a terminal for input.

Expect Script

Expect Script is very easy to learn and as the name suggests it works by parsing the output of the command and when it matches the specified regular expression, it processes the specified instruction.

Expect Script SSH Example

Here is the script I created for automatically login to the SSH server and then login with super user and then run a simple command.


#Usage sshsudologin.expect <host> <ssh user> <ssh password> <su user> <su password>
set timeout 60
spawn ssh [lindex $argv 1]@[lindex $argv 0]
expect "yes/no" {
	send "yesr"
	expect "*?assword" { send "[lindex $argv 2]r" }
	} "*?assword" { send "[lindex $argv 2]r" }
expect "https://www.journaldev.com/1405/#" { send "su - [lindex $argv 3]r" }
expect ": " { send "[lindex $argv 4]r" }
expect "https://www.journaldev.com/1405/#" { send "ls -ltrr" }

Important Points about Expect Script

  1. Notice the first line that specifies that expect script will be used as interpreter.
  2. Expect script default timeout is 10 seconds, so I have set the timeout to 60 seconds to avoid any timeout issues if the login prompt takes time to come.
  3. Notice the expect command followed by the regular expression and then what should be send as a response. The first Yes/No choice is added to make sure that it doesn’t fail if remote server key is not already imported.
  4. The other expect regular expressions varies across the servers, for my server it ends with “#” but for some other servers it might end with “$”, so you may need to edit it accordingly. The only thing to check is that the regular expression for the expect command should matches, so that it will send the corresponding command or password.
  5. The last expect command just shows that we can send commands also once we are logged into the server.

Here is the output when I run the above expect script with correct parameters.

[email protected]:~$/Users/pankaj/scripts/sshloginsudo.expect 'journaldev.com' 'pankaj' 'ssh_pwd' 'su_user' 'su_pwd'
spawn ssh [email protected]
[email protected]'s password:
Last login: Sun Jun  9 19:54:17 2013 from c-67-161-57-160.hsd1.ca.comcast.net
[email protected] [~]# su - su_user
[email protected] [~]# ls -ltr
total 708

  1. The expect script can be reused since all the information is passed as arguments, so it’s best to create alias for each one of them for quick login and save your time in typing all the parameters. For example,
    alias journal="/Users/pankaj/scripts/sshloginsudo.expect 'journaldev.com' 'pankaj' 'ssh_pwd' 'su_user' 'su_pwd'"
  2. It’s always best to use single quotes to pass the arguments, since most of the time passwords contains special characters that can cause funny results if they are not quoted.

NOTE: The above script is tested on Mac OS and Linux systems.

Reference: SourceForge Page

By admin

Leave a Reply

%d bloggers like this: