1

My perl scripts should be able to change the common account to root account during execution and then continue to the rest commands. But I don't know how to do:

  1. Change to root account and able to automatically pass authentication, I used following code, but failed.

    system "echo \"$password\" | sudo -S su root";
    
  2. Able to continue to do the rest scriptsI found when using scripts to change account (simply try system "sudo su";) , it is not able to execute rest scripts

  3. I need to export proxy, but when i using the following code, it does not work:

    system "export http_proxy=http://proxy.companyname.com:8080"; 
    
Eva Cheung
  • 119
  • 1
  • 4
  • 14
  • Possible duplicate of [Run a linux system command as a superuser, using a python script](http://stackoverflow.com/questions/583216/run-a-linux-system-command-as-a-superuser-using-a-python-script) or [how can I run a perl script which needs root?](https://stackoverflow.com/questions/16178015/how-can-i-run-a-perl-script-which-needs-root?rq=1) – code_dredd Feb 27 '17 at 08:04
  • Thanks, but a bit different. I should change the account to root and export proxy by using perl scripts and cannot change the profile/config files or using “sudo command” instead. – Eva Cheung Feb 27 '17 at 08:13
  • Recent versions of `sudo` can be made to read the password from `stdin` by passing then the flag `-S`. Read the [man page](https://www.sudo.ws/man/1.8.18/sudo.man.html) for the details! – salva Feb 27 '17 at 20:41

2 Answers2

1

If you have to execute other scripts to be executed with root and you have perl ssh module installed then you can open a ssh session to the localhost with root user and start executing your scripts from there.

my $ssh = Net::SSH::Perl->new("host1");
$ssh->login("root", "pass1");
$ssh->cmd("sh /opt/...");
rakesh
  • 4,368
  • 1
  • 19
  • 13
0

perhaps try backticks?

my $sudo = `echo \"$password\" | sudo -S su root`;

also similar for the export.

my $exp = `export http_proxy=http://proxy.companyname.com:8080`;
  • Thanks, but I think the function of system ""; is nearly same as the backticks? I actually don't want to get the output of the commands above, I just hope these two commands can be executed. – Eva Cheung Feb 28 '17 at 02:20