Hello,
Is anyone familiar with using php's ssh2 commands?:
http://us3.php.net/manual/en/ref.ssh2.php
For some reason Im having issues returning output, only on certain commands, and it seems sporadic for some and constantly stable for others.
Here is the code:
// log in at server1.example.com on port 22
if(!($con = ssh2_connect("xxx.xxx.xxx.xxx", 22))){
echo "fail: unable to establish connection\n";
} else {
// try to authenticate with username root, password secretpassword
if(!ssh2_auth_password($con, "user", "password")) {
echo "fail: unable to authenticate\n";
} else {
// allright, we're in!
echo "okay: logged in...</br></br>";
// execute a command
if(!($stream = ssh2_exec($con, "find / -name php.ini")) ){
echo "fail: unable to execute command\n";
} else{
// collect returning data from command
stream_set_blocking( $stream, true );
$data = "";
while( $buf = fread($stream,4096) ){
$data .= $buf;
}
fclose($stream);
}
}
}
echo "php.ini location: " . $data;
--this one always works, it returns the output of the find command like this:
okay: logged in...
php.ini location: /etc/php.ini
-----------------------------------------------------------------------------------------------------------------
sometimes i can do:
if(!($stream = ssh2_exec($con, "/etc/init.d/mysqld status")) ){
or:
if(!($stream = ssh2_exec($con, "/etc/init.d/httpd status")) ){
-but they tend to return output on and off, sometimes it does other times it doesnt
-------------------------------------------------------------------------------------------------------------------
i have NEVER been able to do something as simple as:
if(!($stream = ssh2_exec($con, "pwd")) ){
or:
if(!($stream = ssh2_exec($con, "which pwd")) ){
I can never 'cat' anything either, though I know cat is working because i can do this:
if(!($stream = ssh2_exec($con, "cat /etc/php.ini >> haha.txt")) ){
sure enough on the remote server there is haha.txt with php.ini in it..
so this is why I think its simply the output not work, i believe I can do any command, once i got it to output the results of an apache restart, that doesnt output anything now..
i thought maybe it was the size of what i being returned, so i jacked this up:
while( $buf = fread($stream,4096) ){
i made the read length much much larger, but outputting just 'pwd' from /root/ shouldnt be an issue, so I sort of rulled that out
sorry this is long winded, any advice? or tests?