Expect: Difference between revisions

From Leo's Notes
This page was last edited on 13 July 2021, at 06:02.
Initial Content
 
mNo edit summary
 
Line 3: Line 3:
Places where I've used expect include automating fdisk, installers (like the Nvidia Linux driver), and telnet.  
Places where I've used expect include automating fdisk, installers (like the Nvidia Linux driver), and telnet.  


== Introduction ==
==Introduction==


=== Installation ===
===Installation===
Install expect on your system by running:
Install expect on your system by running:


Line 12: Line 12:
<code>yum install expect</code>
<code>yum install expect</code>


=== Quick start ===
===Quick start===
There are two ways to write expect scripts:  
There are two ways to write expect scripts:  


# Write the expect script in a file with the <code>#!/usr/bin/expect</code> hash bang header and execute the script like any other shell script
#Write the expect script in a file with the <code>#!/usr/bin/expect</code> hash bang header and execute the script like any other shell script
# Pass the expect script during execution, like most short <code>awk</code> scripts.
#Pass the expect script during execution, like most short <code>awk</code> scripts.


An example expect script would look like this:
An example expect script would look like this:
Line 53: Line 53:
|<code>interact</code>
|<code>interact</code>
|Pass the program input to the user
|Pass the program input to the user
|-
|<code>set</code>
|Sets a variable.
Eg. Username set to the first argument: <code>set Username [lindex $argv 0]</code>
|}
|}
=== Conditions ===
Conditional reply based on an expected string:
{{Highlight
| code = expect {
    "*movie?" { send -- "The Godfather\r" }
    "*song?" { send -- "Sandstorm\r" }
}
| lang = expect
}}
Conditional reply based on some variable:
{{Highlight
| code = if ( $Username == "leo" ) {
  puts "leo\r"
} else {
  puts "other\r"
}
| lang = terminal
}}
<br />
<br />


== Tasks ==
==Tasks==


=== Use expect to telnet to a device and reboot it ===
===Use expect to telnet to a device and reboot it===
I have a Wyze Cam which I sometimes would want to reboot if something stops working with it. To do so, use the following expect script (which I inlined as part of a bash shell script):
I have a Wyze Cam which I sometimes would want to reboot if something stops working with it. To do so, use the following expect script (which I inlined as part of a bash shell script):
{{Highlight
{{Highlight
Line 80: Line 103:
}}
}}
Note that the <code>expect *#</code> after the reboot command is required. Without this final expect, expect will exit before the reboot gets triggered.
Note that the <code>expect *#</code> after the reboot command is required. Without this final expect, expect will exit before the reboot gets triggered.
<br />
== See Also ==
* https://likegeeks.com/expect-command/

Latest revision as of 06:02, 13 July 2021

Expect is a utility that makes it easy to automate commands that require user input via STDIN.

Places where I've used expect include automating fdisk, installers (like the Nvidia Linux driver), and telnet.

Introduction

Installation

Install expect on your system by running:

apt-get install expect

yum install expect

Quick start

There are two ways to write expect scripts:

  1. Write the expect script in a file with the #!/usr/bin/expect hash bang header and execute the script like any other shell script
  2. Pass the expect script during execution, like most short awk scripts.

An example expect script would look like this:

#!/usr/bin/expect -f

set timeout -1

spawn passwd

expect "New password:\r"
send "new-password123\r"

expect "Retype new password:\r"
send "new-password123\r"

expect eof

Looking at this example script, it should be evident what the script does. Simply put, an expect script starts a program (with spawn), then expects a certain string (expect), and then sends a reply based on this condition (send).

The commands that expect supports are listed below.

Command Description
spawn Starts a program or script
expect Wait for a specific program output
send Writes a reply to the program
interact Pass the program input to the user
set Sets a variable.

Eg. Username set to the first argument: set Username [lindex $argv 0]

Conditions

Conditional reply based on an expected string:

expect {
    "*movie?" { send -- "The Godfather\r" }
    "*song?" { send -- "Sandstorm\r" }
}

Conditional reply based on some variable:

if ( $Username == "leo" ) {
   puts "leo\r"
} else {
   puts "other\r"
}


Tasks

Use expect to telnet to a device and reboot it

I have a Wyze Cam which I sometimes would want to reboot if something stops working with it. To do so, use the following expect script (which I inlined as part of a bash shell script):

#!/bin/bash

if something-went-wrong ; then 
	expect -c "
		set timeout 10
		spawn telnet ${CAM_IP}
		expect \"login:\"
		send \"root\r\"
		expect \"Password:\"
		send \"${CAM_PASSWORD}\r\"
		expect \"*# \"
		send \"reboot\r\"
		expect \"*# \"
		exit"

fi

Note that the expect *# after the reboot command is required. Without this final expect, expect will exit before the reboot gets triggered.


See Also