Can I use one single connection to do both SFTP and Run Shell Command?

+1 vote
asked Aug 1, 2013 by hardywang (240 points)
edited Aug 2, 2013

Here is my requirement, I need to sftp a file to a Unix server, then run a shell command to process that file, and then download the file processed by Unix server.

Currently I instantiate a Sftp object, do the file transfer, then instantiate a Ssh object to run the command, then use the same Sftp object to download file.

So, is it possible I can just open one connection to do all 3 tasks?

1 Answer

0 votes
answered Aug 2, 2013 by Tomas Knopp (58,890 points)
edited Aug 2, 2013
Best answer

Sharing the connection is possible if you use SshSession class and Bind methods on Sftp and Ssh objects like this:

        // establish the shared SSH connection
        var session = new Rebex.Net.SshSession();

        // authenticate
        session.Authenticate("username", "password");

        // bind an SFTP client to the SSH session
        Sftp sftp = new Sftp();

        // bind an SSH client to the SSH session
        Ssh ssh = new Ssh();
commented Aug 2, 2013 by hardywang (240 points)
edited Aug 2, 2013

Great, thanks!