2014-12-17 4 views
-1

ssh2를 사용하여 원격 서버에서 mysql 쿼리를 만듭니다. 여기 내 라인입니다 : "Ressource을 식별 # 5"그렇지 않은 이유를 모르겠어요 : 난 내 명령을 실행하면ssh2_exec에서 mysql을 사용할 때 "Ressource id # 5"가 반환되었습니다.

$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"하고 $ 스트림 변수가 기록을 가지고 작업. 나는 적절히 ssh 명령을 사용하여 bash 명령을 테스트했다.

ssh [email protected]_address "command" 

그리고 그것은 효과가있다. 나는 당신의 도움을 기다리고있어, 난 당신이 phpseclib, a pure PHP SSH implementation 더 운이 있다고 생각 Cordialement,

+0

RTFM : [ssh_exec()] (http://php.net/ssh_exec)는 스트림을 반환합니다. 그런 다음 스트림 ([stream_get_contents()] (http://php.net/stream_get_contents)에서 읽어야 원격 명령의 출력이 무엇인지 알 수 있습니다. –

답변

2

, 감사합니다. 예.

<?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'");