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


In reply to Re: perl and sudo basic question by Old_Gray_Bear
in thread perl and sudo basic question by yelekeri

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.