I have a make target and on running it expects a user input. I want to automate the process with expect but when I spawn "make abc" it keeps telling that Couldn't execute "make abc". No such file or directory.
My expect expect script is:
#!/usr/bin/expect
spawn "make abc"
expect "*[input] File name:*"
send "../regression/regression_lehs.ion"
Any help appreciated!
spawn wants to see the program and any
arguments as separate words, so
spawn make abc
not
spawn "make abc"
With the quotes, you are trying to run a program named "make abc" (with the space)
Related
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 sampleScript.sh
expect "Password:"
send "pass123"
sampleScript.sh code:
echo 'Hello, world.' >foo.txt
my program outputs the following:
'The operation completed successfully. while executing "spawn sampleScript.sh"
(file "compare.tcl" line 6)'
However, there is no foo.txt that is created in my local file folder where the scripts are. Can you help?
The key with expect programs is to let the spawned program exit gracefully. As it currently stands, after your expect script sends the password, it immediately exits, and that kills the spawned program too early.
If you don't need to interact with the sampleScript (i.e. just let it run to completion), the last line in the expect script should be
expect eof
Otherwise, use
interact
Read How to create a Minimal, Reproducible Example -- your updated code does not reproduce the error you're seeing
Tcl code:
when you send something, you usually need to "hit Enter": send "password\r"
Did you add expect eof to the Tcl script? If not, you might be killing sampleScript.sh before it has a chance to create the output file
sampleScript.sh: Is that really your sample script? Where's the password prompt?
In this expect script there will be no ssh server connected, I just want to execute a ".sh" file locally, is that possible?
For instance:
#!/bin/expect
command "xxx.sh" # a command which starts a certain shell script
Also, is it possible to execute a expect script within a expect script?
For instance:
#!/bin/expect
command "xxx.exp" # a command which starts a certain expect script
Any help?
If you need to interact with this script, use spawn xxx.sh
If you just need to run it and capture the output, use set output [exec xxx.sh]
Expect is an extension of the Tcl language, so you would be well served to go through the Tcl tutorial.
The command in Expect to run a shell command is spawn.
#!/bin/expect
spawn command arg1 arg2 ...
command can be any program -- a binary executable, a shell script, another expect script, etc. So you can do:
spawn xxx.sh
or:
spawn xxx.exp
I'm trying to use expect to talk to a bash script, but I'm missing something. My script is:
#!/bin/bash
echo -n "foo? "
read
echo $REPLY
and my expect script is:
#!/usr/bin/expect
spawn ./script.sh
expect "foo? "
send "bar\r\n"
But I never see bar when I run the expect script. What am I missing here?
Dumb me, I needed to add interact to my expect script so that it can finish "interacting" with the script:
#!/usr/bin/expect
spawn ./script.sh
expect "foo? "
send "bar\r\n"
interact
I found the answer here two minutes after asking this question.
I don't familiar well with expect syntax, but you worth try autoexpect:
autoexpect ./script.sh
It will run script.sh and after you'll finish running it an expect script script.exp will be created in current directory.
After than you can edit it if you need to tune something in it.
I have a command line program and I'm trying to automate interaction with that program using Expect. The program is launch by the running the command "ant runconsole" on the build file.
I also embedded the Expect script in a bash script. Currently my code looks like:
#!/bin/bash
cd $HOME/myProj/build
/usr/bin/expect - << EndMark
spawn ant runconsole
EndMark
exit 0
Build this just prints spawn ant runconsole to terminal. And putting "ant runconsole" in quotes returns "couldn't execute "ant runconsole": no such file or directory while executing "spawn "ant runconsole""
This is my first time using Expect so I'm sure this is a really noobish question, but I couldn't find the solution online. I'd really appreciate any help.
What's happening there is that you are creating an Expect session with Ant, but not interacting with it, so it terminates.
You could modify your script slightly thus:
/usr/bin/expect - << EndMark
spawn ant -v runconsole
expect
EndMark
Run that, and you should see some output from Ant returned. For information on how to set up your Expect script, and the interaction with Ant, perhaps see the spawn wiki page or Expect man page.
I am writing a script to run ssh so as to login a remote host, after all the operation is done, I type exit and log off. But I want the script to continue running and write log on the local host. The script is something like:
#!/usr/bin/expect
spwan ssh qwerty#remote_host
expect {
"password:" {
send "123123\r"
}
}
interact;
send "echo $(date) >> login_history.log\r"
But the last command "send ..." always failed with the error message like
"send: spawn id exp4 not open ..."
When I log off from the remote host, can the expect script continue to work as it is running on the local host?
YES, processing can continue after an [interact].
Short answer: change the last {send ...} to {exec date >> login_history.log}
There are several concepts you'll want to understand to achieve the control flow you're after. First, http://www.cotse.com/dlf/man/expect/interact_cmd_desc.htm provides a succinct synopsis and example of intermediate [interact] use.
Second: why did you see the message "... spawn id ... not open ..."? Because the spawn id is not open. The script you wrote said, in effect, "interact, then, after interact is over, send a new command to the ssh process." If you've already logged out, then, of course that id for a defunct process is no longer available.
Third: how do you achieve what you want? I'm unsure what you want. It sounds as though it would be enough for you simply to transform the [send] as I've described above. How does that look to you?