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.

2573 questions
146
votes
9 answers

Use Expect in a Bash script to provide a password to an SSH command

I'm trying to use Expect in a Bash script to provide the SSH password. Providing the password works, but I don't end up in the SSH session as I should. It goes back strait to Bash. My script: #!/bin/bash read -s PWD /usr/bin/expect <
Max
  • 11,487
  • 26
  • 79
  • 129
74
votes
3 answers

What are the best ways to automate a GDB debugging session?

Does GDB have a built in scripting mechanism, should I code up an expect script, or is there an even better solution out there? I'll be sending the same sequence of commands every time and I'll be saving the output of each command to a file (most…
Anonymous
  • 1,522
  • 3
  • 15
  • 21
70
votes
4 answers

Don't make me manually abort a LaTeX compile when there's an error

As suggested here, latexmk is a handy way to continually compile your document whenever the source changes. But often when you're working on a document you'll end up with errors and then latex will panic and wait for user input before continuing. …
dreeves
  • 25,132
  • 42
  • 147
  • 226
69
votes
6 answers

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

I am passing argument in Expect through the command line in a shell script. I tried this #!/usr/bin/expect -f set arg1 [lindex $argv 0] spawn lockdis -p expect "password:" {send "$arg1\r"} expect "password:" {send "$arg1\r"} expect "$…
lk121
  • 1,083
  • 2
  • 10
  • 10
40
votes
6 answers

Using expect to pass a password to ssh

How can I use expect to send a password to an ssh connection. say the password was p@ssword and the ssh command was ssh me@127.0.0.1 What would I do with expect to a make it input the password when it says me@127.0.0.1's password: ? The proper…
Malfist
  • 29,255
  • 58
  • 174
  • 263
39
votes
3 answers

How to access environment variables in an Expect script?

I would like to access the PATH environment variable inside an expect script. How can I achieve that ? My actual script is : #!/usr/bin/expect set timeout 300 send "echo $PATH\r" and its ouput is : can't read "PATH": no such variable while…
Xavier V.
  • 5,160
  • 5
  • 25
  • 34
29
votes
3 answers

Using conditional statements inside 'expect'

I need to automate logging into a TELNET session using expect, but I need to take care of multiple passwords for the same username. Here's the flow I need to create: Open TELNET session to an IP Send user-name Send password Wrong password? Send the…
shuckster
  • 4,871
  • 2
  • 19
  • 19
25
votes
3 answers

How can I make an expect script prompt for a password?

I have an expect script that connects to a few routers through ssh. All these routers have the same password (I know, it's wrong), and the script needs to know that password in order to be able to connect to the routers. Currently, the password is…
MiniQuark
  • 40,659
  • 30
  • 140
  • 167
23
votes
3 answers

How to get the exit code of spawned process in expect shell script?

I am trying to execute a script that executes an EXPECT script and a spawned process which has exit code in it. But I'm unable to get the exit code of the spawned process to main script. I'm always getting zero as success. expect script is : [Linux…
ANR
  • 483
  • 1
  • 3
  • 11
23
votes
2 answers

Any difference between "send_user" and "puts" in Expect?

It's not clear to me if send_user is the same as puts. Every time I want to send an informative message to the user, I would wonder which one I should use. From Expect's man page, it seems like send_user is the same as puts but then what is…
pynexj
  • 15,152
  • 5
  • 24
  • 45
22
votes
4 answers

What is the difference between spawn and exec?

I'm learning to write a TCL (expect) scripts and I notice that some examples show to use spawn, while others show the command exec. I tried googling, but can't find what is the difference? Suppose I call 'exec' in a middle of a long expect script,…
Milan Babuškov
  • 55,232
  • 47
  • 119
  • 176
22
votes
1 answer

How to return spawned process exit code in Expect script?

I use expect for running test scripts. Tests return success/failure through exit code. But expect return equivalent exit code. How to make expect return proper exit status? My tests are sql scripts run with psql (postgresql command processor). Since…
seas
  • 1,342
  • 2
  • 17
  • 28
22
votes
2 answers

How to get Command history by cursor key in Linux tclsh

Can get the command history by using cursor key (like up arrow key) in TCL shell (tclsh). I am running tclsh on fedora with linux version 2.6.21.
Adi
  • 1,439
  • 3
  • 18
  • 27
21
votes
2 answers

Hide output from expect

Here's part of an expect script #/usr/bin/expect spawn -noecho kwalletcli -f Passwords -e keyofmypassword expect ".*" set passwd $expect_out(buffer) # do some thing # ... It read password from kwalletcli, and store in variable passwd. So I can…
yegong
  • 669
  • 1
  • 6
  • 15
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,034
  • 3
  • 29
  • 37
1
2 3
99 100