bash and expect interaction - bash

#!/bin/bash
# Bash commands go here
/usr/bin/expect - << EndMark
This is the expect script
expect commands go here
and here
until:
EndMark
# More bash commands go here.
exit 0
Something like this will allow me to start an expect script from within bash. However when it gets to the More bash commands part, it will stop the expect process (and the processes it spawned) Is it possible to get back into the same expect script?
eg - Start expect script
- At a certain point leave the expect to bash operations
- return to the same expect script?

It is not necessary to leave the expect process in order to issue shell commands - you can use the system command from within the expect script.

Related

Write ActiveState Tcl Program for Windows that expects a password and enters it

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?

How to get the command output in variable?

I executed the below code as .sh file and getting the result as expected. Now I need to store the output of the "send" command and do things further.
I have done the below bash code and now I am keeping the output in a file:
#!/usr/bin/expect -f
spawn iroot
expect ".* password for"
sleep 3
send "password\r"
sleep 5
send "dmidecode -t system | grep Manufacturer > /tmp/manfacdetails.txt\r"
send "exit\r"
interact
How can I do that?
It seems that you are using iroot to obtain root access to a phone and issuing a command as root.
I am assuming here that the command you already have is noninteractive, and produces the output you want if you take out the redirection to a file, without any human interaction.
Then, the simple matter of capturing its output is covered by the common FAQ How to set a variable to the output of a command in Bash?
#!/bin/bash
manufacturer=$(expect <<\____HERE)
spawn iroot
expect ".* password for"
sleep 3
send "password\r"
sleep 5
send "dmidecode -t system | grep Manufacturer\r"
send "exit\r"
interact
____HERE
if [ "$manufacturer" = "Motor Ola" ]; then
ola=1
fi
# Maybe you'll prefer case over if, though
case $manufacturer in
"Samsung" | "LG" ) korean=1 ;;
"Apple")
echo "$0: You're kidding, right?" >&2
exit 127;;
*) echo "$0: Unknown manufacturer $manufacturer" >&2
exit 1;;
esac
If this doesn't work for you then Use expect in bash script to provide password to SSH command has some variants you might want to try.
You also seem to be confused about the nature of scripts. Any executable file which has a shebang as its first line will be executable by whatever interpreter is specified there. Mine has /bin/bash so this is a shell script and more specifically a Bash script, while yours has expect as its interpreter, so it's an Expect script. You also commonly have Awk scripts and Perl scripts and Python scripts (and less commonly but not at all uncommonly scripts in many, many other languages).
As already mentioned, Expect is also a scripting language, and it is possible that you would like for yours to remain an Expect script, rather than a shell script with an embedded Expect script snippet. Perhaps then see expect: store output of a spawn command into variable
The name of the file which contains the script can be anything, but the standard recommendation is to not give it an extension -- Unix doesn't care, and human readers will be confounded if your .sh file is an Expect script (as it currently is).
Perhaps tangentially see also Difference between sh and bash as well as http://shellcheck.net/ which you can use to diagnose syntax errrors in your (shell) scripts.

How to start a shell script within "expect script"?

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

Expect exits too soon

I have the following bash script (script.sh):
#!/bin/bash
read -p "Remove? (y|n): " answer
echo "You answered '$answer'."
and I would like to drive it using expect. I have the following script (expect.exp, in the same directory):
#!/usr/bin/expect -f
set timeout -1
spawn -noecho ./script.sh
expect "^Remove"
send "y\r"
but it doesn't work as expected (pun intended). The result is:
~/Playground$ ./expect.exp
Remove? (y|n): ~/Playground$
So, the expect script somehow fails on the first 'expect "^Remove"' line and exits immediately, and the rest of script.sh does not execute. What am I doing wrong here?
I have been following the basic tutorials found online (the ones with the ftp examples). I am using expect 5.45 on Kubuntu 12.10.
Edit
So it changes if I add either 'interact' or 'expect eof' at the very end. But I have no idea what happens and why. Any help?
Two things I see:
"^Remove" is a regular expression, but by default expect uses glob patterns. Try
expect -re "^Remove"
while developing your program, add exp_internal 1 to the top of the script. Then expect will show you what's happening.
Ah, I see that expect adds special meaning to ^ beyond Tcl's glob patterns.
However, because expect is not line oriented, these characters (^ and $) match the beginning and end of the data (as opposed to lines) currently in the expect matching buffer
So what you see is that you send y\r and then you expect script exits as it has nothing more to do. When your script exits, the spawned child process will be killed. Hence the need to wait for the spawned child to end first: expect eof
Problem
You are not matching any text after the shell script's prompt, so the buffer for your spawned process never gets printed. The script finishes, the spawned process closes, and that's the end of the story.
Solution
Make sure you expect a specific response or your shell script's EOF, and then print your buffer. For example:
#!/usr/bin/expect -f
spawn -noecho "./script.sh"
expect "Remove" { send "y\r" }
expect EOF { send_user $expect_out(buffer) }

Input to Expect Script

in my test file, I had this line hello$LG. So, if I do
`cat /test`
It will show in my bash shell - hello$LG
But when I try to do this in my expect script,
$> expect test.sh `cat /test`
It gives me this {hello$LG} . I don't know why it is getting {}. Anyone knows how to fix this?
Expect is based upon TCL, you should use square brackets to execute code. When trying to execute a shell command you should use either exec or open or spawn.
I am not sure what the test.sh does, but
expect [exec test.sh 'cat /test']
might just do the trick.
There's more on running other programs from Tcl here:
http://www.tcl.tk/man/tcl/tutorial/Tcl26.html

Resources