LinuxQuestions.org
Review your favorite Linux distribution.
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 04-29-2008, 12:56 PM   #1
slinx
Member
 
Registered: Apr 2008
Location: Cleveland, Ohio
Distribution: SuSE, CentOS, Fedora, Ubuntu
Posts: 106

Rep: Reputation: 23
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!
 
Old 04-29-2008, 01:59 PM   #2
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,053

Rep: Reputation: 484Reputation: 484Reputation: 484Reputation: 484Reputation: 484
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
 
Old 04-29-2008, 03:09 PM   #3
slinx
Member
 
Registered: Apr 2008
Location: Cleveland, Ohio
Distribution: SuSE, CentOS, Fedora, Ubuntu
Posts: 106

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

Last edited by slinx; 04-29-2008 at 03:10 PM.
 
Old 04-29-2008, 04:47 PM   #4
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,053

Rep: Reputation: 484Reputation: 484Reputation: 484Reputation: 484Reputation: 484
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
 
Old 04-30-2008, 08:02 AM   #5
slinx
Member
 
Registered: Apr 2008
Location: Cleveland, Ohio
Distribution: SuSE, CentOS, Fedora, Ubuntu
Posts: 106

Original Poster
Rep: Reputation: 23
Wink

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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 coolest Programming 7 02-16-2009 03:59 PM
Troubleshooting script that uses expect kaplan71 Linux - Software 4 02-29-2008 05:23 PM
How to write output from expect to a file? johnpaulodonnell Programming 2 06-22-2007 05:07 AM
Expect script - No output to file pandersson61 Programming 4 05-23-2007 05:18 AM
CGI script problem: Send/Expect command echo on webpage rajatgarg Programming 2 07-11-2004 04:38 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration