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

How do I use a date in an Expect script?

I need to have access to the current date (or time) within the Expect script so that I can add it to the directories that are created within the Expect script, e.g. something similar to these needs to get done: mkdir file I can get the date…
doon
  • 2,311
  • 7
  • 31
  • 52
12
votes
3 answers

What is the equivalent of unbuffer program on Windows?

Hi according to this post, unbuffer connects to a command via a pseudo-terminal (pty), which makes the system treat it as an interactive process, therefore not using any stdout buffering. I would like to use this function on Windows. May I know what…
userpal
  • 1,483
  • 2
  • 22
  • 38
12
votes
6 answers

Can I use Expect on Windows without installing Cygwin?

Expect is a module used for spawning child applications and controlling them. I'm interested in Python and Ruby.
Geo
  • 93,257
  • 117
  • 344
  • 520
11
votes
3 answers

Expect within a Bash script

I wrote a Bash script with Expect within, to connect to a terminal server and clear lines. I am unable to figure out the error I am getting, as in I have given all the braces necessary. I also do not understanding the couldn't read file "line": no…
Pkp
  • 929
  • 6
  • 19
  • 31
11
votes
1 answer

How to disable output buffering in Process.StandardOutput

This question has been asked more than once before, but I have not found a satisfactory answer in any of those discussions. I am launching a command-line process that produces a real-time measurement to STDOUT, producing a new result approximately…
Drew Shafer
  • 4,740
  • 4
  • 30
  • 42
11
votes
2 answers

how to set an expect variable with output of shell command

I want to set a variable b in expect file,here initially i did ssh to a machine through this script,in that machine I want to do fetch a value and set the expect variable using following command: set b [exec `cat /home/a |grep "work"|awk -F '='…
Ek1234
  • 407
  • 3
  • 7
  • 17
11
votes
3 answers

SSH login with expect(1). How to exit expect and remain in SSH?

So I wanted to automate my SSH logins. The host I'm with doesn't allow key authentication on this server, so I had to be more inventive. I don't know much about shell scripting, but some research showed me the command 'expect' and some scripts using…
Koroviev
  • 327
  • 2
  • 4
  • 8
11
votes
1 answer

Difference between \n and \r in expect?

In this particular script they use \n. #!/usr/bin/expect set password [lindex $argv 0] spawn asadmin enable-secure-admin expect "admin" send "admin\n" expect "password" send "$password\n" expect eof exit Question Could \r just as well have been…
Jasmine Lognnes
  • 6,597
  • 9
  • 38
  • 58
10
votes
2 answers

How to write the expect script to install mariadb?

Environment: centos7 + mariadb5.5.64. Let me show the installation info on screen when to run mysql_secure_installation. # mysql_secure_installation NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION…
showkey
  • 482
  • 42
  • 140
  • 295
10
votes
1 answer

How to pass variables from a shell script to an expect script?

I've shell script as below: #!/bin/bash echo "Select the Gateway Server:" echo " 1. Gateway 1" echo " 2. Gateway 2" echo " 3. Gateway 3" read gatewayHost case $gatewayHost in 1) gateway="abc.com" ;; 2) gateway="pqr.com" ;; 3)…
Freephone Panwal
  • 1,547
  • 4
  • 21
  • 39
10
votes
5 answers

Expect-like tool for windows

I am searching for a tool that behaves similarly to Unix's expect tool (or at least, its main function). I want to automate command-line interactive programs with it. EDIT: I am preferring single executables or small apps without big multi…
majkinetor
  • 8,730
  • 9
  • 54
  • 72
10
votes
2 answers

Simplest way to run an Expect script from Python

I'm trying to instruct my Python installation to execute an Expect script "myexpect.sh": #!/usr/bin/expect spawn ssh usr@myip expect "password:" send "mypassword\n"; send "./mycommand1\r" send "./mycommand2\r" interact I'm on Windows so re-writing…
gortron
  • 147
  • 1
  • 3
  • 10
10
votes
2 answers

Is there an implementation of 'expect' or an expect-like library that works in python3?

I would like to use an expect-like module in python3. As far as I know, neither pexpect nor fabric work with python3. Is there any similar package I can use? (If no, does anyone know if py3 support is on any project's roadmap?) A perfectly…
belacqua
  • 543
  • 4
  • 18
9
votes
3 answers

expect script + how to ignore strings if not appears

I write the following expect script in order to automate ssh login to remote Linux machine And run the command "cat /etc/APP_VERSION.txt file" Sometime I not asked from ssh command about- "Are you sure you want to continue connecting…
user1121951
9
votes
3 answers

SSH in Java App with 'expect' like functionality

With 'expect', one can execute SSH commands and parse the output of those commands to alter program flow. I'd like to do this with Java. That is, I want my Java app to launch a SSH session, execute a command on the remote server, and depend on the…
jtnire
  • 1,348
  • 5
  • 21
  • 32