Как я могу выполнять удаленные команды в PHP через ssh?

Я пытаюсь выполнять удаленные команды из php-скрипта через ssh, и я хочу, чтобы выходные данные команд (stdout и stderr) передавались на исходный хост.

Я знаю, что в Perl и Ruby это возможно. Мне не удалось найти таких примеров в php.

Код:

$ip = 'kssotest.yakabod.net';
$user = 'tester';
$pass = 'kmoon77';

$connection = ssh2_connect($ip);
ssh2_auth_password($connection,$user,$pass);
$shell = ssh2_shell($connection,"bash");

$cmd = "echo '[start]';your commands here;echo '[end]'";
$output = user_exec($shell,$cmd);

fclose($shell);

function user_exec($shell,$cmd) {
  fwrite($shell,$cmd . "\n");
  $output = "";
  $start = false;
  $start_time = time();
  $max_time = 2; //time in seconds
  while(((time()-$start_time) < $max_time)) {
    $line = fgets($shell);
    if(!strstr($line,$cmd)) {
      if(preg_match('/\[start\]/',$line)) {
        $start = true;
      }elseif(preg_match('/\[end\]/',$line)) {
        return $output;
      }elseif($start){
        $output[] = $line;
      }
    }
  }
}

Но когда я выполняю его вот так $ php remote.php , я получаю сообщение об ошибке:

PHP Fatal error:  Call to undefined function ssh2_connect() 
in /home/tester/PHP_SSH2/remote.php on line 6

Каков наилучший способ выполнять удаленные команды в PHP через ssh?

5
задан Eric Leschinski 18 April 2013 в 13:13
поделиться