Expect linux command line

How to pass argument in Expect through the command line in a shell script

However, your method should work as well. Check that arg1 is retrieved. For example, with send_user «arg1: $arg1\n» .

#!/usr/bin/expect set username [lindex $argv 0] set password [lindex $argv 1] log_file -a "/tmp/expect.log" set timeout 600 spawn /anyscript.sh expect "username: " < send "$username\r" >expect "password: " < send "$password\r" >interact 

I like the answer provided with this guide.

It creates a parse argument process.

#process to parse command line arguments into OPTS array proc parseargs  < global OPTS foreach $argv < switch -exact -- $key < "-username" < set OPTS(username) $val >"-password" < set OPTS(password) $val >> > > parseargs $argc $argv #print out parsed username and password arguments puts -nonewline "username: $OPTS(username) password: $OPTS(password)" 

The above is just a snippet. It’s important to read through the guide in full and add sufficient user argument checks.

Note, sometimes argv 0 is the name of the script you are calling. So if you run it that way, argv 0 doesn’t work.

expect script.exp password 

That makes argv 1 = password and argv 0 = script.exp.

Arguments with spaces are fine, assuming the argument you want is the first after the script name ( $0 is script name, $1 is the first argument, etc.)

Make sure you use «$ARG» , not $ARG as it will not include the whitespace, but break them up into individual arguments. Do this in your Bash script:

#!/bin/bash ARG="$1" echo WORD FROM BASH IS: "$ARG" #test for debugging expect -d exp.expect "$ARG" exit 0 

Also, as the first answer states, use debug mode, (the -d flag). It will output your argv variables as Expect sees them and should show you what is going on.

Источник

Linux expect Command With Examples

System administrators automate repetitive tasks all the time. Bash scripts provide many programs and features to carry out system automation tasks. The expect command offers a way to control interactive applications which require user input to continue.

Читайте также:  Linux определить ssd диск

This article explains how to use the expect command with practical examples.

Linux expect Command With Examples

  • Access to the command line/terminal.
  • An account with sudo permissions.
  • A text editor, such as Vim or nano.

Linux expect Command Syntax

The expect command runs Expect program scripts using the following syntax:

expect [options] [commands/command file]

The Except program uses the following keywords to interact with other programs:

Command Function
spawn Creates a new process.
send Sends a reply to the program.
expect Waits for output.
interact Enables interacting with the program.

Expect uses TCL (Tool Command Language) to control the program flow and essential interactions.

Some systems do not include Expect by default.

To install it with apt on Debian-based systems, run the following in the terminal:

Alternatively, use yum on Red Hat-based systems:

Follow the installation instructions to complete the setup.

Linux expect Command Options

Below is a table describing available command options for the expect command:

Command Description
-c Specifies the command to execute before the script.
-d Provides a brief diagnostic output.
-D Interactive debugger.
-f Specifies a file to read from.
-i Prompts commands interactively.
-b Reads file line by line (buffer).
-v Print version.

Linux expect Command Examples

The next sections provide practical examples of the expect command, which executes Expect program scripts. To make an Expect script executable, add the following shebang at the start of each script:

The location differs depending on the system. To find the exact path, use:

Exchange the location if Expect is at a different location.

Basic Expect Use

Below is a basic example that explains how the expect command functions:

1. Open a text editor and name the file interactive_script.sh. If you use Vim, run:

2. Add the following code to the file:

#!/bin/bash echo "Hello, who is this?" read $REPLY echo "What's your favorite color?" read $REPLY echo "How many cats do you have?" read $REPLY

interactive_script.sh contents

The code is a basic script with the read command that expects user interaction when run.

Читайте также:  Включение графического интерфейса linux

3. Save the file and close Vim:

4. Change the script to executable:

chmod +x interactive_script.sh

5. Create a new file to store the Expect script with:

The .exp extension is not mandatory, though it helps differentiate Expect scripts from other files.

