Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

perl and sudo basic question

by yelekeri (Novice)
on Jul 09, 2007 at 22:09 UTC ( #625721=perlquestion: print w/ replies, xml ) Need Help??
yelekeri has asked for the wisdom of the Perl Monks concerning the following question:

Monks, I just installed sudo on the unix machine where I can successfully use the "su" command and become some other user. Now when I run this script, it is not giving me any meaningful information, always returns the information as described below. Any thoughts on this :
use Sudo; my $password = '*****'; my $su = Sudo->new( { sudo => '/usr/bin/su', 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}; }
OUTPUT is always this :
HASH(0x14a4e0) STDOUT: STDERR: return:

Comment on perl and sudo basic question
Select or Download Code
Re: perl and sudo basic question
by dynamo (Chaplain) on Jul 09, 2007 at 22:41 UTC
    1 - What output do you expect?
    2 - Can you tell if it is calling the script at all? (for example, add a line in the script saying
    echo "is working" > is_it_working
    to find out.)
    3 - if you run the perl program already as root (or whoever), does it work then? Sudo should not be required in that case.

    - Dynamo
Re: perl and sudo basic question
by almut (Canon) on Jul 09, 2007 at 23:28 UTC
    sudo => '/usr/bin/su',

    Shouldn't this point to the sudo binary, rather than to su?   (Note that those are two different things...)

Re: perl and sudo basic question
by Argel (Prior) on Jul 09, 2007 at 23:49 UTC
    Based on the output $su->sudo_run is returning a Hash reference and you cannot print the contents of the hash the way you are trying. For debuggin purposes try adding the following:

    After use Sudo:

    use Data::Dumper;

    And then replace your line 15 (print $result) with the following:

    print Data::Dumper->Dump([$result],['result']);

    That should show you what is in the hash.

Re: perl and sudo basic question
by Old_Gray_Bear (Bishop) on Jul 10, 2007 at 00:16 UTC
    From the Fine Documetnation:
    use Sudo; my $su; $su = Sudo->new( { sudo => '/usr/bin/sudo', sudo_args => '...', + username => $name, password => $pass, program => '/path/to/binary', program_args => '...', # and for remote execution ... [hostname => 'remote_hostname',] [username => 'remote_username'] } ); $result = $su->sudo_run();
    And
    sudo_run The sudo_run function first checks the attributes to make sure the + minimum required set exists, and then attempts to execute sudo witho +ut shell interpolation. You will need to take this into account in ca +se you get confusing failure modes. You may set the debug attribute t +o 1, 2, or 3 to get progressively more information. The object will return a hash. The hash will have state informatio +n within it. If the C'error' key exists, an error occured and you can + parse the value to see what the error was. If the run was successful +, C'stdout' key exists, and its value corresponds to stdout output fr +om the program run by sudo. Similarly the C'stderr' key will exist fo +r a successful run, and the value corresponds to stderr output from t +he program run by sudo. The C"rc" key will also be defined with the p +rograms return code.
    To summarize, the new() method needs the path to the sudo command rather than the su, and sudo_run() returns a hash that you will have to dig into.

    I think what you want instead of your print "$result \n"; is

    foreach my $key (sort keys %result) { print("$key => $result{$key} \n"); }
    (Note: Coded, not tested -- I have to run ....

    ----
    I Go Back to Sleep, Now.

    OGB

      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) =>
        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 }
        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!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (14)
As of 2014-10-24 13:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (131 votes), past polls