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
8
votes
1 answer

How to check if a variable is set in expect script?

I am doing something like this: #!/usr/bin/expect -f if {$out != ""} { send_user $out } But it doesn't work. Error message: can't read "out": no such variable while executing "if {$out != ""} { send_user $out }" (file "./test" line 3)
Jahid
  • 21,542
  • 10
  • 90
  • 108
8
votes
4 answers

exit expect {} without timeout

I am struggling with the classic problem of typing password automatically in ssh, and like everybody else I am stumbling in the dark regarding expect. Finally I cobbled together a script that kinda work: #!/usr/bin/expect -f # command line…
Chen Levy
  • 15,438
  • 17
  • 74
  • 92
8
votes
1 answer

how to clean up the previous expect_buf in an expect script

I've written a expect function to get the output of a command and my code is like below proc do_cmd {cmd id} { set spawn_id $id send "$cmd\r" expect "$cmd\r" expect { -re "\n(.*)\r\n" {return $expect_out(1,string)} default {exit 1} …
user1726366
  • 2,256
  • 4
  • 15
  • 17
8
votes
2 answers

Unattended (no-prompt) Homebrew installation using expect

According to the Homebrew installation instructions, the following command can be used to install: ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)" This works, but it needs user input two times; to confirm the install and…
olerass
  • 410
  • 2
  • 4
  • 8
8
votes
2 answers

How to determine the exit status code of a command send by pexpect

In the following code snippet, how do I find the exit code of make? Specifically, I need to know if make has failed or succeeded. Thanks for any inputs. process = pexpect.spawn("/bin/bash") process.expect("make\r")
doon
  • 2,311
  • 7
  • 31
  • 52
8
votes
2 answers

How to store output in a variable while using expect 'send' command

Thanks. But the account and password are needed. So I must send them and then send ovs-vsctl command. the scripts are something like this: spawn telnet@ip expect -re "*login*" { send "root" } expect -re "password*" { send…
user3153829
  • 81
  • 1
  • 1
  • 3
8
votes
1 answer

Terminating spawn sessions in expect

I'm trying to address an issue with an Expect script that logs into a very large number of devices (thousands). The script is about 1500 lines and fairly involved; its job is to audit managed equipment on a network with many thousands of nodes. As a…
Eleck
  • 197
  • 1
  • 1
  • 6
8
votes
3 answers

Read file into String and do a loop in Expect Script

What I am trying to do is to: Create a .exp file, which will read from the *.txt file from the same directory and parse all the content in the text file into a string variable in the expect script. Loop the string, which contains a series of host…
Tony
  • 233
  • 2
  • 7
  • 15
8
votes
4 answers

How to use a linux expect script to enter answer a prompt for password

I am having some trouble writing a script that will launch my forticlient vpn command line client and send my password when it is prompted. Here is my code: #!/usr/bin/expect -f set loadme "./forticlientsslvpncli --server myvpnserver --vpnuser…
wblakenc
  • 464
  • 1
  • 5
  • 19
8
votes
4 answers

How to wait for a process to complete using tcl-expect

I am writing a script using expect in which I have to rlogin to some host & after that I need to send some commands. Now I want to exit to that host and relogin again to some other host and send some commands. But the run of my script is not waiting…
XYZ_Linux
  • 3,247
  • 5
  • 31
  • 34
8
votes
2 answers

Why do I get a segmentation fault in my simple c++ program using libexpect.so?

I am busy with a project where I have to automate some processes in bash or ssh so I decided to use the libexpect.so library. If you don't know what libexpect is, it provides an expect extension that I can use in a c++ program, and expect is just a…
benzeno
  • 661
  • 1
  • 6
  • 13
8
votes
4 answers

How to automate telnet session using Expect?

I'm trying to write an expect script to automate telnet. This is what I have so far. #!/usr/bin/expect # Test expect script to telnet. spawn telnet 10.62.136.252 expect "foobox login:" send "foo1\r" expect "Password:" send "foo2\r" send "echo…
Justin
  • 742
  • 5
  • 17
  • 34
8
votes
1 answer

What is the difference in OCMock expect and stub methods?

I am trying to use OCMock for testing my app. But I am confused where should we use expect and where to use stub? Can anyone help please?
sanjana
  • 203
  • 2
  • 5
8
votes
3 answers

How can I assign a variable using $expect_out in TCL/EXPECT?

If I want to match DEF_23 using the following regexp: expect { -re "DEF_\[0-9]*" set result $expect_out(1,string) } why does it say no such element in array? How does $expect_out work, and how can I capture the DEF using a regexp and assign…
user707549
7
votes
5 answers

spawn_id: spawn id exp6 not open

I know that this issue is already mentioned here, but the solution does not work for me. I have this script (let's name it myscript.sh) that spawns a process on remote environment and that should interact with it. #!/usr/bin/expect log_user 0 set…
Eedoh
  • 5,818
  • 9
  • 38
  • 62