+1 vote
by (240 points)
edited

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
by (58.9k points)
edited
 
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();
        session.Connect("server");

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

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

        // bind an SSH client to the SSH session
        Ssh ssh = new Ssh();
        ssh.Bind(session);
by (240 points)
edited

Great, thanks!

...