LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux > Linux - Newbie
User Name
Password
Linux - Newbie This forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Tags used in this thread
Popular LQ Tags , , , , , , ,

Reply
 
Thread Tools
Old 10-29-2008, 07:07 AM   #1
etomato
LQ Newbie
 
Registered: Oct 2008
Posts: 3
Thanked: 0
In a perl script, how to test if user has sudo execute w/o password ?


[Log in to get rid of this advertisement]
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.
etomato is offline  
Tag This Post ,
Reply With Quote
Old 10-29-2008, 08:45 PM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 5.4
Posts: 7,429
Thanked: 325
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
chrism01 is offline     Reply With Quote
Old 11-02-2008, 03:16 PM   #3
etomato
LQ Newbie
 
Registered: Oct 2008
Posts: 3
Thanked: 0

Original Poster
Question

Quote:
Originally Posted by chrism01 View Post
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.
etomato is offline  
Tag This Post , , , , , ,
Reply With Quote
Old 11-02-2008, 03:55 PM   #4
Quakeboy02
Senior Member
 
Registered: Nov 2006
Distribution: Debian Lenny 2.6.28.8 SMP
Posts: 2,720
Thanked: 57
What is it that you're trying to do that you can't know up front that you have permission to do it?
Quakeboy02 is online now     Reply With Quote
Old 11-02-2008, 09:05 PM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 5.4
Posts: 7,429
Thanked: 325
Those are Core modules (ie part of the std install): http://perldoc.perl.org/5.8.8/index-modules-I.html
chrism01 is offline     Reply With Quote
Old 11-03-2008, 06:35 AM   #6
etomato
LQ Newbie
 
Registered: Oct 2008
Posts: 3
Thanked: 0

Original Poster
Quote:
Originally Posted by Quakeboy02 View Post
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?

Last edited by etomato; 11-03-2008 at 06:36 AM..
etomato is offline     Reply With Quote
Old 11-19-2008, 11:18 PM   #7
Stone53
LQ Newbie
 
Registered: Nov 2008
Posts: 1
Thanked: 0
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"
}
Stone53 is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash - how to test, if a script is exececuted with sudo ? almatic Programming 6 10-19-2008 04:36 PM
How do I use sudo in a script without asking for password? Usalabs Linux - General 1 08-28-2008 02:59 PM
Shell script sudo without password prompt under Darwin? lhouk Other *NIX 3 02-08-2008 12:16 PM
Running a script as root with sudo without entering the user password kloss Linux - General 8 10-10-2005 12:39 PM
Why cant i execute a perl script dude4you Linux - Newbie 2 03-02-2004 06:50 PM


All times are GMT -5. The time now is 01:21 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration