in reply to Re: perl and sudo basic question
in thread perl and sudo basic question

So I have updated the code as per the comments from all of you. Also in the test.sh script, there is only one line which has "echo hello", thats it. Here is the updated info.
use Sudo; use Data::Dumper; my $password = '*****'; my $su = Sudo->new( { sudo => '/usr/SYSADM/bin/sudo', username => "*****" , password => $password, program => "/tmp/test.sh" , program_args => '' } ); $result = $su->sudo_run(); print "$result \n"; if (exists($result->{error}) ) { &handle_error($result); } else { printf "STDOUT: %s\n",$result->{stdout}; printf "STDERR: %s\n",$result->{stderr}; printf "return: %s\n",$result->{rc}; print Data::Dumper->Dump([$result],['result']); foreach my $key (sort keys %$result) { print("$key => $result->{$key} \n"); } }
OUTPUT is :
HASH(0x14a4e0) STDOUT: STDERR: return: $result = { 'HASH(0x1499a0)' => undef }; HASH(0x1499a0) =>

Replies are listed 'Best First'.
Re^3: perl and sudo basic question
by yelekeri (Novice) on Jul 10, 2007 at 03:41 UTC
    well, I found something interesting here when this script is ran with -W option :
    <%23>: perl -W sud.pl Reference found where even-sized list expected at /apps/Perl/lib/site_ +perl/5.8.8/Sudo.pm line 85.
    Sudo.pm seems to be fine and I never got any error messages during this package installation. Line #85 looks something like this:
    83 if ($_sudo_stat_[5] != 0 ) 84 { 85 %ret = { 86 'error' => (sprintf 'Error: the sudo bin +ary "%s" is not set to group id = 0',$sudo) 87 }; 88 return \%ret; 89 }

      Just take the warning you get literaly: You are assigning a reference to a hash to a hash-variable - either replace the braces with parenthesis or declare ret as a scalar ($ret instead of %ret) -- don't forget to modify the return statement as well in the latter case.

      regards,
      tomte


      An intellectual is someone whose mind watches itself.
      -- Albert Camus

Re^3: perl and sudo basic question
by Argel (Prior) on Jul 10, 2007 at 06:36 UTC
    On line 27 %$result should be %{ $result }.

    Update: Dang, johngg is right. I didn't know %$ dereferenced a hash -- I've always done it the %{$href} way. Thanks for the heads up and sorry for any confusion!

      On line 27 %$result should be %{ $result }.

      Why?

      $ perl -le ' > $h = {a => 1, b => 2}; > print for sort keys %$h;' a b $

      Cheers,

      JohnGG

        I don't know how it happened, but the Sudo.pm is wrong, this code is wrong :
        %ret = { 'error' => (sprintf 'Error: the sudo binary "%s" is not set t +o group id = 0',$sudo) }; return \%ret;
        So I changed manually the code to return the correct info like this:
        $ret = { 'error' => (sprintf 'Error: the sudo binary "%s" is not set t +o group id = 0',$sudo) }; return $ret;
        Now it is giving me the correct error information. But the actual script seems to be not running. Here is the test.sh file info :
        echo hello echo something >> /tmp/testfile
        I haven't seen hello in the display OR the file /tmp/testfile has not been created. The "result" code is 256, what does it mean.