LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   return value of expect script .. (https://www.linuxquestions.org/questions/linux-software-2/return-value-of-expect-script-738979/)

shriyer 07-09-2009 02:31 PM

return value of expect script ..
 
Hey guys,..
I am trying to return a variable's value from an expect script to the command line or a calling script..
$res has a value of 1.

I do a ..
expect "*"
sleep 1
send "exit `echo \$res`\r"
interact

But on command line.. the return value of expect script is 0.

tried a ..
expect "*"
sleep 1
send "exit \$res\r"
interact

Doesnt seem to be working .. How do I return a variable's value from an expect script back to caller ?

lucmove 07-09-2009 03:50 PM

Hi. I don't know the answer, but you should find these two pages interesting:

http://wiki.tcl.tk/23430

http://wiki.tcl.tk/_/search?S=expect

Good luck!

jlinkels 07-09-2009 08:09 PM

Which command line? When you send "exit 1", the shell at the other end that is, the host you connected to will exit with a value of 1.

Did you do:
Code:

set exp_internal 1
in your expect code?
So you will see every byte sent back and forth, and you'll see whether or not "exit 1" is sent properly.

jlinkels

shriyer 07-10-2009 08:54 AM

Thanks for the replies,
I wanted it at the command line of the host , ...
Do u mean ... the return value will be available to the calling shell at the remote server .. where I logged in using expect ?

Umm.. Can I propagate this value back to the command line or calling script at my end ..(calling host ?)

Thanks

shriyer 07-10-2009 09:02 AM

I tried both "set exp_internal 1"

and expect "*"
send "set exp_internal 1\r"

But still the return value of the expect script at the host (calling ) server ...(my end ) .. is always 0.

I want the expect script to return a variable value to the calling host server, ie, from where the expect script was ran to login to the remote server.

Thanks a ton for the help and replies.

shriyer 07-10-2009 09:04 AM

And lucmove,..thanks for the cheeky link to the interesting post about "sleep".:)

I was actually inserting sleeps without really understanding its necessity ;)

jlinkels 07-10-2009 09:21 AM

Quote:

Originally Posted by shriyer (Post 3603350)
I want the expect script to return a variable value to the calling host server, ie, from where the expect script was ran to login to the remote server.

The concept is a bit hard to grab, I find it difficult as well.

Don't forget that Expect doesn't do anything else but emulating your keyboard input and send it to the remote. Now forget about expect a while, and try the following.

Code:

$ telnet your.remote.host
Trying 192.168.0.158...
Connected to your.remote.host
Escape character is '^]'.
Linux 2.6.18.6-VISUS-VISUS (jlinkels_pc.mydomain.com) (pts/0)
RBE_RNW_NLANT2 login: jlinkels
Password:
Last login: Fri Jul 10 14:09:47 +0000 2009 on pts/0 from jlinkels_pc.mydomain.com.
No mail.
jlinkels@RBE_RNW_NLANT2:~$ exit 3
Connection closed by foreign host.
jlinkels@jlinkels_pc:~$ echo $?
0

The parts in blue are the response from the remote host. As you can see I exit with code 3, but if I ask the exit code on my local host I get 0. That is because the remote shell terminated with 3, but my own telnet process terminated with 0

Now I make a local connection:
Code:

jlinkels@jlinkels_pc:~$ sh
sh-3.2$ exit 4
exit
jlinkels@jlinkels_pc:~$ echo $?
4
jlinkels@jlinkels_pc:~$

and you see I am able to retrieve the value for the exit code on my local host.

I do not completely understand what you try to achieve, and I might better be able to help you if you elaborate on that. If you want to see the return value from Expect in your shell, end Expect like this:
Code:

exit $res
but do not do send exit 5
Everything you send end up at the remote end.

jlinkels

shriyer 07-10-2009 10:04 AM

Thanks jlinkels for the reply,
Yeah I think I havent elaborated what my purpose is.

I login to a remote netdump server. (This server holds RAM dumps on crash)
(This login is automated by the expect script which is called by a ksh script.. for now im just trying this expect script on its own from the command line.)

I run a df /var/crash to see if the crash directory has enough space..
I store this result in a local var on the remote netdump server $var.

I pass in the current estimated dump size of the host server as an argument to the expect script ...

I compare these two .. and the result is 1 if there is enough space and 0 if not enough space. This is stored in variable $res on remote server.

On my runs, which I have made verbose.. I see there is enough space, and $res is 1.

I want to convey this back to the calling host server ,with the value of this variable $res on remote netdump server,.. so the host can verify,.. if a crash happens ... it can safely dump it over to the remote server...

Script :

