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
0
votes
2 answers

Variable within an expect file

I have an expect file that looks like this. I invoke the script using expect myfile.exp I want to be able to send a value of 1,2,3 or 4. Is there a notion of variable within the expect file? expect -exact "> " sleep .1 send -s -- "1\r" expect…
Rajshekar Iyer
  • 111
  • 2
  • 14
0
votes
1 answer

Solaris 11 Expect script test for ssh-key already installed? WITHOUT ssh-copy-id ssh-pass

I have the following expect script on a Solaris 11.4 server. As Solaris 11 does not ship with ssh-copy-id or ssh-pass. I want to create a script to automate copying my ssh key to 100+ servers. After a bit of Google expect seems to be only option -…
AndyM
  • 562
  • 4
  • 22
0
votes
2 answers

Error transporting binary data over ssh/telnet (even shell) in expect script

It seems like no one is maintaining this repo and this repo any more. Assuming I'd like to fetch data from remote with user/password auth automating in expect script, like the following: ps: I use sh to refer ssh/telnet connection $ expect -v expect…
fangxlmr
  • 21
  • 2
0
votes
1 answer

Using tcl/expect to prepare a tunnel

As part of a bigger plan (jumping through a bunch of hops and then create a port-tunnel to mongodb in a setup that PortForwarding is disabled) I attempted to create a tcl/expect script to verify if it is possible to relay a stream prepared by…
NiMa Thr
  • 458
  • 3
  • 12
0
votes
1 answer

How to send ctrl + u + enter and ctrl + v + enter using expect4j java library?

Basically I would like to know how to send three key combinations while using expect4j java library. Enter key can be sent as send("\r"). Similarly how to do for three key combinations. Please help.
Yuvi
  • 21
  • 2
0
votes
3 answers

Trying to Create a remote login tool: "interact" in "expect << EOF" does not work

The use case of this script is I have various servers with different ssh keys. I am trying to write a script so when called will log into the specified server. An example of usage would be: ./ServerLogin.sh Server1 I feel that I am fairly…
Cody
  • 65
  • 6
0
votes
2 answers

"Expect" command line script to connect to VPN and enter password

I am trying to write an "Expect" script to connect to a VPN... Trying to write and expect (https://likegeeks.com/expect-command/) script to connect to a vpn, is this the right idea: The commands to connect are: sudo vpnName [ENTER] *Password*…
0
votes
1 answer

TCL expect re get substring with expect_out

I have a question about regular expressions in expect, I use the following expression: The string to match is: pwd /root root@FLC320-14-ACT:/root# I want to find the "/root" with following expect send "pwd\r" expect -re { …
0
votes
1 answer

Expect script fails when running from crontab but works when run manually

When I run this shell script with the crontab it doesn't work but if I do it manually if it works. I am missing an environment variable to configure or something additional needs to be done to make it work. Thanks for your help. Script:…
Tomas
  • 47
  • 1
  • 5
0
votes
2 answers

Make Expect throw error on unexpected output

I'm using the following expect script test.exp to test a CLI application I'm developing: spawn ./note_trainer.exe --seed 0 expect "note_trainer using seed 0" expect "Note G# transposed down 6 semi-tones gives what? " send "D\r" expect…
arvidj
  • 775
  • 5
  • 25
0
votes
0 answers

Getting script that works from terminal to work from run

I am using a script which is basically identical to the first one in https://stackoverflow.com/a/4783182/7238575. It works perfectly when run in the terminal or using the option "Run in terminal" (when setting the preference to asking what to do…
Kvothe
  • 233
  • 1
  • 8
0
votes
0 answers

My Tcl expect script on Centos 5.5 or 8.2 tries to ssh to a Windows 10 machine and connects but can't reliably get output

I have a Tcl script that uses Expect on a CentOS 5.5 (old legacy work environment) or a CentOS 8.2 machine that opens an SSH session to a Windows 10 machine. I'm able to successfully connect but the data I get from $expect_out(buffer) is not…
Bagels1b
  • 1
  • 1
0
votes
2 answers

Getting list of files using expect command in a shell script

How to get list of files using expect command in a shell script My script shell: remotepass="12345" echo echo "list files ssh" echo "==========" listDoc=$(ssh user@111.111.111.111 ls…
Tomas
  • 47
  • 1
  • 5
0
votes
0 answers

TCL expect -re match

I am trying to get information with telnet at different machines. To do this i am using the Expect package. My problem is to expect the right prompt because the machines has two different prompt -> or =>. I try to do this with expect -re "(->|=>) $"…
0
votes
2 answers

Improving an expect script to log onto whitelisted postgres instance

I am using expect for a streamlined login script to a whitelisted-only (thus low vulnerability) postgres instance. Here is the redacted script #!/usr/bin/expect set RDSI [lindex $argv 0] spawn /usr/local/bin/psql -U postgres -h $RDSI -d…
WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560