LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   In a perl script, how to test if user has sudo execute w/o password ? (http://www.linuxquestions.org/questions/linux-newbie-8/in-a-perl-script-how-to-test-if-user-has-sudo-execute-w-o-password-679735/)

etomato 10-29-2008 06:07 AM

In a perl script, how to test if user has sudo execute w/o password ?
 
In a Perl script I execute an command with sudo.

I need to capture the command's output to an array and must execute the command with sudo without entering a password.

How to test if sudo is setup correctly before executing the command?
Or how to redirect if prompted for a password so I can parse the output?

my $cmd = 'sudo /usr/sbin/sanpath-command -l';
@cmdStdout = `$cmd`;

Is there something I can do via the shell within my Perl script?

Thanks.

chrism01 10-29-2008 07:45 PM

If you don't know in advance if you need a passwd, all you can do is run it and see.
You can capture the output into a scalar (not array) and parse that.
You can also look at these 2 modules that enable fine-grained control:

http://perldoc.perl.org/IPC/Open2.html
http://perldoc.perl.org/IPC/Open3.html

etomato 11-02-2008 02:16 PM

Quote:

Originally Posted by chrism01 (Post 3325597)
If you don't know in advance if you need a passwd, all you can do is run it and see.
You can capture the output into a scalar (not array) and parse that.
You can also look at these 2 modules that enable fine-grained control:

Not what I am looking for.

WITH A SCRIPT I need to initially and continuously test that my script can execute a command with sudo and without having to enter a password.

And without having to install yet another Perl module.

The script will run on AIX, Solaris and Vmware ESX Linux.

Quakeboy02 11-02-2008 02:55 PM

What is it that you're trying to do that you can't know up front that you have permission to do it?

chrism01 11-02-2008 08:05 PM

Those are Core modules (ie part of the std install): http://perldoc.perl.org/5.8.8/index-modules-I.html

etomato 11-03-2008 05:35 AM

Quote:

Originally Posted by Quakeboy02 (Post 3329366)
What is it that you're trying to do that you can't know up front that you have permission to do it?

The commands are to query SAN path and the output will be parsed.

Sudo to permit execution might have been granted.
Sudo to permit execution might have been granted but erroneously removed.

The Perl version varies and definitely not 5.8.

And I have over 700 servers to deploy this on. (aix, linux, solaris)

Again, I need an automated method to test that sudo without password is working.

Any ideas?

Stone53 11-19-2008 10:18 PM

why not this? havent tried it but this would be my first shot.

my $cmd = 'sudo /usr/sbin/sanpath-command -l';
$cmdStdout = `$cmd`;
#check the output if its asking for the password
if ($cmdStdout !~m/passw/i)
{
print "no password required\n";
}
else
{
print "password required\n"
}


All times are GMT -5. The time now is 05:54 AM.