.NET SSH port forwarding

最后都变了- 提交于 2019-12-18 11:12:22

问题


I am trying to build in SSH port forwarding into a .NET application that I am writing.

I have tried using sharpSSH, but it requires the user to input their password every time, and I don't want that. I am going to handle storing the password.

I have downloaded Granados, but there is basically zero documentation for it. How do I accomplish port forwarding with Granados or any other free SSH library for .NET?


回答1:


If you set up an DSA key on the SSH server remotely, you could save a key for the user (do this as a one-time thing) and then save the key on the server as an authorized user.




回答2:


The SSH.NET library is a simple way to achieve this:

using (var client = new SshClient("client.net", "user", "password"))
{
    client.Connect();

    var port = new ForwardedPortLocal("localhost", 10000, "remote.net", 80);
    client.AddForwardedPort(port);

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

    // ... hold the port open ... //

    port.Stop();
    client.Disconnect();
}



回答3:


These C# alternatives are all derived from JCraft's Java Jsch:

  1. sharpSSH (inactive since Jan 2010) / author's page / article
  2. DotNetSSH (inactive since Jun 2010)
  3. SSH.NET Library (active as of Jan 2012)
  4. Nsch (generated/updated from Jsch Feb 2012)

The Granados product page links to the Poderosa project which includes a PortForwarding plugin. The source code for the channel.cs and connectionmanager.cs files there appear to implement port forwarding. See this answer for a recommendation.

Nsch appears to be a hidden gem within MonoDevelop's NGit; it is mostly-automatically converted (background info) from Jsch.

Further research in Feb 2011 by Krzysztof Kowalczyk of Sumatra PDF.




回答4:


Although poorly documented - or at least the documentation eludes me - this seems to be able to handle SSH connections including file transfers and port forwarding: https://github.com/sshnet/SSH.NET




回答5:


Here is a method without promoting any of these parameters: (Fully-automated port forwarding) using SharpSSH

(user,host,Lport,Rhost,Rport,DSA-key-confirmation,Password)

    Dim JJ As Tamir.SharpSsh.jsch.JSch = New Tamir.SharpSsh.jsch.JSch()
    Dim sess As Tamir.SharpSsh.jsch.Session = JJ.getSession("user", "remoteadd.dydns.com")
    Dim conf As Hashtable = New Hashtable()
    conf.Add("StrictHostKeyChecking", "no")
    sess.setConfig(conf)
    sess.setPassword("password")
    sess.connect()
    sess.setPortForwardingR(45, "127.0.0.1", 256)


来源:https://stackoverflow.com/questions/2835646/net-ssh-port-forwarding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!