HOME

TheInfoList



OR:

Expect is an extension to the Tcl scripting language written by Don Libes. The program automates interactions with programs that expose a
text terminal A computer terminal is an electronic or electromechanical hardware device that can be used for entering data into, and transcribing data from, a computer or a computing system. The teletype was an example of an early-day hard-copy terminal ...
interface. Expect, originally written in 1990 for the
Unix Unix (; trademarked as UNIX) is a family of multitasking, multiuser computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, an ...
platform, has since become available for
Microsoft Windows Windows is a group of several Proprietary software, proprietary graphical user interface, graphical operating system families developed and marketed by Microsoft. Each family caters to a certain sector of the computing industry. For example, W ...
and other systems.


Basics

Expect is used to automate control of interactive applications such as
Telnet Telnet is an application protocol used on the Internet or local area network to provide a bidirectional interactive text-oriented communication facility using a virtual terminal connection. User data is interspersed in-band with Telnet control ...
,
FTP The File Transfer Protocol (FTP) is a standard communication protocol used for the transfer of computer files from a server to a client on a computer network. FTP is built on a client–server model architecture using separate control and data ...
,
passwd passwd is a command on Unix, Plan 9, Inferno, and most Unix-like operating systems used to change a user's password. The password entered by the user is run through a key derivation function to create a hashed version of the new password, w ...
,
fsck The system utility fsck (''file system consistency check'') is a tool for checking the consistency of a file system in Unix and Unix-like operating systems, such as Linux, macOS, and FreeBSD. A similar command, CHKDSK, exists in Microsoft Windo ...
,
rlogin The Berkeley r-commands are a suite of computer programs designed to enable users of one Unix system to log in or issue commands to another Unix computer via TCP/IP computer network. The r-commands were developed in 1982 by the Computer System ...
, tip,
SSH The Secure Shell Protocol (SSH) is a cryptographic network protocol for operating network services securely over an unsecured network. Its most notable applications are remote login and command-line execution. SSH applications are based on a ...
, and others. Expect uses
pseudo terminal In some operating systems, including Unix and Linux, a pseudoterminal, pseudotty, or PTY is a pair of pseudo-device endpoints (files) which establish asynchronous, bidirectional communication ( IPC) channel (with two ports) between two or more p ...
s (Unix) or emulates a console (Windows), starts the target program, and then communicates with it, just as a human would, via the terminal or console interface. Tk, another Tcl extension, can be used to provide a
GUI The GUI ( "UI" by itself is still usually pronounced . or ), graphical user interface, is a form of user interface that allows users to interact with electronic devices through graphical icons and audio indicator such as primary notation, inst ...
.


Usage

Expect serves as a "glue" to link existing utilities together. The general idea is to figure out how to make Expect use the system's existing tools rather than figure out how to solve a problem inside of Expect. A key usage of Expect involves commercial software products. Many of these products provide some type of command-line interface, but these usually lack the power needed to write
script Script may refer to: Writing systems * Script, a distinctive writing system, based on a repertoire of specific elements or symbols, or that repertoire * Script (styles of handwriting) ** Script typeface, a typeface with characteristics of handw ...
s. They were built to service the users administering the product, but the company often does not spend the resources to fully implement a robust scripting language. An Expect script can spawn a shell, look up environmental variables, perform some Unix commands to retrieve more information, and then enter into the product's command-line interface armed with the necessary information to achieve the user's goal. After retrieving information by interacting with the product via its command-line interface, the script can make intelligent decisions about what action to take, if any. Every time an Expect operation is completed, the results are stored in a local variable called $expect_out. This allows the script to harvest information to feedback to the user, and it also allows conditional behavior of what to send next based on the circumstances. A common use of Expect is to set up a testing suite for programs, utilities or embedded systems. DejaGnu is a testing suite written using Expect for use in testing. It has been used for testing GCC and remote targets such as embedded development. Expect script can be automated using a tool called 'autoexpect'. This tool observes your actions and generates an Expect script using heuristics. Though generated code may be large and somewhat cryptic, one can always tweak the generated script to get the exact code. # Assume $remote_server, $my_user_id, $my_password, and # $my_command were read earlier in the script. # Open a Telnet session to a remote server, and wait # for a username prompt. spawn telnet $remote_server expect "username:" # Send the username, and then wait for a password prompt. send "$my_user_id\r" expect "password:" # Send the password, and then wait for a shell prompt. send "$my_password\r" expect "%" # Send the prebuilt command, and then wait # for another shell prompt. send "$my_command\r" expect "%" # Capture the results of the command into a variable. This # can be displayed, or written to disk. set results $expect_out(buffer) # Exit the Telnet session, and wait for a special # end-of-file character. send "exit\r" expect eof Another example is a script that automates FTP: # Set timeout parameter to a proper value. # For example, the file size is indeed big and the network # speed is really one problem, you'd better set this # parameter a value. set timeout -1 # Open an FTP session to a remote server, and # wait for a username prompt. spawn ftp $remote_server expect "username:" # Send the username, and then wait for a password prompt. send "$my_user_id\r" expect "password:" # Send the password, and then wait for an 'ftp' prompt. send "$my_password\r" expect "ftp>" # Switch to binary mode, and then wait for an 'ftp' prompt. send "bin\r" expect "ftp>" # Turn off prompting. send "prompt\r" expect "ftp>" # Get all the files send "mget *\r" expect "ftp>" # Exit the FTP session, and wait for a special # end-of-file character. send "bye\r" expect eof Below is an example that automates SFTP (with a password): #!/usr/bin/env expect -f # Procedure to attempt connecting; result 0 if OK, 1 otherwise proc connect # Read the input parameters set user index $argv 0set passw index $argv 1set host index $argv 2set location index $argv 3set file1 index $argv 4set file2 index $argv 5 #puts "Argument data:\n"; #puts "user: $user"; #puts "passw: $passw"; #puts "host: $host"; #puts "location: $location"; #puts "file1: $file1"; #puts "file2: $file2"; # Check if all were provided if # Sftp to specified host and send the files spawn sftp $user@$host set rez onnect $passwif puts "\nError connecting to server: $host, user: $user and password: $passw!\n" exit 1 Using passwords as command-line arguments, like in this example, is a huge security hole, as any other user on the machine can read this password by running " ps". You can, however, add code that will prompt you for your password rather than giving your password as an argument. This should be more secure. See the example below. stty -echo send_user -- "Enter Password: " expect_user -re "(.*)\n" send_user "\n" stty echo set PASS $expect_out(1,string) Another example of automated SSH login to a user machine: # Timeout is a predefined variable in Expect which by # default is set to 10 seconds. # spawn_id is another predefined variable in Expect. # It is a good practice to close spawn_id handle # created by spawn command. set timeout 60 spawn ssh $user@machine while wait close $spawn_id


