0

I need to launch a Perl script on server B from server A with a PHP WebPage through SSH.

My command looks like:

$cmd_string="ssh user@serverB 'perl path/to/script.pl param1 param2'";

I tried both inside PHP script but nothing happen:

exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $cmd_string, $outputfile, $pidfile));
exec($cmd_string, $output);

Running this command through terminal works just well.

Thanks for your help

4
  • So what error / return value do you get? Commented Jun 4, 2015 at 10:41
  • Are you able to login into the server with php? Commented Jun 4, 2015 at 12:47
  • SSH connection is automatized for user "user" to server B. But in error.log I get: Could not create directory '/var/www/.ssh'. Host key verification failed. It looks as if process is ran by 'www-data' instead of 'user'?? Commented Jun 5, 2015 at 13:15
  • Don't you mean && just before the echo? One & would throw the command into the background and run the echo independently: I guess it would report if it could launch the first program, but not the first program's status. Commented Jun 9, 2015 at 12:47

1 Answer 1

1

My recommendation - use phpseclib, a PHP SSH implementation:

<?php
include('Net/SSH2.php');

$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
    exit('Login Failed');
}

echo $ssh->exec('perl path/to/script.pl param1 param2');
?>

exec() is often disabled on hosts for security reasons.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.