Solved

Evaluating a SSH2 stream using PHP

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now