Solved

Evaluating a SSH2 stream using PHP

Posted on 2010-08-25
4
632 Views
Last Modified: 2012-05-10
Hi Experts,

I am  trying to evaluate the result of a SSH2 stream so I can  branch to another section of my code based on the output.  The connection to the server is working fine and I get the output but the evaluation does not work.

The section of code that does not work is:

if ($data == 'pierre' ) {
   echo "Welcome";
}

Can you please have a look?

Thanks

Pierre-Alex

P.S. I am running

phpinfo()
PHP Version => 5.2.9
-------------- My code ---------------

// Set the command to execute

$command = 'whoami';

// Connect 
if ($ssh = ssh2_connect('192.168.108.201', 22))  {

	//Authenticate
	if(ssh2_auth_password($ssh, 'pierre' ,  'password')) { 

		//exec a command and return a stream
		$stream = ssh2_exec($ssh, $command);

		// force PHP to wait for the output
		stream_set_blocking($stream, true);

		// read the output into a variable
		$data = '';
		while($buffer = fread($stream, 4096)) 
		    $data .= $buffer;
	}

// close the stream
fclose($stream);



// print the response
echo $data;

//evaluate the result
if ($data == 'pierre' ) {
   echo "Welcome";
}

}

 echo "End";

--------- What I get is  ----------------

pierre
End

--------- What I should get is  ----------------

pierre
Welcome
End

Open in new window

0
Comment
Question by:pierre-alex
  • 2
  • 2
4 Comments
 
LVL 2

Accepted Solution

by:
dfendig earned 500 total points
ID: 33518863
$data probably contains the EOF character, otherwise you wouldnt get the carriage return on the two echo statements.
Try $data = substr($data,0,-1); to remove the possible EOF char.
0
 
LVL 2

Expert Comment

by:dfendig
ID: 33518911
or try this
while (!feof($stream)) {
  $data .= fread($stream, 4096);
}
0
 

Author Closing Comment

by:pierre-alex
ID: 33518956
Thanks. That worked !

Is there a way to print the EOF character  (for troubleshooting purposes)
0
 

Author Comment

by:pierre-alex
ID: 33518974
dfendig

Just so you know I got a "time out" message with the other option :

or try this
while (!feof($stream)) {
  $data .= fread($stream, 4096);
}

Rgds

Pierre
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP installation issues 11 59
How to init a part of Javascript based on PHP value for Datatables & Yadcf 8 43
ignore other .htaccess 2 44
PHP AJAX JSON 2 58
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question