Read and flush output continuously

Get help with using the PHP Secure Communications Library.

Moderator: Nuxius

Forum rules
The purpose of this forum is to provide support for phpseclib, a pure PHP SSH / SFTP / RSA library.

Posts by new users are held in a moderation queue and are not publicly visible until the post is approved.

Read and flush output continuously

Postby nbben » Tue Jul 10, 2012 3:06 am

Hi,

I have a server that continually outputs text immediately after logging in via SSH (it doesn't accept any commands), and I'm trying to figure out how to capture that output so I can store the data in my own log files. The read() method is capturing the data and holding it until it matches some text, but I would like for it to just dump the data to my file as it's received. Is that possible with this library? Something along these lines:

Code: Select all
$fh = fopen('log.txt', 'a+b');
while ($line = $ssh->read()) {
   fwrite($fh, $line);
   // clear the read buffer here if needed, since we already have the data and don't want to consume any more memory
}


Any ideas? Thanks!
nbben
Traveler
 
Posts: 2
Joined: Tue Jul 10, 2012 2:30 am

Re: Read and flush output continuously

Postby TerraFrost » Tue Jul 10, 2012 9:45 am

There's the Net_SSH2::setTimeout() function (which you could do in conjunction with ->read()) but that'll kill the connection after the timeout limit has been reached.

What if I added two new functions: Net_SSH2::disableTimeoutDisconnect() and Net_SSH2::enableTimeoutDisconnect()?

The default behavior would be to kill the connection but if disableTimeoutDisconnect() was called it'd just return all the output collected between the latest call and the call before that one.
TerraFrost
Legendary Guard
 
Posts: 12218
Joined: Wed Dec 04, 2002 6:37 am

Re: Read and flush output continuously

Postby nbben » Tue Jul 10, 2012 11:06 pm

TerraFrost wrote:There's the Net_SSH2::setTimeout() function (which you could do in conjunction with ->read()) but that'll kill the connection after the timeout limit has been reached.

What if I added two new functions: Net_SSH2::disableTimeoutDisconnect() and Net_SSH2::enableTimeoutDisconnect()?

The default behavior would be to kill the connection but if disableTimeoutDisconnect() was called it'd just return all the output collected between the latest call and the call before that one.


That seems like it would work. Would it still have to reach the timeout limit before a read() would return anything (but it just won't disconnect)? For example:

Code: Select all
Net_SSH2::disableTimeoutDisconnect();
Net_SSH2::setTimeout(5);
while (1) {
     sleep (1);
     echo Net_SSH2::read(); // will this only echo something every 5 seconds, or will it echo something every second (since we're sleeping on each loop)?
}
nbben
Traveler
 
Posts: 2
Joined: Tue Jul 10, 2012 2:30 am

Re: Read and flush output continuously

Postby TerraFrost » Wed Jul 11, 2012 5:10 pm

I'd do it more like this:

Code: Select all
Net_SSH2::disableTimeoutDisconnect();
while (1) {
     Net_SSH2::setTimeout(5);
     echo Net_SSH2::read();
}

Regarding your "will this only echo something every 5 seconds, or will it echo something every second (since we're sleeping on each loop)?"... it'd echo stuff out every 6 seconds actually.
TerraFrost
Legendary Guard
 
Posts: 12218
Joined: Wed Dec 04, 2002 6:37 am


Return to phpseclib support

Who is online

Users browsing this forum: No registered users and 0 guests