Linux Stall
  • Home
  • Android
  • How to
  • Perl
  • Tips
  • Tutorials
No Result
View All Result
Linux Stall
No Result
View All Result
Home Tips

Writing A Basic Expect Script For Automating Tasks

Chankey Pathak by Chankey Pathak
July 12, 2020
in Tips
46 0
0
Professional Developer programmer working a software website design and coding technology, writing codes and database in company office, Global cyber connection technology.
15
SHARES
771
VIEWS
Share on FacebookShare on Twitter

Today I will be showing you how to use a basic expect script inside of your bash scripts for automation. Expect works just like the name sounds, expect to see a prompt or other piece of text and then send the response. With this you can do things such as automatically entering password for SSH (not recommended for daily use), automatically answer prompts, etc.

Please note that for this to work you will need to have the expect package installed, which is usually not installed by default. You can usually install it by running one of the below commands depending on your distro.

sudo yum install expect -y
sudo apt-get install expect -y

In the quick and easy example below I will show you how to automatically SSH to a server and then automatically run multiple commands. You can also add the command interact afterwards to take over and input information manually, I will include this but it will be commented out.

#!/bin/bash

# Login credentials
password="your-password-here";
user="your-user-here";

print "Automatic SSH Connector";

/usr/bin/expect<<EOD
set timeout 30; # Time to wait before timing out if expected string doesn't appear
spawn ssh -o StrictHostKeyChecking=no "[email protected]$1"; # Start SSH session
expect "*?assword:"; # Expect password
send "$password\r"; # Send password credential
expect '$'; # Expect terminal prompt
send "df -h\r"; # Send "df -h" command
expect '$'; # Expect terminal prompt
send "free -m\r"; # Send "free -m" command
interact; # Let user take over and interact with terminal session
expect EOD
EOD

exit 0;

The way this script will work is it will start an SSH session to [email protected]$1 (./scriptname.sh yourdomain), then supply the password as-well as run a few commands and then turn control over to the user.

If you have any questions then feel free to ask, expect can be a great tool that really makes life easier.

Tags: automationbashshell scriptTips
Previous Post

How to Install And Enable SAR (SYSSTAT) On Linux Servers

Next Post

Using Basic Loops In Bash Scripts

Chankey Pathak

Chankey Pathak

Data Scientist at Morgan Stanley. I've been using Linux since past 12 years. I plan to share what I know about Linux in this blog.

Related Posts

conceptual image, methaphor of busy and fast business, man with airplane desk
Tips

Drastically Speed up your Linux System with Preload

July 23, 2020
The three-dimensional network topology infographics with ip addresses 3d illustration
Tips

Useful Remote IPMI Commands For Managing Servers

July 11, 2020
How to run sudo command without password on Linux?
FAQ

How to run sudo command without password on Linux?

June 21, 2015
Add auto-complete to “yum”
Tips

Add auto-complete to “yum”

August 18, 2013
How to display a digital clock in Linux terminal?
Tips

How to display a digital clock in Linux terminal?

September 20, 2012
random password from command line
Tips

Your own command line password generator!

February 17, 2012
Next Post
A programmer coding

Using Basic Loops In Bash Scripts

Leave a Reply Cancel reply

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

  • Terms and Conditions
  • Contact Us
  • About Us

© 2012 - 2020 Linux Stall - A place for all your Linux needs.

No Result
View All Result
  • Home
  • Android
  • How to
  • Perl
  • Tips
  • Tutorials

© 2012 - 2020 Linux Stall - A place for all your Linux needs.

Welcome Back!

Login to your account below

Forgotten Password?

Create New Account!

Fill the forms bellow to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In