tags:

views:

143

answers:

5

Does anyone know a SSH/ SFTP/ FTP wrapper class around pfsockopen();?? I'm still on my quest to keep persistent connections in PHP.

A: 

I Dont know of a class implementation built around that but there is an ssh2 module if that helps you...

prodigitalson
Many thanks but my crucial thing is persistence...
Simon
+2  A: 

After a quick read, it looks like opening a socket deals with a different layer then what you are wanting. You want to connect via SSH or SFTP, which is the Application Layer using a method that makes connections via TTP/TLS/UDP, which is the transport layer.

So really what you want (I think) is to create an SSL or TLS connection using the pfsockopen() function, and then use that connection to pass data via the SSH/SFTP protocol.

According to the PHP site:

If you have compiled in OpenSSL support, you may prefix the hostname with either ssl:// or tls:// to use an SSL or TLS client connection over TCP/IP to connect to the remote host.

So my best guess is that you set your hostname to start with ssl:// and then use the SSH or SFTP port as the port (so port 22 or port 989). Something like:

$persistent_socket = pfsockopen("ssl://myhostsite", 22);

or

$persistent_socket = pfsockopen("ssl://myhostsite", 989);
Anthony
Thanks. Yeah, I realise that. I'm hoping someone somewhere knows of a PHP class that implemented the complete SSH protocol...
Simon
So you are wanting to use the persistent socket to send via SSH, right? Do the SSH functions for PHP not allow for setting a socket handler?
Anthony
I just looked, it seems that a request was made for persistent connections for SSH2 almost 2 years ago. What a drag. I'm surprised you can't just point SSH2 at the SSL socket handler.
Anthony
This seems to be a non-starter. Will use a deamon to maintain persistent connections.
Simon
I have actually been pondering your question for a week. I hope a daemon proves to be simpler than anything either of us could have come up with as a workaround.
Anthony
A: 

Here's an SSH / SFTP wrapper class around fsockopen():

http://phpseclib.sourceforge.net/

It's not pfsockopen() but maybe replacing the one fsockopen() call with pfsockopen() will do the trick?

+1  A: 

@user260294: Thank you so much for http://phpseclib.sourceforge.net/. Although unrelated to original question, it saved me hours of coding on a similar project.

@Simon: You haven't seen anyone using pfsockopen() specifically with SSH / SFTP due to the fact that it does not work for these specific protocols. The idea is that if a connection already exists you are able to reuse it with pfsockopen without resending any headers, but this is not allowed with daemons listening on port 22. You have no other choice but to reestablish the connection and resend the headers. This applies to anything that deals with SSH1/2/SFTP.

In the case of a daemon that establishes a persistent connection - when it times out you have to reconnect all over... So I don't see much of a point in doing that.

Raine
A: 

wow the package at http://phpseclib.sourceforge.net/ works perfectly for sftp connection and no need to install or add modules or anything. It just works. Thanks sooo much

Tim