Add command to visudo with dynamic parameters

241 Views Asked by At

I've writen a perl script that needs sudo rights, but it accepts parameters that are always different. eg

sudo /home/user/script.pl user

I guess adding this to visudo will not work. Is this possible? If not I was thinking about maybe using something like a pipe to solve this problemm like:

echo user | sudo /home/user/script.pl

Is there a way to pass variables this way to a perl script?

The only other option I see is writing the parameters to a file. Then I can still add the script to visudo.

1

There are 1 best solutions below

0
On

Why not use ARGV? content of tmp/script.pl

use strict;
use warnings;

print join(',', @ARGV);

exit;

and as result

$ sudo perl tmp/script.pl foo bar
> foo,bar

Or you can use Getopt::Long (extended processing of command line options)