Solved

Evaluating a SSH2 stream using PHP

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

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.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

688 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