在ssh2_exec中使用 mysql 时返回的“资源 ID #5”


"Ressource id #5" returned when using mysql in ssh2_exec

我正在使用ssh2在远程服务器上进行mysql查询。以下是我的台词:

$connection = ssh2_connect("ip_address", 22);
if (ssh2_auth_pubkey_file($connection, "user", ".ssh/id_rsa.pub", ".ssh/id_rsa"))
{
echo("Ok");
}
else
{
die("KO");
}
$stream = ssh2_exec($connection, "mysql -u root --password=password -D db -e 'SELECT * FROM table'");
echo $stream;

当我启动命令时,我有消息"OK",$stream变量写道:"资源 id #5"我不知道为什么它不起作用。我在 bash 中使用正确的 ssh 命令测试了该命令:

ssh user@ip_address "command"

它奏效了。我在等你的帮助,谢谢亲切感,

我认为你可能会对phpseclib有更多的运气,这是一个纯粹的PHP SSH实现。

<?php
include('Net/SSH2.php');
include('Crypt/RSA.php');
$ssh = new Net_SSH2("ip_address", 22);
$key = new Crypt_RSA();
$key->loadKey(file_get_contents(".ssh/id_rsa"));
if ($ssh->login('user', $key)) {
    echo "Ok";
} else {
    die("KO");
}
echo $ssh->exec("mysql -u root --password=password -D db -e 'SELECT * FROM table'");