-1

I am using ssh2 to make a mysql query on a remote server. Here are my lines:

$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;

When i launch my command, i have the message "OK" and the $stream variable writes : "Ressource id #5" I don't know why it doesn't work. I tested the command in bash, with properly ssh command :

ssh user@ip_address "command"

And it worked. I'm waiting your help, Thank you, Cordialement,

stupigou
  • 21
  • 3
  • RTFM: [ssh_exec()](http://php.net/ssh_exec) returns a stream. you then need to READ from that stream ([stream_get_contents()](http://php.net/stream_get_contents)) to see what the output of your remote command was. – Marc B Dec 18 '14 at 19:06

1 Answers1

2

I think you might have more luck with phpseclib, a pure PHP SSH implementation. eg.

<?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'");
albanykia
  • 21
  • 1