Cannot use echo command if i import except in script - shell

I want to use expect method in my script so i used this command in script
#!/usr/bin/expect
now I'm unable to declare any array or even use echo commands after using this command
Full Code
#!/usr/bin/expect
declare -a arr=("ip1" "ip2")
for i in "${arr[#]}"
do
spawn ssh myhost#i
send "exit\n"
done
The entire array declaration and for loop throws error like invalid command name , i'm not able to use echo command . when i try the echo command without importing except everything works as expected .

The first line declare that your script is an expect script and expect script uses Tcl scripting language. What you did was mixing bash and Tcl in the same script, which is why the error message. Setting up an list in Tcl and loop over is easy:
#!/usr/bin/expect
set arr {ip1 ip2}
foreach i $arr {
spawn ssh myhost#$i
send "exit\r"
}

Related

Passing variable to Expect and Spawn

I'm writing a script that will scp a tar file from my local server to a remote host. Since the script generates the file through a pre-requisite process, the name is generated dynamically. My script needs to take the name of the file and pass it to scp for transfer.
#!/usr/bin/expect -f
spawn scp test.$(date +%y%m%d_%H%M).tar user#IP-ADDRESS:/destination/folder
set pass "password"
expect "password: "
send -- "$pass\r"
expect eof
I've tried setting the filename as a variable but keep seeing the same error:
can't read "(date +%y%m%d_%H%M)": no such variable
while executing "spawn scp test.$(date +%y%m%d_%H%M).tar user#IP-ADDRESS:/destination/folder"
$(date +%y%m%d_%H%M) is not a Tcl command. If you use expect, you have to learn Tcl. To get a formatted date in Tcl, use the clock command. Also, interpolation of the result from a command in Tcl is not done by $(....), but by [....]. You can find examples for this construct here.
Decided to go another route since the team was able to provision a new Artifactory repo for this binary and alike. However, to the advice provided here I was able to make a few discoveries which I used to fix my issues:
I also had a password with $ symbol and that also caused a world of issues.
#!/bin/bash
TEST=$(date +%y%m%d_%H%M)
/usr/bin/expect <<eof
set password {pas\$word}
spawn scp "$TEST" user#IP-ADDRESS:/destination/folder
expect "*password:"
send "$pasword\r"
expect eof

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.

bash: Interactive scripting with expect_out not working

I am trying to run a script interactively with expect & send in bash.
The main script prints following to console
The available files are...
1 File_001.bin
2 File_002.bin
3 File_003.bin
and I want to find index of a specific file say File_002.bin, i am writing interactive script like below
#!/usr/bin/expect -f
spawn mainScript.sh;
expect -re "^.*File_002.bin.*$";
set filename $expect_out(0,string);
send_user "The filename is: $filename\n";
But its not matching anything! Any idea what's the problem?

Need modification in an ssh script that's written in expect

Am working on a script to ssh into list of servers using expect tool. Getting below error while running it
./script
#!/usr/local/bin/expect -f
while /usr/bin/read hostname
do
spawn ssh user#$hostname
expect "user#$hostname's password"
send "resuidt\n"
expect "user#$hostname"
interact
done < srvlist
Below is my error:
missing operand at _#_
in expression "_#_/usr/bin/read"
(parsing expression "/usr/bin/read")
invoked from within
"while /usr/bin/read hostname"
(file "./script" line 3)
Need help to fix this error.
You are writing an Expect program, which is basically a Tcl program. Your while loop is not Tcl syntax, but looks like a (Posix/Ksh/Bash/Zsh)-shell script.
You have to make up your mind: Write everything in Tcl, or split your application into two files: One (in shell script) as "main program", and a separate expect script, which will be called by the shell script.
As user1934428 indicates you are using bash-type while loop syntax.
Below is one example of how to make an expect script perform the actions you want.
#!/usr/local/bin/expect -f
set file hostname
set user myusername
set passwd mypassword
set f [open $file]
foreach target [split [read $f] "\n"] {
spawn ssh $user#$target
expect {
timeout {send_user "Expect Timeout\n" ; exit}
"password:"
}
send "$passwd\r"
expect {
timeout {send_user "Expect Timeout\n" ; exit}
"$user#$target"
}
interact
}
close $f
I included timeouts in the expect sections because I've found if you do not add these safety mechanisms the expect script can proceed even without the proper responses.
if you want to use shell variables directly into the expect script then you have to pass those variables as $env(shell_variable_name) inside the expect script
example:spawn ssh $env(myusername)#$env(hostname)

How to return a value from child expect script to parent sh script

I have a expect script inside a shell script. My problem is I am unable to get a variable value from the child expect script to the shell parent script.
Please find my code below:
#!/bin/sh
expect <<- DONE
spawn telnet myemailserver.com imap
expect "* OK The Microsoft Exchange IMAP4 service is ready."
send "a1 LOGIN myuser mypass\r"
expect "a1 OK LOGIN completed."
send "a2 EXAMINE INBOX\r"
expect "a2 OK EXAMINE completed."
send "a3 SEARCH UNSEEN\r"
expect "a3 OK SEARCH completed."
set results $expect_out(buffer)
set list [split $results "\n"]
send "a4 LOGOUT\r"
expect "Connection closed by foreign host."
spawn echo $list
expect eof
DONE
echo $list
exit 0
I found out that the variable list at the last line is empty. Is there a way to pass the value from variable $list to the shell parent script?
Your here-document is subject to shell variable expansion before the script is given to the expect interpreter. The $list variable is substituted with nothing (assuming you don't already have a shell variable named list in your program). You need to ensure the here-doc is single quoted (shown below)
Just like working with awk or sed, the shell inter-process communication is performed through passing data along the standard IO channels: the shell script has to capture the output of the expect program:
list=$( expect <<'END'
log_user 0
# expect program here
puts $list
END
)
echo $list
Since I'm suppressing normal terminal output of spawned programs with log_user 0 in order to send only the crucial information back to the shell, you have to replace spawn echo with expect's puts command.

Resources