#!/usr/bin/expect -f
spawn ssh -o StrictHostKeyChecking=no abcdump@defserver
expect "abcdump@defserver's password: "
send "abcdef\r"
expect ".com"
send "var=\$\(df /var/crash | grep -v Filesystem | tr -s ' ' ' ' | cut -d ' ' -f4\);var=\$\(echo \"\$var * 1000\" | bc\);echo The value of var is \$var\r"
expect "*"
send "set exp_internal 1\r"
expect "*"
#sleep 1
send "echo The value of argument is [lindex $argv 0]\r"
expect "*"
#sleep 1
send "res=\$\(echo \"\$var > [lindex $argv 0]\" | bc\)\r"
expect "*"
send "echo Res is \$res;exit \$res\r"
interact



Script Run :

./system-backup 10
spawn ssh -o StrictHostKeyChecking=no **@**
Red Hat Enterprise Linux AS release 3 (Taroon Update 9)

Unauthorized access to or use of this system is prohibited.
All access and use may be monitored and recorded.

**@**'s password:

##################################
RedHat Network Satellite Proxy

(****)
##################################

Last login: Fri Jul 10 09:59:00 2009 from ***
var=$(df /var/crash | grep -v Filesystem | tr -s ' ' ' ' | cut -d ' ' -f4);var=$(echo "$var * 1000" | bc);echo The value of var is $var
set exp_internal 1
echo The value of argument is 10
res=$(echo "$var > 10" | bc)
echo Res is $res;exit $res
-bash-2.05b$ var=$(df /var/crash | grep -v Filesystem | tr -s ' ' ' ' | cut -d ' ' -f4);var=$(echo "$var * 1000" | bc);echo The value of var is $var
The value of var is 33025440000
-bash-2.05b$ set exp_internal 1
-bash-2.05b$ echo The value of argument is 10
The value of argument is 10
-bash-2.05b$ res=$(echo "$var > 10" | bc)
-bash-2.05b$ echo Res is $res;exit $res
Res is 1
logout
Connection to *** closed.

**********

I passed in a dummy size of 10 as argument .. So the comparison returns 1. ie enough space.. I want to echo this back to the calling server /script /commandline.
So where I do a $res.. I want to echo this local variable back to the calling server.

Thanks a ton.

jlinkels 07-10-2009 10:44 AM

I see. You want to have the outcome available on the remote host from something you determine on the local host.

I think that is impossible. The exit status is available to the parent of the called process. In this case only the sshd daemon on the remote host gets the exit passed from you Expect script and I do no see any way to get a hold to that.

Are you able to create files on the remote host? Usually /tmp is open for everyone. You are able to execute commands remotely because you check some value. Can't you execute a command which does echo 1 > /tmp/candump.txt Your dump process reads the contents of the file, or even just detects existence of the file and takes action.

You can even further simplify that by execution the space test on the remote host, and in the same script write the file.

But that is all dependent whether you can create a file and you can tweak you dump process so it can read or detect a file.

I don't see any other options. Usually you would create an environment variable, but I don't think you can pass those from one process to the other.

jlinkels

shriyer 07-10-2009 04:06 PM

Thanks for the reply,..
I did finally figure it out.phew.

This is the code :

#!/usr/bin/expect -f
set timeout 2
spawn ssh -o StrictHostKeyChecking=no abcdump@def
expect "abcdump@def's password: "
send "abcdef\r"
expect ".com"
send "var=\$\(df /var/crash | grep -v Filesystem | tr -s ' ' ' ' | cut -d ' ' -f4\);var=\$\(echo \"\$var * 1000\" | bc\);echo The value of var is \$var\r"
expect "*"
send "echo The value of argument is [lindex $argv 0]\r"
expect "*"
send "res=\$\(echo \"\$var > [lindex $argv 0]\" | bc\);if \[ \$res == 1 \];then logout;fi\r"
expect {
"closed." {exit}
default {exit 1}
}
interact

So... like i said,, i wanted to return a variable value from remote server back to host calling server.

I did it indirectly by checking if the value was greater than something . logout. then expect that the connection is closed , and exit the EXPECT script with 0 code.

if expect ".closed" fails, that means connection wasnt closed, that means.. the variable i was testing for wasnt what i wanted...
and i exit the expect script with the help of the default feature..with status 1. this is available back to host command line /calling script.

Like jlinkels mentioned.. if u "send" an exit command with status.. for eg "send exit 1\r" .. the exit code will be picked up only by the calling shell at the remote server... and the expect script on HOST server will still exit with 0 always.
So we need to exit the expect script with appropriate status ;)
-Shrikant


All times are GMT -5. The time now is 01:40 AM.