LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   problem receiving output from Expect script (https://www.linuxquestions.org/questions/linux-software-2/problem-receiving-output-from-expect-script-638708/)

slinx 04-29-2008 12:56 PM

problem receiving output from Expect script
 
Hello,

I'm trying to use an expect script to ssh to a host, execute a command, and return the output of the command to a variable. However, the variable gets filled with the output from the spawn directive:

for instance, this bash code calls expect:

Code:

chkselinux=`./sshlogin.exp ${myuser} ${mypass} ${myhost}"cat /selinux/enforce"`
The Expect script in sshlogin.exp consists of:

Code:

spawn -noecho ssh $user@$ipaddr $scriptname $arg1 $arg2 $arg3
match_max 100000
expect {
 "*yes/no*"
 { send -- "yes\r" }
 "*?assword:*"
 { send -- "$password\r" }
}

So the result I expect in "chkselinux" is either "1" (if selinux is on), or something else (usually "cat /selinux/enforce: file not found"). However, what actually goes in the variable is all the output of the spawn directive, plus the output of the ssh command embedded in the middle.

Of course, if I can set up host keys, I can just run an ssh command from the bash script, but that is not an option in this environment.

Interact may work, but how do I tell the bash script to end the expect script?

Thanks!

jlinkels 04-29-2008 01:59 PM

Because you embedded the call of the expect script in ``, all the output of the expect script goes into the variable chkselinux.

Expect echoes all output to stdout, and that is what is put as result from calling in ``

(Just a tip, use $(...) instead of `...`. It is equal, but is avoids reading mistakes)

What answer do you actually expect from your expect script (no pun intended). Should it tell you whether your login was succesful? How does that fit with the output of "cat /selinux/enforce"? Where do the variables $user, $password etc get their values from in the expect script?

I am trying to help, but I might be more succesful if I understand what you try to achieve.

jlinkels

slinx 04-29-2008 03:09 PM

Thanks for your reply - the user, password, and hostname arguments are provided by the bash script that calls the expect script, and it reads them from a file. That part of the script works - it logs in as expected and runs the desired commands. I just want to capture ONLY the output from the command, not the entire login process.

I tried using a log_file argument, but that also only captures the login process and not the single command I want to run.

Basically, I want to be able to do the same thing as ssh, assuming one has installed one's id_dsa.pub key in the server's .ssh/authorized_keys file:

Code:

output_var=$(ssh user@host "some_command")
But do it with an expect session.

BTW I know about the $() construct, but the script was written by my boss, and I don't want to throw him off :)

jlinkels 04-29-2008 04:47 PM

I see exactly what you mean.

it shouild be soooo simple to surpress the Expect output and seems to be impossible.

Maybe you could write a proc for the expect part in tcl, catch the output in a tcl var, process it to see if the expected string is in the result, and pass that result back to bash. Something like:

Code:

proc do_spawn {
  spawn $command $arg1 $arg2
  expect ...
  blah blah
  send exit
}

set spawn_result [do_spawn]
if {[string first cannot $spawn_result] > 0} {
  puts "command failed"
} else {
  puts "command succesful
}


Sorry, but I am out of ideas for the moment.

Quote:

Originally Posted by slinx
BTW I know about the $() construct, but the script was written by my boss, and I don't want to throw him off

Tell your boss I said so :)

jlinkels

slinx 04-30-2008 08:02 AM

Thanks for the code suggestion, I may try that out - I did end up re-writing the code to just use ssh commands with installed authorized_keys, though with the number of servers we have, I need to use expect to copy the keys up the the various servers first.


All times are GMT -5. The time now is 11:07 PM.