Questions tagged [expect]

Expect is a Unix/Linux extension for the Tcl scripting language. It was designed as an automation and testing tool, and it makes it trivial to interact with complex programs, such as remote shells and security maintenance. It has a particularly powerful response recognition.

Expect adds many extensions to the Tcl scripting language which simplify scripting interactive applications controlling such hard-to-interact programs as, for example, telnet, ftp, passwd, fsck, rlogin, tip, ssh, and others.

Expect/tcl can run a GUI interface or run from the command line. Since Tcl is a full featured programming language, Expect can easily interact with databases, webservers, etc. with intuitive, powerful, and easy-to-anticipate processing.

Expect is in the public domain. There is full support for Linux and Windows.

Links

Common "gotcha"s

Take careful note of this caveat from the expect(1) man page:

Expect takes a rather liberal view of scoping. In particular, variables read by commands specific to the Expect program will be sought first from the local scope, and if not found, in the global scope. For example, this obviates the need to place "global timeout" in every procedure you write that uses expect. On the other hand, variables written are always in the local scope (unless a "global" command has been issued). The most common problem this causes is when spawn is executed in a procedure. Outside the procedure, spawn_id no longer exists, so the spawned process is no longer accessible simply because of scoping. Add a "global spawn_id" to such a procedure.

Debugging Mode

The expect program has a debugging mode where it prints out exactly what it receives from the spawned programs and what it is looking for. This can be extremely helpful when working out why your scripts are not working! Enable it by passing the -d option on the command line.

2733 questions
7
votes
1 answer

How can I automate a (usually) interactive build script with Github Actions?

I'm trying to add CI to a project that uses a set of build scripts written in bash. The scripts prompt for input a few times for configuration information (setting flags, setting parameters, etc.) Does Github Actions have its own commands for…
Chara
  • 338
  • 6
  • 14
7
votes
5 answers

Is there an equivalent of Don Libes's *expect* tool for scripting interaction with web pages?

In the bad old days of interactive console applications, Don Libes created a tool called Expect, which enabled you to write Tcl scripts that interacted with these applications, much as a user would. Expect had two tremendous benefits: It was…
Norman Ramsey
  • 198,648
  • 61
  • 360
  • 533
7
votes
2 answers

How to count command line arguments in expect script?

This is my simple expect script: #!/usr/local/bin/expect puts "Argu 1 : [lindex $argv 0]" Run --- expect example.expt Testing Output ------ Argu 1: Tesing I need to count the arguments, which are passed by command line. Like this: expect…
Mr. Black
  • 11,692
  • 13
  • 60
  • 85
7
votes
2 answers

TCL/Expect equivalent of Bash $@ or how to pass arguments to spawned process in TCL/Expect

If somebody wants to call external program (which was passed as a Bash argument) from Bash and also pass it command line options (which were also passed as a Bash arguments) the solution is fairy…
Wakan Tanka
  • 7,542
  • 16
  • 69
  • 122
7
votes
1 answer

How to suppress expect send output?

I have an expect script which I'd like to behave as a fancy ssh program that hops several machines and sets up the environment on target machine before running the commands. I can use log_user 0/1 to turn off / on output from expect and that helps…
Bitdiot
  • 1,506
  • 2
  • 16
  • 30
7
votes
2 answers

How to start a shell script within "expect script"?

In this expect script there will be no ssh server connected, I just want to execute a ".sh" file locally, is that possible? For instance: #!/bin/expect command "xxx.sh" # a command which starts a certain shell script Also, is it possible to…
user3636706
  • 197
  • 1
  • 2
  • 15
7
votes
1 answer

Using two interact in a Expect script

I'm trying to write a script that connects to a Linux box via SSH and allows interactive control of Cisco equipment from there; after I finish controlling the equipment, I want to exit the shell, too. I have SSH keys and do not need a password to…
MiddleCloud
  • 210
  • 1
  • 2
  • 16
7
votes
5 answers

How to use 'expect' to copy a public key to a host?

I am using the following syntax to copy a public key to a host, in order to be able to log in afterwards to the host without password query: ssh-copy-id $hostname in which $hostname is the hostname of the system with the username, e.g.…
Alex
  • 41,580
  • 88
  • 260
  • 469
7
votes
5 answers

Bash/Expect Script for SSH

I am new to Expect and scripting in general. I am trying to make a few scripts to make my life a bit easier when pulling network device configurations. I managed to create a basic Expect script to SSH to a device and save the configuration. I want…
minorix
  • 71
  • 1
  • 1
  • 2
7
votes
1 answer

How to create a "loop" statement within an {expect} script?

Assumption is, this is the script #!/usr/bin/expect set a "__test__" i would like to create a loop inside this script so it can print the value for $a with the a number infront of it based on the loop. so if i wanted it to loop 3 times.. end…
user2712491
7
votes
3 answers

Expect regex number pattern

I'm having this poblem with a expect script. I want to capture when the output of a command is "0" and distinguish from other numbers or chains like "000" or "100" my code is: #!/usr/bin/expect -f set timeout 3 spawn bash send "echo 0\n" expect…
Dedalo
  • 131
  • 1
  • 1
  • 8
7
votes
1 answer

default timeout handler for expect script

I have a expect script that need to fail when certain any of the expect condition is not meet. For example: expect "Hello World" If the expect script does not find "Hello World" in certain amount of time, it should fail. According to expect manual,…
fzhou
  • 417
  • 1
  • 4
  • 11
7
votes
1 answer

color texts at expect shell

As the title, is there any way to use color texts at expect shell? Like the shell script echo command below. echo -e "\033[32m Hello World"
jaeyong
  • 8,951
  • 14
  • 50
  • 63
7
votes
3 answers

How to use Bash script variables in Expect conditional statements

I am writing a Bash script and using Expect to do sftp. Now in the Expect block I want to access a Bash variable in a conditional statement. But, I am unable to do so. How can do this? Also, the execution of this script is controlled from a C…
puneet agrawal
  • 343
  • 2
  • 4
  • 13
6
votes
1 answer

Expect is not recognizing $expect_out(buffer) or $expect_out(0,string)

I have Ubuntu 9.10 (and cannot change it in the near future due to testing) with expect5.45. Expect is working, but I have (at least) two issues. The first is that expect does not seem to be matching the string "password:". On my machine, the…
Mike Loutris
  • 61
  • 1
  • 3