How to sudo to another user to run command in perl


You need to run the script from User ABC and in the script you have to sudo to User DEF to run command.

Net::SSH::Perl enables you to simply and securely execute commands on remote machines, and receive the STDOUT, STDERR, and exit status of that remote command. It contains built-in support for various methods of authenticating with the server (password authentication, RSA challenge-response authentication, etc.). It completely implements the I/O buffering, packet transport, and user authentication layers of the SSH protocol, and makes use of external Perl libraries (in the Crypt:: family of modules) to handle encryption of all data sent across the insecure network


## The Script is run with user ABC 
## Here you will sudo to user DEF (login as 'DEF' user)
print "Connecting as DEF user...\n";
my $ssh=Net::SSH::Perl->new($yourhost, debug=>1, protocol=>'1,2');
## as 'DEF' user to provide 777 permission to directories
$ssh->cmd("mkdir -p /u01/def/");
$ssh->cmd("touch "/u01/def/def.txt);
## here it will return back to user ABC

Pls. note, you need to use below directive,

use Net::SSH::Perl;