SFTP and SSH: Separate Connections Required?
I have an application using your code that does several SSH and SFTP command during processing. Can I just establish a connection, authenticate passwords and the other setup steps once and then use that connection throughout the program or do I need to perform these steps in every function? If I can do I need a separate connection for SFTP and for SSH?
You’ll need one connection for SFTP, and one connection for SSH. So you need to have one instance of an SFTP object and one instance of the SSH object. Therefore, you have two connections (one in each object).
You should be able to maintain those connections. With SSH, you may open and close logical channels. In fact, you may have any number of logical channels open simultaneously on the same connection. With SFTP, you may upload and download any number of files on the same connection.
One thing to beware of, and this usually applies to any client-server protocol, is that if the client becomes inactive for some period of time the server may decide to close the connection. This time limit is entirely up to the server and the client has no control over it. Therefore, you might have your application periodically send a “no-op” message to the server when it is inactive. With SSH, you would do it by calling SendIgnore. With SFTP you might call RealPath(“.”,””) just for the sake of generating traffic.
However, even with the “no-op” strategy, your application should still be able to handle the chance that the server may disconnect, or you may lose the connection for some external reason, at any time.