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 ? |
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! |
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 So you will see every byte sent back and forth, and you'll see whether or not "exit 1" is sent properly. jlinkels |
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 |
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. |
And lucmove,..thanks for the cheeky link to the interesting post about "sleep".:)
I was actually inserting sleeps without really understanding its necessity ;) |
Quote:
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 Now I make a local connection: Code:
jlinkels@jlinkels_pc:~$ sh 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 Everything you send end up at the remote end. jlinkels |
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. |
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 |
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. |