Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 08-08-2009, 05:16 AM SSH via PHP
Skilled Talker

Posts: 55
Name: Brian
Trades: 0
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?
bmp99 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Reply     « Reply to SSH via PHP
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.45345 seconds with 12 queries