Linux – Perl, which executes multi-line linux shell commands and requires authentication?

Perl, which executes multi-line linux shell commands and requires authentication?… here is a solution to the problem.

Perl, which executes multi-line linux shell commands and requires authentication?

I’ve been looking around but can’t figure it out. I figured out how to execute perl’s shell script, for example:

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

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

su
$password
nautilus

So it becomes root, and then open the nautilus browser. When I try this by executing each command individually, as shown below, it doesn’t work. I would appreciate any suggestions on the correct way to accomplish this: Thanks

$cmd = "su";
system $cmd;
$cmd = $password;
system $cmd;
$cmd = "nautilus";
system $cmd;

Solution

Check out the Expect module. It will do what you want. (It should already be included in the last few Perl releases).

Related Problems and Solutions