Using Expect As A Command In Your Bash Scripts

In this post I will be showing you how to use expect in your scripts as a command instead of as a script itself. This can be useful for automatically responding when running other commands and more.

How To Install Expect

First we need to make sure expect is installed, depending on the distribution you are running, you can use one of the following commands.

sudo yum install expect
sudo apt-get install expect

How To Use Expect Commands

So lets get started by making a quick bash script and adding the expect command, I will explain how it works afterwards.

#!/bin/bash

echo "Memory Checker";
read -p "SSH Password: " password

expect -c "set timeout 30
spawn ssh user@yourdomain.com
expect assword
send $password/r
expect '$'
send free/r
expect '$'
send exit";

echo "Finished checking free memory";

The expect -c¬†command starts expect, we then set a timeout for 30 seconds (if the expected doesn’t happen in 30 seconds then terminate), then spawn is used to start whatever program or script you want to automate (in this case just ssh), then expect will be used to wait for a certain text and then send will send the response we want. You can also add an interact to the end if you wish to take over after a certain action is performed. Here is an example with interact.

#!/bin/bash

echo "Memory Checker";
read -p "SSH Password: " password

expect -c "set timeout 30
spawn ssh user@yourdomain.com
expect assword
send $password/r
expect '$'
send free/r
interact";

echo "Finished checking free memory";

Saving Expect’s Output To A Log File

If you would like to log your expect scripts output into a file, you can do so with a simple bash redirection. This will stop you from seeing the output or being able to interact though, that being said you can look at the log afterwards. Here is an example below.

#!/bin/bash

echo "Memory Checker";
read -p "SSH Password: " password

expect -c "set timeout 30
spawn ssh user@yourdomain.com
expect assword
send $password/r
expect '$'
send free/r
expect '$'
send exit" > /path/to/log.txt && echo "Success" || echo "Epic Fail";

echo "Finished checking free memory";

As you can see with the above examples, expect can be a very valuable tool when it comes to doing repetitive tasks. Here 2 quick tips that you should keep in mind when writing scripts in expect or adding expect commands to your scripts:

  1. Try to avoid hard coding passwords into your scripts if you don’t absolutely have to, if somebody else looks at your scripts source they will get the exposed password. Instead use read to prompt the user for a password.
  2. Always do a dry run before running a new expect script on multiple servers, if something gets messed up then you would probably prefer to only fix 1 server rather than multiple.

For more information, please checkout my other expect tutorial.Thanks for reading, please don’t forget to like/comment/share. Thanks!

You may also like

Leave a Reply

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