Alternatives

Various projects implement Expect-like functionality in other languages, such as C#,
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's mo ...
, Scala,
Groovy ''Groovy'' (or, less commonly, ''groovie'' or ''groovey'') is a slang colloquialism popular during the 1950s, '60s and '70s. It is roughly synonymous with words such as "excellent", "fashionable", or "amazing", depending on context. History The ...
,
Perl Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was offi ...
,
Python Python may refer to: Snakes * Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia ** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia * Python (mythology), a mythical serpent Computing * Python (pr ...
,
Ruby A ruby is a pinkish red to blood-red colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sap ...
,
Shell Shell may refer to: Architecture and design * Shell (structure), a thin structure ** Concrete shell, a thin shell of concrete, usually with no interior columns or exterior buttresses ** Thin-shell structure Science Biology * Seashell, a hard o ...
and Go. These are generally not exact clones of the original Expect, but the concepts tend to be very similar.


C#


Expect.NET
nbsp;— Expect functionality for C# (.NET)
DotNetExpect
nbsp;— An Expect-inspired console automation library for .NET


Erlang


lux
- test automation framework with Expect style execution commands.


Go


GoExpect
nbsp;- Expect-like package for the Go language
go-expect
nbsp;- an Expect-like Go language library to automate control of terminal or console based programs.


Groovy


expect4groovy
nbsp; - a Groovy DSL implementation of Expect tool.


Java


ExpectIt
— a pure Java 1.6+ implementation of the Expect tool. It is designed to be simple, easy to use and extensible.
expect4j
nbsp;— an attempt at a Java clone of the original Expect
ExpectJ
nbsp;— a Java implementation of the Unix expect utility
Expect-for-Java
nbsp;— pure Java implementation of the Expect tool
expect4java
nbsp; - a Java implementation of the Expect tool, but supports nested closures. There is also wrapper for Groovy language DSL.


Perl


Expect.pm
nbsp;—
Perl Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was offi ...
module (newest version a
metacpan.org


Python


Pexpect
nbsp;—
Python Python may refer to: Snakes * Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia ** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia * Python (mythology), a mythical serpent Computing * Python (pr ...
module for controlling interactive programs in a pseudo-terminal
winpexpect
nbsp;— port of pexpect to the Windows platform
paramiko-expect
nbsp;— A Python expect-like extension for the Paramiko
SSH The Secure Shell Protocol (SSH) is a cryptographic network protocol for operating network services securely over an unsecured network. Its most notable applications are remote login and command-line execution. SSH applications are based on a ...
library which also supports tailing logs.


Ruby


RExpect
nbsp;— a drop in replacement for the expect.rb module in the standard library.
Expect4r
nbsp;— Interact with Cisco IOS, IOS-XR, and Juniper JUNOS CLI


Rust


rexpect
nbsp;- pexpect-like package for the Rust language.


Scala


scala-expect
nbsp;— a Scala implementation of a very small subset of the Expect tool.


Shell


Empty
nbsp;— expect-like utility to run interactive commands in the Unix shell-scripts
sexpect
nbsp;— Expect for shells. It's implemented in the client/server model which also supports attach/detach (like
GNU screen GNU Screen is a terminal multiplexer, a software application that can be used to multiplex several virtual consoles, allowing a user to access multiple separate login sessions inside a single terminal window, or detach and reattach sessions ...
).


References


Further reading

* *


External links

* * * {{Webarchive, url=https://web.archive.org/web/20171025185023/https://www.ibm.com/developerworks/community/blogs/brian/entry/when_to_use_expect_scripting_and_when_to_avoid_it10, date=2017-10-25, title=When to use Expect scripting and when to avoid it (IBM Developerworks) Scripting languages Free software programmed in Tcl Automation software Tk (software) Public-domain software with source code