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
9
votes
6 answers

How can I encrypt or hide passwords in a Perl script?

I am working on Perl script that uses Expect to login via telnet to remote machines (don't ask, gotta use telnet). I also do perforce p4 login operations as necessary and use expect to pipe in the correct passwords. For now I just read passwords…
Ville M
  • 2,009
  • 7
  • 30
  • 45
9
votes
1 answer

How to use expect inside bash script

Could anybody please tell me why this is not working? #!/bin/bash cd /home touch somefile /usr/bin/expect<
Hasan Rumman
  • 577
  • 1
  • 6
  • 16
9
votes
2 answers

expect, interact and then again expect

There are several posts regarding the same, but i still not able to make my expect script work properly. My intention is to automate everything but leave the password enter for the user. So there are 3 parts of the script: automated login give the…
Petras L
  • 183
  • 1
  • 1
  • 7
9
votes
5 answers

expect utility is not working when executing from jenkins

we have a unix script which uses expect utility for interactive execution. This script works well when we run from unix server. If we run this script from Jenkins, it is not working. Below is the script var="xxxxx" expect -c " spawn sudo cp…
Kishore Tamire
  • 2,054
  • 5
  • 23
  • 25
9
votes
2 answers

expect script to ssh returns invalid command name

I am trying to write an expect script which would ssh into a server, send sudo su, then check the iptables status and put the output in a log file on the server. Below is the script. 1 #!/usr/bin/expect 2 exp_internal 1 3 log_user 0 4 set…
9
votes
2 answers

how to implement expect "interact" command using java

I want to implement the expect "interact" command using java. In expect, it's possible to open an ssh session, authenticate and, then, use the "interact" command to give the control back to the user. Is that possible with java? I've tried with…
user1073494
9
votes
4 answers

How to use bash/expect to check if an SSH login works

My team manages many servers, and company policy dictates that the passwords on these servers must be changed every couple of weeks. Sometimes, our official database of passwords gets out of date for whatever reason (people forget to update it,…
Daniel Kats
  • 5,141
  • 15
  • 65
  • 102
9
votes
2 answers

Perl Rover v3 pass environment variable to in the Rulesets

I am using perl Rover module version 3 to login to the Linux/Unix server and run the script. In the ruleset if I add the full path name it copies the script to the remote server, not able to substitute the environment variable. eg. This works: …
sfgroups
  • 18,151
  • 28
  • 132
  • 204
9
votes
1 answer

How to use Expect in a Bash script

I am trying to write a script that pulls the latest version of my software from a Git repository and updates the configuration files. When pulling from the repository though, I have to enter a password. I want the script to automate everything, so I…
LordZardeck
  • 7,953
  • 19
  • 62
  • 119
8
votes
2 answers

TCL expect regular expression

I am trying to write one script which climbs up from one system to another through TCL/Expect. It is working for me. I need a regular expression in which expect "$ " and expect "# " is combined , so that any system with any prompt in the path…
Harikrishnan R
  • 1,444
  • 4
  • 16
  • 27
8
votes
3 answers

How do I tell expect that I have finished the interactive mode?

I am writing some expect commands in bash. Script: #!/bin/bash set timeout -1 expect -c " spawn telnet $IP $PORT1 sleep 1 send \"\r\" send \"\r\" expect Prompt1> interact timeout 20 { sleep 1 } expect { Prompt2> {send…
Pkp
  • 929
  • 6
  • 19
  • 31
8
votes
1 answer

Expect Script - Fixing weird terminal resizing behaviour

I wrote a small expect script to connect to multiple SSH Servers. However if I interact with the terminal after initializing the connection, the terminal window behaves very odd. For example, if I start the script in a non-fullsize terminal, and…
NullDev
  • 6,739
  • 4
  • 30
  • 54
8
votes
2 answers

Tcl + Check file existence

I'm trying to check if a file exists or not in Tcl, but I can't seem to get a true result. Even though I know it is present. while {true} { if { [file exists $file_name] == 1} { exp_send "copy file.txt destination \r" …
c0d3rs
  • 107
  • 1
  • 2
  • 8
8
votes
1 answer

Embedding an Expect script inside a Bash script

I have the following script: #!/bin/bash if [ `hostname` = 'EXAMPLE' ] then /usr/bin/expect << EOD spawn scp -rp host:~/outfiles/ /home/USERNAME/outfiles/ expect "id_rsa':" send "PASSWORD\r" interact spawn scp -rp host:~/errfiles/…
Marses
  • 1,464
  • 3
  • 23
  • 40
8
votes
4 answers

Other solutions/languages that are superior to the TCL-based Expect?

I am amazed by how Expect (TCL) can automate a lot of things I normally could not do. I thought I could dig deeper into Expect by reading a book, but before I do that I want to ask if there are other solutions/languages that could do what Expect…
never_had_a_name
  • 90,630
  • 105
  • 267
  • 383