LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-09-2009, 02:31 PM   #1
shriyer
LQ Newbie
 
Registered: Jul 2009
Posts: 17

Rep: Reputation: 0
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 ?
 
Old 07-09-2009, 03:50 PM   #2
lucmove
Senior Member
 
Registered: Aug 2005
Location: Brazil
Distribution: Debian
Posts: 1,432

Rep: Reputation: 110Reputation: 110
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!
 
Old 07-09-2009, 08:09 PM   #3
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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
 
Old 07-10-2009, 08:54 AM   #4
shriyer
LQ Newbie
 
Registered: Jul 2009
Posts: 17

Original Poster
Rep: Reputation: 0
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
 
Old 07-10-2009, 09:02 AM   #5
shriyer
LQ Newbie
 
Registered: Jul 2009
Posts: 17

Original Poster
Rep: Reputation: 0
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.
 
Old 07-10-2009, 09:04 AM   #6
shriyer
LQ Newbie
 
Registered: Jul 2009
Posts: 17

Original Poster
Rep: Reputation: 0
And lucmove,..thanks for the cheeky link to the interesting post about "sleep".

I was actually inserting sleeps without really understanding its necessity
 
Old 07-10-2009, 09:21 AM   #7
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Quote:
Originally Posted by shriyer View Post
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
 
Old 07-10-2009, 10:04 AM   #8
shriyer
LQ Newbie
 
Registered: Jul 2009
Posts: 17

Original Poster
Rep: Reputation: 0
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.

Last edited by shriyer; 07-10-2009 at 10:06 AM.
 
Old 07-10-2009, 10:44 AM   #9
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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
 
Old 07-10-2009, 04:06 PM   #10
shriyer
LQ Newbie
 
Registered: Jul 2009
Posts: 17

Original Poster
Rep: Reputation: 0
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Expect script: how do i send function key F12 in an expect script alix123 Programming 4 09-01-2013 09:06 PM
Expect script help shriyer Linux - Server 3 07-08-2009 08:42 PM
Expect Script coolest Programming 7 02-16-2009 03:59 PM
expect return failes Ricio Programming 14 07-22-2008 09:45 AM
Troubleshooting script that uses expect kaplan71 Linux - Software 4 02-29-2008 05:23 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 05:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration