I have a problem that I want to execute an "expect script" inside of an expect script. Is there anyway to do this? I appreciate any help.
[TL;DR]
one sample would help you understand
spawn expect ./runmysql.exp $dbEnv "select * from SalesOrder where orderNo='$orderId' \\G; \r"
Reference for more reading:
https://todzhang.com/blogs/tech/en/expect-script-is-your-secret-to-productivity
Related source:
https://github.com/CloudsDocker/MagnificentExpectScripts/blob/main/call_another_expect_script_from%20_one_expect_script.exp
Related
My script look like this , but not getting the password out.
#!/usr/bin/bash
export -p curdir=`basename ${PWD%/*/*/}`
/usr/bin/expect <<EOF
spawn scp -v -i ... <local file> <remote file containing $curdir>
expect -re "Enter passphrase.+:"
send "<password>\n"
exit;
EOF
exit
Something is wrong, as the passsword is evidently not sent. Something wrong with the expect line, the send line, or ???
When this script is executed it is asking for the password.
Thanks for any help.
I made this way too hard. ftp does everything I needed to do. And there are lots of places on the internet that explain how to use it. One can define bash variables as part of a bash script, and the password is simply part of what you define. scp is great of one copy, but ftp as part of a script is really easy.
so i've written a short expect script which logs into a APC Power Distribution Unit interface via telnet and polls the current ampage.
#!/usr/bin/expect
set ip "192.168.0.1"
set username "myusername"
set password "mypassword"
spawn "/bin/bash"
send "telnet $ip\r"
expect "*User Name*"
send "$username\r"
expect "*Password*"
send "$password\r"
expect "*APC*"
send -- "phReading all current\r"
expect "*Success*"
send "quit\r"
expect eof
The script does its job and I see the amps on screen, displayed like this:
apc>phReading all current
E000: Success
1: 7.5 A
apc>quit
What i need to do is 'export' that 7.5 figure, either to a text file or pass it to a bash script as a variable.
Any ideas on how i can do this?
Thank you!
Expect is an extension of TCL, so you have access to all of TCL's constructs.
If I were you I would have the expect script write directly to a file.
See the section "Writing a file" here: http://wiki.tcl.tk/367. It has a simple example for just that. In your case, you will want to open the file for append (a) instead of write (w).
open command documentation at: http://www.tcl.tk/man/tcl8.6/TclCmd/open.htm
Let me know how that works for you.
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)
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 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?