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

Juniper Client

Its all about Networks

  • Juniper SRX
  • Juniper eBooks
  • Juniper Switches
    • Juniper Ex Switch
    • Juniper Networks Switches
    • Juniper Switch
  • Juniper Apps
  • News
  • Juniper eBooks
  • About Us
  • Show Search
Hide Search

Automating responses to scripts on Linux utilizing anticipate and autoexpect

vijesh · January 12, 2021 · Leave a Comment


The Linux anticipate command takes script writing to a completely new degree. As an alternative of automating processes, it automates working and responding to different scripts. In different phrases, you’ll be able to 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.

This is the bash script:

#!/bin/bash

echo "How are you doing?"
learn ans

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

#!/usr/bin/anticipate

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

If you run the script, you need to see this:

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

Observe 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 attainable timeout. The second kicks off the script that will probably be asking for a response utilizing spawn.

Observe that the anticipated prompts must 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”.

This is a barely extra sophisticated instance. If a script that you simply use asks you to establish 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 gives the identify 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 explicit listing, and you can do this extra simply by simply working the tar command. For extra advanced interactions, this could not be the case.

When to make use of anticipate

On the whole, the one time you’ll doubtless need to use anticipate is with scripts that ask far more questions than you’re feeling like answering or that you simply need to run if you’re not round. For instance, a script used to put in some explicit utility would possibly ask six important questions after which require that you simply reply “y” many extra occasions earlier than continuing with the set up. There could also be occasions, too, that you simply would possibly want to put in an utility on 32 servers or just need or have it put in in a single day whereas nobody is utilizing them. You possibly can automate that course of by asking anticipate to run the script and  present all the solutions for you.

This 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 person 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 person 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 will probably be requested and ship the fitting solutions to every one.

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

Autoexpect

There’s a solution to make making ready anticipate scripts loads simpler. That’s, you’ll be able to present your script as an argument to the autoexpect command and it’ll create the anticipate script for you. You employ 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 person 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 supplied. This 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 person 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 may then do installs with out having to produce the small print or simply schedule them to run on their very own.

Wrap-Up

The anticipate command is useful for working scripts that require an extended 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 can be high of thoughts.

Copyright © 2021 IDG Communications, Inc.

Filed Under: News

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

Juniper targets data-center automation with Apstra replace

Telemetry steps into the enterprise-networking highlight

Don’t Await a Refresh to Obtain a Fashionable Community

Cut back the Community Crew’s Workload with AI Applied sciences

Eight sizzling networking applied sciences for 2023

Received Community Downtime? Right here’s How you can Proactively Scale back It

IT Leaders Have a Inexperienced Alternative to Help Sustainability

Cloud suppliers ought to unify digital networking and SD-WAN

IT provide points have organizations shifting from just-in-time to just-in-case shopping for

Information middle networking developments to observe for 2023

Seize AI-driven Alternatives to Clear up Hybrid Work Challenges

How AI, Automation, and Zero Belief Can Enhance Enterprise Networks

For Searching IFSC Codes in Banks Visit Here

For Biographies visit Crazum.com

Footer

About Juniper Client

Juniper Client is a blog dedicated in solving juniper related problems like juniper srx load balancing, juniper routers, juniper switches etc. Juniper Client is the premier provider of information, intelligence and insight for Juniper Network and IT Executives. Our main focus is to deliver news, opinion and networking tools for managing business solutions. We offer a unique and valuable information for businesses to meet their marketing objectives. Read More...

FIND IT HERE

Copyright © 2023 · Daily Dish Pro on Genesis Framework · WordPress · Log in