Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Executing unix command from script

by Neminath (Initiate)
on Jul 08, 2008 at 16:34 UTC ( #696253=perlquestion: print w/ replies, xml ) Need Help??
Neminath has asked for the wisdom of the Perl Monks concerning the following question:

I have trouble in executing command. The command requires root privileges. on my ubuntu box i need to use sudo command which ask for a password which makes my script interactive; which i don't want. Is there any way to avoid it. Any idea something like if i invoke my script with root privileges those privileges are inherited to commands in the script which avoids prompting of password each time. Ubuntu does not allow to login as root. so privileged commands has to be prefixed with sudo.

Comment on Executing unix command from script
Re: Executing unix command from script
by pc88mxer (Vicar) on Jul 08, 2008 at 16:41 UTC
    You can configure sudo to not ask for a password. See the NOPASSWD option in man sudoers.
Re: Executing unix command from script
by Tanktalus (Canon) on Jul 08, 2008 at 16:47 UTC

    Welcome to unix. A bit of convolution, but still far simpler than the Windows security model, IMO. (Though still not perfect, but I digress.)

    First off, just because you can't log in as root doesn't mean you can't log in as root. ;-) It just means you can't log in via gdm (the GUI) as root - a good thing by any security-minded definition. You should be able to gain root access in any number of other ways, some requiring a password, others have the password as optional.

    What I've done in the past is set up sudo to allow running of a particular command without a password. In my case, I want to run it as a particular user, so I have:

    %build ALL=(nobody) NOPASSWD: /full/path/to/script
    With this, anyone can run "sudo -u nobody /full/path/to/script" and not be asked for a password. Well, that's great, but a bit cumbersome. So my script does this:
    # Are we the right user? use User::pwent; my $user_wanted = 'nobody'; my $user = getpwnam($user_wanted); if ($< != $user->uid()) { exec(qw(sudo -u), $user_wanted, $0, @ARGV); } [...]
    Now I can just run /full/path/to/script, and it will exec itself as the right user if it isn't already.

    Hope that helps.

Re: Executing unix command from script
by taffer (Novice) on Jul 08, 2008 at 16:49 UTC
    Another possible solution is to create a shell script or an expect script that could do the sudo cmd and supply the password to it, then call this script from perl. It is a bit of an ugly solution, but should work.
Re: Executing unix command from script
by moritz (Cardinal) on Jul 08, 2008 at 17:04 UTC
    You can configure sudo so that it doesn't ask for a password when executing particular commands. Note that is a huge security hole if done wrong.

    See man 5 sudoers for a description of the configuration file syntax.

Re: Executing unix command from script
by philipbailey (Chaplain) on Jul 08, 2008 at 20:47 UTC
    Another way, not mentioned so far, is to set the "setuid" bit on the executable file. This assumes the file is owned by root. Something like:
    chmod u+s script.pl
    As for any method giving ordinary users elevated privileges, there are security issues. You may need to check that Perl has been compiled with the "ENABLE_SUIDPERL" option. There may be other complications--but this is something else for you to consider.
Re: Executing unix command from script
by sgifford (Prior) on Jul 09, 2008 at 02:29 UTC
    Any idea something like if i invoke my script with root privileges those privileges are inherited to commands in the script which avoids prompting of password each time

    That's actually exactly what happens: if your script runs as root, anything it runs will also run as root. If you run your main script under sudo, it will run as root, so none of the commands it runs should need sudo to run.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (10)
As of 2014-08-28 11:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (259 votes), past polls