如何通过ssh在PHP中执行远程命令?

6

我正在尝试通过ssh从php脚本执行远程命令,并且我希望来自命令的输出(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

如何通过ssh在PHP中执行远程命令?


1
看起来你还没有安装pecl ssh2包。 - Jeff Busby
我尝试安装SSH2,@ http://www.php.net/manual/en/ssh2.installation.phpsudo pecl install ssh2 channel://pecl.php.net/ssh2-v 以下是输出: 无法在首选状态“stable”中下载pecl/ssh2,最新版本为0.11.2,稳定性为“beta”,请使用“channel://pecl.php.net/ssh2-0.11.2”进行安装 parsePackageName():“version”既不是有效的版本也不是“channel://pecl.php.net/ssh2-version”中的有效状态 无效的软件包名称/软件包文件“channel://pecl.php.net/ssh2-version” 安装失败 如果我从源代码尝试,但无法弄清如何编译config.m4 - kamal
5个回答

6
如果由于繁琐的程序而无法添加php包,这里有一个简单的类可以解决问题。
class ExecuteRemote
{
    private static $host;
    private static $username;
    private static $password;
    private static $error;
    private static $output;

    public static function setup($host, $username=NULL, $password=NULL)
    {
        self::$host = $host;
        self::$username = $username;
        self::$password = $password;
    }

    public static function executeScriptSSH($script)
    {
        // Setup connection string
        $connectionString = self::$host;
        $connectionString = (empty(self::$username) ? $connectionString : self::$username.'@'.$connectionString);

        // Execute script
        $cmd = "ssh $connectionString $script 2>&1";
        self::$output['command'] = $cmd;
        exec($cmd, self::$output, self::$error);

        if (self::$error) {
            throw new Exception ("\nError sshing: ".print_r(self::$output, true));
        }

        return self::$output;
    }
}

你是在说如果这不是一个红旗,他可以添加一个PHP包。你在谈论什么样的PHP包?一些第三方库吗?有什么建议吗? - Klaus
我想我当时是在提到ssh2 pecl包。然而,自从我回答这个问题将近5年以来,Composer已经成为php的一个有用的工具。请查看getcomposer.org获取您所需的所有软件包。 - Parris Varney

3

我尝试了,但安装失败了。sudo pecl install ssh2 channel://pecl.php.net/ssh2-version [sudo] tester的密码: 无法在首选状态“stable”中下载pecl/ssh2,最新版本是0.11.2,稳定性为“beta”,请使用“channel://pecl.php.net/ssh2-0.11.2”进行安装。 parsePackageName():“version”不是“channel://pecl.php.net/ssh2-version”中的有效版本或有效状态。 无效的软件包名称/软件包文件“channel://pecl.php.net/ssh2-version” 安装失败。 - kamal

2

2

通过SSH密钥管理身份验证非常简单:

$cmd = 'ssh user@host script ' . $arguments . ' 2>/dev/null';
$result = shell_exec($cmd);

PHP 5.5



网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接