Tutorial

Expect Script SSH Example Tutorial

Published on August 4, 2022
author

Pankaj

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, 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

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Pankaj

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
October 10, 2013

You’d better add ssh options : -oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null , to avoid be asked to add into ~/.ssh/known_hosts

- Tanky Woo

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    April 5, 2014

    Suppose i want to this stuff on 500 nodes, i cant do it sequentially, so for that i might want to use threading or some other way… do you have any idea about that ?

    - vikas

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      May 3, 2014

      Hi Pankaj, I used this and its very useful. Can you please let me know how can I do this in Java. My requirement is , I need to login to a server 1st and then remote login to another server and then do su and then run a script. ssh server1 ssh server2 su - user cd /path sh script.sh I need to do above commands from java. Can you please help me out !! Thanks, Madhu

      - Madhu

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        July 3, 2014

        I have a problem with Expect. I am supposed to do the following task which involves running a command script in Solaris. This command then asks the user two passwords (one after another). I am using Expect to do this task. However, I am unable to get the desired output and the script is failing. It is unable to send the password values #!/usr/bin/expect set pri_user_password [lindex $argv 0] set sec_user_password [lindex $argv 1] spawn -f expect "Password for property local_service_password : " { send “$pri_user_password\r” } expect "Password for property remote_service_password: " { send “$sec_user_password\r” } Can you tell me what the problem might be?

        - LearningExpect

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          August 18, 2014

          HEllo, I have a simple expect script to be executed from our automation server on a Cisco wireless controller (version - 7.6.110). I am using ssh to connect to the device as telnet is disable on it. Script reads as below:- # Login to device spawn telnet $tc_device_ip$ expect “Login:” send “$tc_device_username$\n” expect “User:” send “$tc_device_username$\n” expect “Password:” send “$tc_device_password$\n” # Save the config send “save config” send “y\n” send “exit\n” But when i try to run it fails with below error:- spawn ssh 10.70.6.21 couldn’t execute “ssh”: no such file or directory while executing “spawn ssh 10.70.6.21” (file “C:\Windows\Sysnative\config\systemprofile\AppData\Local\Temp\t014256.t22638” line 2) Created temporary file C:\Windows\system32\config\systemprofile\AppData\Local\Temp\t014256.t22638 Executing command: E:\NA\server\ext\expect\bin\expect.exe C:\Windows\Sysnative\config\systemprofile\AppData\Local\Temp\t014256.t22638 spawn ssh 10.70.6.21 couldn’t execute “ssh”: no such file or directory while executing “spawn ssh 10.70.6.21” It seems expect is not able to recognize ssh itself and throwing an error “spawn ssh 10.70.6.21”. Please assist me in identifying where i am going wrong it this. Thanks in Advance. -Abhi

          - Abhinadnan

            JournalDev
            DigitalOcean Employee
            DigitalOcean Employee badge
            August 22, 2014

            Hi Pankaj I have gone through this web page. It was very helpful to learn about expect scripts. I need one help from you. I wrote same script which you have mentioned in this page to login to remote server. After login I am going to particular path and call shell script inside expect script. Its is executing properly in terminal( I am using Linux server). When I am calling that script on jenkins. Its not excuting properly. Please help me out its very urgent. If you need some information please let me know. Thanks pawar

            - Gowtham

              JournalDev
              DigitalOcean Employee
              DigitalOcean Employee badge
              November 14, 2014

              Could you please share the packages for send and intreact commands, as while running command getting command not found error. The operating system am using was RHEL 6.5 .

              - MOHAN

                JournalDev
                DigitalOcean Employee
                DigitalOcean Employee badge
                December 4, 2014

                Hi there, I have a problem when trying to ssh to a solaris box using groovy script and expect To give you some background, I can ssh to the solaris box with no problem from command line using putty, I can also execute my groovy and expect script from Eclipse and I am able to connect. However when I try to do the same from a web server that is deployed on weblogic I get the following error: Error: java.lang. IllegalArgumentException: No Configuration was registered that can handle the configuration named com.sun.security.jgss.krb5.initiate This happens when I use def shell = ex.spawn(‘solarisBox’, 22, user,password) ex is an instantiated object of ExpectJ… a Java wrapper for expect if I try to use def shell = ex.spawn(“ssh user@solarisBox”) in my script shell.expect(prompt) shell.send(pass) I get Java.IOException broken pipe using groovy 1.5.5 and I believe the expect version is 2.0.7 Please help, thank you so much in advance.

                - Samir

                  JournalDev
                  DigitalOcean Employee
                  DigitalOcean Employee badge
                  July 20, 2015

                  Awesome :) now I can have up to six consoles open on a remote machine and monitor its logs with a single key combination Ctrl-Alt-m, want to learn how I did ? https://ychaouche.informatick.net/kde

                  - yassine

                    JournalDev
                    DigitalOcean Employee
                    DigitalOcean Employee badge
                    February 9, 2016

                    How can I ssh to one more session through an existing ssh session?

                    - Vamshi

                      Try DigitalOcean for free

                      Click below to sign up and get $200 of credit to try our products over 60 days!

                      Sign up

                      Join the Tech Talk
                      Success! Thank you! Please check your email for further details.

                      Please complete your information!

                      Become a contributor for community

                      Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

                      DigitalOcean Documentation

                      Full documentation for every DigitalOcean product.

                      Resources for startups and SMBs

                      The Wave has everything you need to know about building a business, from raising funding to marketing your product.

                      Get our newsletter

                      Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

                      New accounts only. By submitting your email you agree to our Privacy Policy

                      The developer cloud

                      Scale up as you grow — whether you're running one virtual machine or ten thousand.

                      Get started for free

                      Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

                      *This promotional offer applies to new accounts only.