• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
sharadchhetri

sharadchhetri

Tutorials On Linux, Unix & Open Source

  • Home
  • Linux Commands
  • Resources
    • Learn Linux
  • My WordPress plugins

How to Use Expect In Bash Script

December 7, 2010 by Sharad Chhetri 10 Comments

In this bash script we will use expect scripting. Expect is known primarily as an indispensable application for system administrators. We use expect generally in case where input from user is required , for eg. ssh, ftp, telnet, sudo etc.

Given below is sample bash script which will first ssh the remote host and then get command output from remote host (here, we used uname -a command at Step )

Step 1 : Create a new file.

vi expectcmd

Step 2 : Copy and paste below given content in file.
Change the value as per your information in variables –

HOST=Give Remote Hostname
USER=Give Remote Host user name
PASS=Give user’s password

#!/bin/bash
HOST="localhost"
USER="chitti"
PASS="123"
CMD=$@
XYZ=$(expect -c "
spawn ssh $USER@$HOST
expect \"password:\"
send \"$PASS\r\"
expect \"\\\$\"
send \"$CMD\r\"
expect -re \"$USER.*\"
send \"logout\"
")

echo "$XYZ"

And at the end, save the file.

Step 3: Make your file executable by owner of file , run the given below command.

chmod 750 expectcmd

Step 4: Give commands as argument along with expectcmd script. See below given example.
Note: (Try this command to get kernel info of Remote machine)

sh expectcmd "uname -a"

See the below given screenshot for reference.

expect command

Share this:

  • Twitter
  • Facebook
  • More
  • Print
  • Email
  • LinkedIn
  • Reddit
  • Tumblr
  • Pinterest
  • Pocket
  • Telegram
  • WhatsApp
  • Mastodon

Related posts:

  1. How To Use case in Bash Script, Example
  2. How To Use opt In Bash Script
  3. Set and reset user password by bash script
  4. send email after mysql backup through bash script in simple way
  5. Xen Server License Nagios Plugin (bash script)
  6. Nagios plugin check_mailfromd bash script
  7. MySQL backup bash script
  8. Replace keyword with its filename without extension : bash script
  9. Install VirtualBox On Ubuntu 22.04 LTS Desktop (Bash Script)
  10. Bash : print variable value inside single and double quotes

Filed Under: Linux Tagged With: bash script, expect

Reader Interactions

Comments

  1. Ines says

    October 25, 2021 at 6:36 am

    “Expect not found”

    How can you use expect interpreter in bash ? Those are two different interpreters.

    Reply
  2. yoshi says

    May 9, 2019 at 10:12 am

    perfect ! thanks

    Reply
  3. prakash says

    August 3, 2018 at 6:47 am

    help:
    I have servers hostname with 2 different domain suffix
    like :
    test12345.servera.com
    test12345.serverb.com
    how to diclare the domain variable on expect script; if server.com not login then login to server.com

    help needed urgent

    Reply
  4. Umesh Kaushik says

    March 14, 2017 at 6:37 am

    Hi thanks for this tutorial, however, I am getting this error, could you please help me here?
    ran command like this: sh expectcmd “uname -a”
    output:
    usage: send [args] string
    while executing
    “send ”
    spawn ssh hadoop[at]some-ip
    hadoop[at]some-ip’s password:

    Reply
    • Anon Y Mous says

      April 6, 2017 at 7:14 pm

      The “r” characters in the variables usage fields of the “send” portions of the script need to be preceeded by the “\” character. Please see the “cat expectcmd” example for exact syntax that bash is expecting. I also had same issue as I’m relatively new with bash scripting…hope this helps.

      Reply
      • Sharad Chhetri says

        April 7, 2017 at 5:08 pm

        Thanks Anon,

        In screenshot, person can find the exact way of writing code. Due to pre tag issue, it is not reflecting in webpage, will try to fix this.

        Regards
        Sharad

        Reply
  5. satheesh says

    August 4, 2016 at 1:59 am

    hi,
    this isfine to run 1 command uname -a, but if i want to 5 commands and in 50> servers, ho to do it..I tried different ways, but not working….

    Reply
    • Sharad Chhetri says

      August 5, 2016 at 10:14 am

      Hi Satheesh,

      Use for..loop with expect in bash script.

      Regards
      Sharad

      Reply
  6. Angel Campoverde says

    November 19, 2015 at 9:51 pm

    You either made a bunch of mistakes in the code you wrote above or your code was screwed by the webpage, first of all

    send “$PASSr” -> send “$PASSr” given that your password is $PASS and you want to do enter “r” after it

    Second all the commands inside the main quotation marks are escaped (ie ” -> “). For instace

    expect -c “spawn ssh -Y user@service.server.domain
    expect “Password:”
    send “$PSSWDr”
    interact”

    Reply
  7. Twiggy says

    November 28, 2012 at 9:08 am

    Wow! Great tihnking! JK

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Our Social Media Presence

  • Facebook
  • GitHub
  • Twitter

Linux Command

What is Linux Internal And External Command

Linux Basic Commands With Examples For Every Beginner

tr command to convert lines to space , tab and vertical tab

smbpasswd command not found on CentOS 7 and RHEL 7

Solution : semanage command not found

Unix / Linux : How to print duplicate lines from file

More Posts from this Category

You Might Like These Articles!

simplecodesyntax wordpress plugin

SimpleCodeSyntax : My Another WordPress Plugin

Install Nginx

How To Install Nginx On Ubuntu 22.04 LTS

Install Latest Git package in Ubuntu Operating System

How To Always Install Latest Git Package In Ubuntu Operating System

Bash script for installing VirtualBox on Ubuntu 22.04 LTS Desktop

Install VirtualBox On Ubuntu 22.04 LTS Desktop (Bash Script)

libfuse

dlopen(): error loading libfuse.so.2 – Got Error On Ubuntu

Failed to open/create the internal network

VirtualBox Error: Failed to open/create the internal network

Always Useful Tips And Tricks

curl command to check the http status

How to put slider shortcode only in home page in WordPress

Show visitor ip address php code

shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory

This kernel requires an x86-64 CPU but only detected an i686 CPU

Allow wget and yum in iptable

How to configure htdigest in nagios server

Explore 90+ Article On "Linux Tips And Tricks"

Copyright © 2023 ยท
The material in this site cannot be republished either online or offline, without our permission.
Proudly Blogging From Bharat.

  • Contact
  • About Me
  • My WordPress plugins
  • Privacy Policy