?
Solved

Evaluating a SSH2 stream using PHP

Posted on 2010-08-25
4
Medium Priority
?
644 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 2

Accepted Solution

by:
dfendig earned 2000 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses

764 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