Perl executing linux shell commands that are multi-line require authentication?

I look around but can't figure it out. I figured out how to execute a perl shell script execution, for example:

#!/usr/bin/perl
$cmd = "nautilus";
system $cmd;

However, I want to execute a shell script from perl that would execute something like this on the command line:

su
$password
nautilus

Thus, it will become root, and then the nautilus browser will open. When I tried this, just by executing each command separately, as shown below, this does not work. I would appreciate any advice on the right way to do this .. thanks

$cmd = "su";
system $cmd;
$cmd = $password;
system $cmd;
$cmd = "nautilus";
system $cmd;
+3
source share
3 answers

Expect. , . ( Perl)

+3

, root. sudo, , , ​​ Linux. root - , , script, , , - , sudo root. , , root.

/etc/sudoers:

myuser ALL=NOPASSWD: /usr/bin/nautilus

perl script:

system("sudo /usr/bin/nautilus");

+1

, . , Cfreak, Expect :

# cat foo.pl
  #!/usr/bin/perl
  if( open SHELL, "| /bin/bash" )
  {
    print SHELL <<'COMMANDS';
  echo hello
  pwd
  # do whatever
  COMMANDS
    close SHELL;
  }
  else
  {
    die "horrible error: $!";
  }
# ./foo.pl
  hello
  /mypath/whatever/
#
0

Source: https://habr.com/ru/post/1754658/


All Articles