Javascript required
Skip to content Skip to sidebar Skip to footer

Continue Connecting If You Expect to Find in This Location

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, expect script ssh, expect script tutorial 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. sshsudologin.expect

          #!/usr/bin/expect  #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 "yes\r" 	expect "*?assword" { send "[lindex $argv 2]\r" } 	} "*?assword" { send "[lindex $argv 2]\r" }  expect "# " { send "su - [lindex $argv 3]\r" } expect ": " { send "[lindex $argv 4]\r" } expect "# " { send "ls -ltr\r" } interact                  

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.

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

Extra Tips on expect script

  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

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.

Sign up

merionhicip1942.blogspot.com

Source: https://www.digitalocean.com/community/tutorials/expect-script-ssh-example-tutorial