Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: perl and sudo basic question

by Old_Gray_Bear (Bishop)
on Jul 10, 2007 at 00:16 UTC ( #625740=note: print w/replies, xml ) Need Help??


in reply to perl and sudo basic question

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

Replies are listed 'Best First'.
Re^2: perl and sudo basic question
by yelekeri (Novice) on Jul 10, 2007 at 02:46 UTC
    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 }

        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

      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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2021-06-24 16:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (130 votes). Check out past polls.

    Notices?