6. Add the following code to the script:

#!/usr/bin/expect spawn ./interactive_script.sh expect "Hello, who is this?\r" send -- "phoenixNAP\r" expect "What's your favorite color?\r" send -- "Blue\r" expect "How many cats do you have?\r" send -- "1\r" expect eof

expect_response.exp contents

The script consists of the following lines:

  • spawn creates a new process running the interactive_script.sh file.
  • expect writes the expected program message and waits for the output. The final line ends the program.
  • send contains the replies to the program after each expected message.

7. Save the file and close:

8. Make the script executable:

chmod +x expect_response.exp
expect expect_response.exp

expect expect_response.exp terminal output

The expect_response.exp script spawns the program process and sends automatic replies.

Expect with Variables

Use the set command to store variables and pass values in Expect scripts. For example, to hardcode a variable, use:

For user input arguments use:

set MYVAR1 [lindex $argv 0] set MYVAR2 [lindex $argv 1]

In both cases, reference the variable in the script with $ .

The following example demonstrates using both variables in the Expect script from the previous example (expect_response.exp):

#!/usr/bin/expect set NAME "phoenixNAP" set COLOR "Blue" set NUMBER [lindex $argv 0] spawn ./interactive_script.sh expect "Hello, who is this?\r" send -- "$NAME\r" expect "What's your favorite color?\r" send -- "$COLOR\r" expect "How many cats do you have?\r" send -- "$NUMBER\r" expect eof

expect script with variables

Passing a number specifies the $NUMBER variable:

expect_response.exp variables terminal output

The other two variables ( $NAME and $COLOR ) are hardcoded.

Expect with Commands

Use the expect command to automate responses to other programs and commands.

For example, the passwd command prompts the user to input the password twice. While the process is simple for one user, difficulties arise when adding a default password for hundreds of new users as a system administrator.

Expect easily automates the responses to other commands.

Читайте также:  Перезапуск всех демонов linux

1. The following script provides an example of using the expect command with the passwd command:

#!/usr/bin/expect set USER [lindex $argv 0] set PASS [lindex $argv 1] set timeout 1 spawn passwd $USER expect -exact "Enter new UNIX password: " send -- "$PASS\r" expect -exact "Retype new UNIX password: " send -- "$PASS\r" expect eof

expect password user input script

The code takes two passed arguments as the username and password to provide for the passwd command.

sudo expect passwd.exp terminal output

The script automatically sets the password for the provided username.

3. To automate the task for multiple users, use a while loop. The syntax for the TCL while loop is different from the Bash while loop:

#!/usr/bin/expect set PASS «Welcome@123» set i 1 set timeout 1 while

expect password change multiuser

The script assumes all users have the username user1 , user2 , etc., up to user10 . The password is hardcoded as Welcome@123 . After each user, the code increments the i value.

sudo expect passwd.exp multiuser terminal output

For the code to work, the example assumes the users already exist on the system.

Note: Exposing the script in any way makes the default password vulnerable. After this step, all users should change their password to a stronger password and opt-in for a password manager.

Autoexpect

Instead of writing Expect scripts from scratch, the Autoexpect program helps generate scripts interactively.

To demonstrate how Autoexpect works, do the following:

1. Run the Bash script from the first example (interactive_script.sh) using Autoexpect:

autoexpect ./interactive_script.sh

autoexpect script generation terminal output

The output prints a confirmation message and the Expect script name (script.exp) to the console.

2. Provide answers to the questions. The replies save to the script.exp file and generate the Expect program code automatically. When complete, the output prints a confirmation.

3. Review the generated script in a text editor to see the code:

autoexpect script contents

The interactions are saved to the Expect script for future use.

After following the steps from this guide, you know the purpose of Expect scripts, the expect command, and some use cases.

Expect is a powerful automation tool for system administration tasks and code testing. Use the man command to review the complete manual for Expect.

Источник

Оцените статью
Adblock
detector