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
22
votes
5 answers

Why is my expect script failing on line 1?

The very first line of my expect script fails. Here are the entire contents of my script and it fails: #!/usr/bin/expect -f And it fails right off the bat with ": no such file or directory as my response. Expect is in fact installed and is…
Dennis Day
  • 607
  • 2
  • 5
  • 13
20
votes
6 answers

Is there an Expect equivalent gem for Ruby?

Is there an Expect equivalent gem for Ruby? I tried searching on code.google and rubygems.org, but sadly it did not show up. FYI: Expect is a Unix automation and testing tool, written by Don Libes as an extension to the Tcl scripting language, for…
Jokester
  • 5,501
  • 3
  • 31
  • 39
20
votes
2 answers

How to get expect -c to work in single line rather than script

Running: my_machine~/opt/ams/data/ep/success$ expect -c "spawn /usr/bin/scp xmlEventLog_2010-03-22T14-28-36_PFS_1_2.xml adaptive@10.10.12.17:/opt/ams/epf_3_4/xmlEventLog_2010-03-22T14-28-36_PFS_1277900174_2.xml; expect { '*password:*' { send…
amadain
  • 2,724
  • 4
  • 37
  • 58
20
votes
2 answers

How to use expect with optional prompts?

Let's say I am trying to write an expect script for a test.sh that has three prompts: prompt1, prompt2, prompt3. My code is like this: spawn test.sh expect "prompt1" send "pass1" expect "prompt2" send "pass2" expect "prompt3" send "pass3" However,…
joshualan
  • 2,030
  • 8
  • 23
  • 32
16
votes
2 answers

What unit testing frameworks are available for x86 assembler?

It seems that unit testing has become all the rage these days and I know many of you are going to think: "Well why not just use language X with framework Y already?" But I'm proposing this idea more as a proof of concept, or out of nostalgic…
Dwight Spencer
  • 1,472
  • 16
  • 22
16
votes
4 answers

Expect script does not work under crontab

I have an expect script which I need to run every 3 mins on my management node to collect tx/rx values for each port attached to DCX Brocade SAN Switch using the command #portperfshow# Each time I try to use crontab to execute the script every 3…
Redouane Nour
  • 161
  • 1
  • 1
  • 4
16
votes
3 answers

use expect to spawn command with arguments containing spaces

I want to use expect to run a simple command cat /tmp/id_rsa.pub over ssh. In a shell, I can run this wo problem, (with manually put in the password) ssh root@localhost 'cat /tmp/id_rsa.pub' I want to automate this with expect. My expect script…
Richard
  • 14,642
  • 18
  • 56
  • 77
16
votes
2 answers

how to check if a host is in your known_host ssh

I have the following command works in my script that adds the host to the known hosts in ssh. VAR2=$(expect -c ' spawn ssh -o StrictHostKeyChecking=no '"$REMOTE_HOST_USER@$REMOTE_HOST_IP"' expect "*?assword:*" send "'"$REMOTE_HOST_PASSWD"'\r" …
barp
  • 6,489
  • 9
  • 30
  • 37
14
votes
2 answers

expect script to automate telnet login

I have been trying to create an expect script to automatically login to my device through telnet If there are no multiple possibilities for the expect command , the script works fine, logs in to the device. #!/usr/bin/expect set timeout 20 set ip…
woodstok
  • 2,704
  • 3
  • 34
  • 50
14
votes
2 answers

Background spawned process in Expect

I'm using expect to start an application on my server: #!/usr/bin/expect set timeout -1 spawn "bin/start-all.sh" expect { -re "Found MongoDB in" { send "y\r"; exp_continue } -re "Found Hadoop in" { send "y\r"; exp_continue } -re "Going…
Lucas
  • 419
  • 2
  • 5
  • 11
14
votes
3 answers

When to Expect and When to Stub?

I use NMock2, and I've drafted the following NMock classes to represent some common mock framework concepts: Expect: this specifies what a mocked method should return and says that the call must occur or the test fails (when accompanied by a call…
Jeff Sternal
  • 47,787
  • 8
  • 93
  • 120
13
votes
8 answers

How to send a password with a $ symbol in Expect Script

I have an expect script that I need to login to a remote system and execute commands. This script works with the exception of providing the password to the root account. The root password contains a dollar sign that I cannot seem to get to work.…
linsek
  • 3,334
  • 9
  • 42
  • 55
12
votes
3 answers

pexpect can't pass input over 1024 chars?

I'm currently passing some input to a process with pexpect with the following code: p = pexpect.spawn('cat', timeout=5.0 ) p.maxread = 5000 p.setecho(False) # prevent the process from echoing stdin back to us INPUT_LEN =…
tba
  • 6,229
  • 8
  • 43
  • 63
12
votes
4 answers

git credential.helper does not store username password

I am writing a deployment script which clones a git repo and then performs other tasks such as fetch etc .I have run this git config --global credential.helper cache The username and password for cloning step are provided by an expect script. But…
iamads
  • 198
  • 1
  • 1
  • 8
12
votes
1 answer

Expect - Interrupt program - Ctrl+C

I am running the following script to start a capture on a remote server and download the file afterwards. Currently I have to pause it with Ctrl+C and manually exit. How can I replace the interact and define a trigger to kill the tcpdump or catch…
husvar
  • 373
  • 1
  • 3
  • 13