Solved

Evaluating a SSH2 stream using PHP

Posted on 2010-08-25
4
633 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

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 …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

792 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