Automating responses to scripts on Linux utilizing anticipate and autoexpect

gears orange large efficient automated machine learning automation

Thinkstock

The Linux anticipate command takes script writing to a completely new stage. As an alternative of automating processes, it automates working and responding to different scripts. In different phrases, you possibly can write a script that asks how you’re after which create an anticipate script that each runs it and tells it that you simply’re okay.

Here is the bash script:

#!/bin/bash

echo “How are you doing?”
learn ans

Here is the anticipate script that gives the response to the question:

#!/usr/bin/anticipate

set timeout -1
spawn ./ask # ask is title of script to be run
anticipate “How are you doing?r”
ship — “okr”
anticipate eof

Once you run the script, you must see this:

$ ./ask.exp
spawn ./ask
How are you doing?
okay

Word that you’d simply invoke the anticipate script (ask.exp). The script itself solutions the query for you.

The primary line of the anticipate script (ask.exp) proven above disables a doable timeout. The second kicks off the script that shall be asking for a response utilizing spawn.

Word that the anticipated prompts need to match what’s requested exactly. Even “How are you doing?” and “How are you doing? ” (observe the additional area between the query mark and citation mark) aren’t matches. And, in case your script consists of the command echo -n “How are you doing? “, the anticipate script wants to make use of anticipate “How are you doing? “, not anticipate “How are you doing? n”.

Here is a barely extra sophisticated instance. If a script that you simply use asks you to determine a listing that you simply need to again up within the type of a compressed tar file, it would appear like this:

#!/bin/bash

echo -n “Listing> ”
learn dir
tar -czf $dir.tar.gz ~/$dir

The corresponding anticipate script that runs the script and supplies the title of the listing to be backed up would possibly appear like this:

#!/usr/bin/anticipate

set timeout -1
spawn ./dirbackup
anticipate “Listing> ”
ship — “PNGsr”
anticipate eof

Clearly, this script is not significantly helpful as a result of it backs up only one specific listing, and you could possibly try this extra simply by simply working the tar command. For extra complicated interactions, this may not be the case.

When to make use of anticipate

Usually, the one time you’d probably need to use anticipate is with scripts that ask far more questions than you are feeling like answering or that you simply need to run if you’re not round. For instance, a script used to put in some specific software would possibly ask six crucial questions after which require that you simply reply “y” many extra instances earlier than continuing with the set up. There could also be instances, too, that you simply would possibly want to put in an software on 32 servers or just need or have it put in in a single day whereas nobody is utilizing them. You would automate that course of by asking anticipate to run the script and supply the entire solutions for you.

Here is an instance, although the script on this case is not actually doing something with the solutions that it collects.

The set up script:

#!/bin/bash

echo -n “Location of set up file> ”
learn dir
echo -n “Which parts to you want to set up [all,core,enduser]> ”
learn ans
echo -n “Is your license file up-to-date? [Y,n]> ”
learn ans
echo -n “Ought to consumer responses be logged? [Y,n]> ”
learn ans
echo -n “Enter Y if prepared to start set up [Y,n]> ”
learn ans

The anticipate script that may run this script will look comparable:

#!/usr/bin/anticipate

set timeout -1
spawn ./installapp

anticipate “Location of set up file> ”
ship “/var/installsr”
anticipate “Which parts to you want to set up [all,core,enduser]> ”
ship “allr”
anticipate “Is your license file up-to-date? [Y,n]> ”
ship “Yr”
anticipate “Ought to consumer responses be logged? [Y,n]> ”
ship “Yr”
anticipate “Enter Y if prepared to start set up [Y,n]> ”
ship “Yr”
anticipate eof

The set up script would, in fact, want to make use of the collected solutions and run the right instructions to put in the required software program. The trick right here is to seize the questions that shall be requested and ship the correct solutions to every one.

Word that the sequences used on this anticipate script are required to flee the sq. brackets.

Autoexpect

There’s a technique to make getting ready anticipate scripts lots simpler. That’s, you possibly can present your script as an argument to the autoexpect command and it’ll create the anticipate script for you. You utilize a command like autoexpect ./installapp and it’ll construct an anticipate script with the solutions that you simply present:

$ autoexpect ./installapp
autoexpect began, file is script.exp
Location of set up file> /var/installs
Which parts to you want to set up [all,core,enduser]> all
Is your license file up-to-date? [Y,n]> Y
Ought to consumer responses be logged? [Y,n]> Y
Enter Y if prepared to start set up [Y,n]> Y
autoexpect carried out, file is script.exp

The resultant script.exp file will then embrace a proof that it was created with autoexpect and can embrace the responses you offered. Here is what we would see within the file after the interactions proven above:

$ tail -18 script.exp
set timeout -1
spawn ./ask04
match_max 100000
anticipate -exact “Location of set up file> ”
ship — “/var/installsr”
anticipate -exact “/var/installsr
Which parts to you want to set up [all,core,enduser]> ”
ship — “allr”
anticipate -exact “allr
Is your license file up-to-date? [Y,n]> ”
ship — “Yr”
anticipate -exact “Yr
Ought to consumer responses be logged? [Y,n]> ”
ship — “Yr”
anticipate -exact “Yr
Enter Y if prepared to start set up [Y,n]> ”
ship — “Yr”
anticipate eof

The autoexpect device prepares the script for non-interactively working an set up. You’ll be able to then do installs with out having to provide the main points or simply schedule them to run on their very own.

Wrap-Up

The anticipate command is helpful for working scripts that require a protracted sequence of solutions and permits you to run them in an un-manned style whereas autoexpect makes it straightforward to create anticipate scripts with out stressing out over the syntactical particulars.

Be a part of the Community World communities on

Fb

and

LinkedIn

to touch upon subjects which might be high of thoughts.

Sandra Henry-Stocker has been administering Unix methods for greater than 30 years. She describes herself as “USL” (Unix as a second language) however remembers sufficient English to put in writing books and purchase groceries. She lives within the mountains in Virginia the place, when not working with or writing about Unix, she’s chasing the bears away from her hen feeders.

Copyright © 2021 IDG Communications, Inc.

Supply

Germany Devoted Server

Leave a Reply