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

Expect: "missing close-brace" and send command failure

I'm trying to run an expect script that SSHs to another local device to install and run a python script. All variables are defined outside of the script: set prompt "$SSHuser@$SSHname:~$" spawn ssh -o \"StrictHostKeyChecking no\"…
0
votes
0 answers

Spawn some other .sh file from the middle while leaving the other spawn open

I have following expect file: spawn ./SpeedVPN.sh [lindex $argv 0] expect "password for user:" send "123456789\r" expect "Enter 'yes' to accept, 'no' to abort; anything else to view:" send "yes\r" expect "Username:" send "vpnUserName\r" expect…
Masoudy BaBa
  • 383
  • 2
  • 12
0
votes
1 answer

Write ActiveState Tcl Program for Windows that expects a password and enters it

I want to write a shell program in windows that runs another shell script and expects a password prompt from the Git bash terminal and inputs it. This is what I have so far: #!/bin/sh # \ exec tclsh "$0" ${1+"$@"} package require Expect spawn…
JasonN
  • 11
  • 3
0
votes
1 answer

Expect: How does one export the executing environmt to the runtime environment? (where keys are sent)

I have tried the following: set timeout -1 spawn $env(SHELL) match_max 100000 send -- "ssh somewhere@addr" expect "*" for { set i 0 } { $i < [array size env] } { incr i } { send -- "echo env($i) = $env($i)" expect "*" } send -- "env >…
Chris
  • 28,822
  • 27
  • 83
  • 158
0
votes
1 answer

Linux shell: why "send" command doesn't run

I need some help on below code. I just need to achieve some simpliest task. bakcup and replace some ssl certificate files. but it doesn't seem to work. what is wrong with below code: import_esx() { local username=root local backuptimestamp=$(date…
Jerry Jie
  • 1
  • 2
0
votes
0 answers

May I know what is the equivalent command for "cat" in expect shell script

I am writing a script to generate VMware vcsa ssl certificate csr file and then print it out on screen. Here is my code: /usr/bin/expect << EOF set timeout 300 spawn ssh -l root $Ip_Address expect { "(yes/no)?" { send "yes\r";…
Jerry Jie
  • 1
  • 2
0
votes
1 answer

Why does Expect dump commands without execution?

I'm learning Expect, and I've noticed that my Expect scripts sometimes reach a point where they begin to dump commands without executing them. What causes this to happen? I've scraped Google and a couple Expect tutorials, but the keywords related…
Cryo
  • 817
  • 1
  • 7
  • 16
0
votes
1 answer

Is it possbile to let pexpect output the texts it matches?

I am familiar with expect script so I feel a bit odd when I first use pexpect. Take this simple script as an example, #!/usr/bin/expect set timeout 10 spawn npm login expect "Username:" send "qiulang2000\r" expect "Password:" send "xxxxx\r" expect…
Qiulang
  • 10,295
  • 11
  • 80
  • 129
0
votes
0 answers

issue with expect exception handling in foreach loop

I have a expect script that fires commands on remote devices, some of the devices can have credentials issues for which I need to skip is the password prompt repeats after entering the password. #!/usr/bin/expect -f set x [list…
0
votes
1 answer

Expect script calls bash script with output that may not occur

I'm trying to write an expect script that calls a bash script that may or may not output a line of text: ... expect "this string appears" ... expect "this string may or may not appear" ... expect "this string appears" ... How do I code in expect to…
0
votes
1 answer

Expect script with conditions

Following script is ok, but it has to be modified with condition where i need help, or advice how to approach it, on place where i have to send expect for loadbalancing, if it is enabled, Yes, then i have to provide ip address of second node, i dont…
user14178868
0
votes
0 answers

xclip not getting stdin from pexpect, or pexpect not sending?

This import pexpect def run(cmd, stdin): child = pexpect.spawn(cmd, encoding='utf-8') child.send(stdin) child.sendeof() run('xclip -selection clipboard', 'lol') should copy string lol into my clipboard, so that I paste it around by…
caveman
  • 422
  • 3
  • 17
0
votes
1 answer

How to compile a file in "Expect"?

I am using a user interface menu which is using Expect files to display messages. In an attempt to add a menu item I had to change one of these file, however, it seems to me that the changes are not getting reflected. Do we need to compile this…
jscode
  • 121
  • 1
  • 1
  • 6
0
votes
1 answer

Using bash variable with expect

My script look like this , but not getting the password out. #!/usr/bin/bash export -p curdir=`basename ${PWD%/*/*/}` /usr/bin/expect < expect -re "Enter passphrase.+:" send…
0
votes
1 answer

How to use the output of the shell command in expect script's send

I have a expect script written to perform a login operation. The login requires the 2fa code, this code can be obtained by executing the shell command 2fa my_login. Now how do I execute the 2fa command and pass it's output to the send of my…
Santhosh Tpixler
  • 361
  • 4
  • 12