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

/bin/expect $expect_out multiple regex matches

I'm using the following expect(1) script to receive a list of users on a Teamspeak server using the Telnet Server Query. #!/usr/bin/expect # Spawn TELNET spawn telnet example.com 10000 expect "TS3" # Login send "login $USER $PASS\r" # Set…
0stone0
  • 34,288
  • 4
  • 39
  • 64
0
votes
0 answers

Calling a script from another script doesn´t execute all the commands when SSH

I am trying to call a script from another script because I am using "spawn" (from expect packet) to execute an SSH to another machine. Basically, after executing a comparison and routine in my main script (scriptA.sh) it calls for another script…
X T
  • 445
  • 6
  • 22
0
votes
0 answers

How to use interactive inputs using expect module in ansible

when executes command on remote host we have to give inputs like below . when we give any thing wrong it has to ask second time. This the response on Remote host when executed…
Hari
  • 15
  • 8
0
votes
1 answer

Expect/Send Issue

Currently, I am working on a script to automatize a process, in this point my script is short and simple but I have had some Issues with expect/send. code: #!/usr/bin/expect -f #!/bin/bash set ip *** set ip2 *** set user *** set usr2 *** set OTP…
0
votes
1 answer

Passing variables from sh to expect not working with docker-compose

The Dockerfile contains: ENV VAR 1 COPY ./setup.exp /tmp/ RUN chmod a+x /tmp/setup.exp The expect file: #!/usr/bin/expect set timeout -1 spawn setup -v expect "Enter variable: " send -- "$env(VAR)\r" The shell file…
0
votes
0 answers

Script to run show commands on Networking devices - macOS

I'm working on a Bash/expect script to run show commands on multiple networking devices. To identify the devices there are 2 acceptable options: IP Address Hostname -> The IP for this will be grepped from an inventory file. My…
vikdean
  • 23
  • 1
  • 5
0
votes
1 answer

How to pass parameters to a (.sh) script that has Spawn command?

I have the following little script: #!/usr/bin/expect set timeout 90 set ip [lindex $argv 0] set username [lindex $argv 1] set password [lindex $argv 2] set commands "sudo du -sh /var/tmp" set regexp ".*" set failonmatch "false" if {[string trim…
Mikael
  • 171
  • 2
  • 12
0
votes
1 answer

How to assign multi commands to a spawn in UNIX?

How can I execute all these commands in 1 spawn? These are the commands: sudo du -ckhx /opt/ | sort -rh | head -10 sudo du -ckhx /usr/ | sort -rh | head -10 sudo du -ckhx /var/ | sort -rh | head -10 This is the spawn command: spawn ssh -o…
Mikael
  • 171
  • 2
  • 12
0
votes
0 answers

Expect script inside bash and resize of terminal

I'm trying to get terminal resize working inside SSH session, that is spawned by expect, which is spawned by bash. #!/bin/bash ... SSH_PASS=XXXXX SSH_ADDR=YYYYY expect <(cat << EOD trap { set rows [stty rows] set cols [stty…
0
votes
2 answers

Automatic restart of ip-phones via ssh and cron (sshpass, expect or other solutions)

The task is to remotely reboot ip-phones every night. sshpass: sshpass -p 12345678 ssh -o StrictHostKeyChecking=no admin@192.168.2.1 reboot In response: Connection to 192.168.2.1 closed by remote host. In addition, the phone, as usual in CLI, asks…
KoKoKo
  • 1
  • 1
0
votes
1 answer

Expect script: Create method to login to a remote server using ssh and send commands after method has returned

I've been trying to write an expect script which has one method to open an ssh connection and this method can be called by other methods so that they can send additional commands for execution. What I already have is - #!/usr/bin/expect set timeout…
yabhishek
  • 408
  • 4
  • 14
0
votes
1 answer

Duplicated response using expect that doesn't occur when run manually

I've been trying, with a little help to create an expect script to query a service via telnet. Sometime the response is received with two system prompts concatenated together in the expect debug response [2]. This doesn't display when done…
MattB
  • 135
  • 1
  • 3
  • 10
0
votes
0 answers

No responise to expect send after successful match and send

I'm trying to write an expect script, the prompt I am expecting to handle is ade 123456789: / >. The number 123456789 will change, so I though I could just match on the only > I'm expecting to receive. I can't seem to get it to work…
MattB
  • 135
  • 1
  • 3
  • 10
0
votes
1 answer

for loop expect results differents between interactive and script

At this link I've read the for loop management for except https://tcl.tk/man/tcl8.6/TclCmd/for.htm If you call expect and paste the code in in you get the correct behavoir : @gigi:~$ expect expect1.1> for {set x 0} {$x<10} {incr x} { puts "x is…
user2239318
  • 2,578
  • 7
  • 28
  • 50
0
votes
0 answers

Catching the query results executed in the remote PostGreSQL server through expect command inside Python script

I want to catch the PostGres query results that gets executed in the remote server (passwordless-ssh) via expect command that is used to supply the password for the given PostGres user. Actually, there are two versions of our application, the older…
Ibrahim Quraish
  • 3,889
  • 2
  • 31
  • 39