in reply to Running a perl script with root, changing to another user and then back to root?
Hello morthed,
Welcome to the Monastery. Well a quick and dirty solution could be to ssh as root do what ever you want exit and continue.
Well ssh might not be the best option what about the Sudo module? You can write a small script to be executed by root. Sample of code:
#!/usr/bin/perl use Sudo; use strict; use warnings; my $su; my $name = "root"; my $pass = "password"; $su = Sudo->new( { sudo => '/usr/bin/sudo', #sudo_args => '...', username => $name, password => $pass, program => '/home/tinyos/Monks/sample.pl', #program_args => '...' } ); my $result = $su->sudo_run(); if (exists($result->{error})) { printf "STDERR: %s\n",$result->{error}; } else { printf "STDOUT: %s",$result->{stdout} if $result->{stdout}; printf "STDERR: %s",$result->{stderr} if $result->{stderr}; } print $ENV{USER}."\n"; __END__ $ perl test.pl STDOUT: root tinyos
In the sample.pl script:
#!/usr/bin/perl use strict; use warnings; print $ENV{USER}."\n";
Hope this helps, BR.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Running a perl script with root, changing to another user and then back to root?
by morthed (Novice) on Feb 15, 2018 at 14:40 UTC | |
by thanos1983 (Parson) on Feb 15, 2018 at 15:08 UTC |