Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Incorrect value 0 reported in command execution return value variable $?, when trying with expect->send command

by Davewhite (Acolyte)
on Feb 25, 2012 at 13:39 UTC ( [id://956108]=perlquestion: print w/replies, xml ) Need Help??

Davewhite has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am trying to remotely run an executable on a remote machine using the expect.pm module. I am able to successfully connect and execute the executable on remote machine.

Able to successfully get the executable output but facing issue when trying to check the return value of executable run

I am trying in following manner

 $exp->send("$command ; echo COMMAND_RET:$? | sed 's/^/COMMAND_OUT: /g'; echo -n END_; echo EXPECT\n");

Following format output gets displayed on console

<Command output>

COMMAND_OUT: COMMAND_RET: 0

END_EXPECT

My problem is that $? value printed in above code always displays 0, while the executable is returning non zero value too for some cases

While trying to manually execute the same command on shell prompt, correct value of $? gets appended COMMAND_OUT: COMMAND_RET: string.

Not able to detect why is S? value with expect->send is showing 0 value always.

Any advice on the same is really grateful

  • Comment on Incorrect value 0 reported in command execution return value variable $?, when trying with expect->send command
  • Download Code

Replies are listed 'Best First'.
Re: Incorrect value 0 reported in command execution return value variable $?, when trying with expect->send command
by Perlbotics (Archbishop) on Feb 25, 2012 at 14:20 UTC

    Insert this line and the solution will reveal before your weary eyes...

    print "$command ; echo COMMAND_RET:$? | sed 's/^/COMMAND_OUT: /g'; ech +o -n END_; echo EXPECT\n";
    Hint: You need to escape $?.

    Why not get rid of the sed-invocation?

    $exp->send("$command ; echo 'COMMAND_OUT: COMMAND_RET:'\$?; echo END_E +XPECT\n");
    Depending on the shells flavour of echo, you could also get rid of the second echo invocation.

      Thanks a lot for the guidance. It's working as per your suggestion.

Re: Incorrect value 0 reported in command execution return value variable $?, when trying with expect->send command
by Corion (Patriarch) on Feb 25, 2012 at 13:49 UTC

    I don't know much about Expect, but what you are calling as command is really calling a shell executable and passing it that command. I venture that whatever shell you are using is not passing the exit code upwards, or, as a shell, is actually running OK and thus exits with exit code 0. Also see the documentation of Expect on ->exitstatus maybe.

      The same piece of code is working fine and returning correct return when executed on the same shell manually

      I was thinking that all the collective input to expect->send command also executes on the some shell, so it should contain the command execution status value rather than the shell exit value, as the "echo COMMAND_RET:$?" gets executed before the shell exit.

        I'm sorry! On rereading your original post, I realize you are using double quotes:

        $exp->send("$command ; echo COMMAND_RET:$? | sed 's/^/COMMAND_OUT: /g +'; echo -n END_; echo EXPECT\n");

        So your command is not actually what you send over the wire. Use

        my $cmd = "$command ; echo COMMAND_RET:$? | sed 's/^/COMMAND_OUT: /g'; + echo -n END_; echo EXPECT\n"; warn "Sending command [$cmd]"; $exp->send($cmd);

        for debugging such issues. Personally, when dealing with interpolating data into strings, I prefer sprintf over plain interpolation:

        my $cmd = sprintf q{%s; echo COMMAND_RET:$? | sed 's/^/COMMAND_OUT: /g +'; echo -n END_; echo EXPECT\n}, $command; warn "Sending command [$cmd]"; $exp->send($cmd);
Re: Incorrect value 0 reported in command execution return value variable $?, when trying with expect->send command
by JavaFan (Canon) on Feb 25, 2012 at 15:29 UTC
    You are printing the value of *Perls* $?, and that will be 0. If you want the shell's, $?, you must make sure Perl doesn't get its grubby hands on it. Use single quotes, a backslash, or write it as "\x24?".
Re: Incorrect value 0 reported in command execution return value variable $?, when trying with expect->send command
by salva (Canon) on Feb 25, 2012 at 23:37 UTC
    Hi, I am trying to remotely run an executable on a remote machine using the expect.pm module.

    Why don't you use one of the SSH modules available from CPAN instead? (For instance Net::OpenSSH or Net::SSH2).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://956108]
Approved by Perlbotics
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-25 22:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found