SSH.NET icon indicating copy to clipboard operation
SSH.NET copied to clipboard

SCPClient and Port Forwarding (Re-opened)

Open boukenka opened this issue 9 years ago • 4 comments

I have a question for the usage of SCP. Is it possible to define a Forwarded Local Port? I have tried via the coding. But I couldn't succeed. Is it possible with the SCP Client and the SSH.NET library? Or is there another way to use SCP with port forwarding? Or with SCP, it is not possible (and in that case my question has no meaning)?

Could you provide to me a bit of coding how to implement it please (I have reopened the request because it was closed without getting a coding example)

boukenka avatar Dec 21 '16 18:12 boukenka

Is it possible to get an update, a bit of coding as example please? Any comment?

boukenka avatar Dec 28 '16 17:12 boukenka

Good evening. Any possible update please?

boukenka avatar Jan 16 '17 20:01 boukenka

@boukenka Suppose you have the following scenario:

  • Server B is not accessible for you (behind firewall), but you need to download some files from it
  • Server A is the only accessible jump server from your workstation, so you can create a tunnel and download the file (if Server B is accessible from Server A).

Basic example:

// Connect to Server A
using (SshClient ServerA_SshClient = new SshClient(ServerA, 22, username, password))
{
          ServerA_SshClient.Connect();

          // Initialize Local Port Forwarding
          var port = new ForwardedPortLocal("127.0.0.1", 40000, ServerB, 22);
          ServerA_SshClient.AddForwardedPort(port);

          port.Exception += delegate (object sender, ExceptionEventArgs e)
          {
                  Console.WriteLine(e.Exception.ToString());
          };

          // Start Local Port Forwarding
          port.Start();

          // Download the file from Server B via Server A using Local Port Forwarding
          using (ScpClient ServerB_ScpClient = new ScpClient("127.0.0.1", 40000, username, password))
          {
                ServerB_ScpClient.Downloading += new EventHandler<ScpDownloadEventArgs>(ScpDownloading);
                ServerB_ScpClient.Connect();
                ServerB_ScpClient.Download("/home/john/scp_test_file.txt", new FileInfo(@"c:\Users\john\Downloads\scp_test_file.txt"));
                ServerB_ScpClient.Disconnect();
          }

          // Stop Local Port Forwarding and Disconect
          port.Stop();
          ServerA_SshClient.RemoveForwardedPort(port);
          ServerA_SshClient.Disconnect();
}
static void ScpDownloading(object sender, ScpDownloadEventArgs e)
{               
        // Checking download status here...    
}

Let me know if you need more help.

kasbst avatar Apr 27 '17 20:04 kasbst

Thank you very much for the answer. That's exactly what I needed as an example

boukenka avatar Apr 27 '17 23:04 boukenka