vb.net 无法使用 SSH.NET 连接到 SSH 服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37603149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:54:53  来源:igfitidea点击:

Unable to connect to SSH server using SSH.NET

vb.netsshssh.net

提问by Matthew Finerty

I am currently trying to make a control panel-like program for my Linux server using Visual Basic and SSH.NET. Right now, I want to make the Linux machine reboot when I press a button.

我目前正在尝试使用 Visual Basic 和 SSH.NET 为我的 Linux 服务器制作一个类似控制面板的程序。现在,我想让 Linux 机器在按下按钮时重新启动。

Here is what I have so far:

这是我到目前为止所拥有的:

Dim connInfo As New Renci.SshNet.PasswordConnectionInfo("IP", "USERNAME", "PASSWORD")
Dim sshClient As New Renci.SshNet.SshClient(connInfo)
Dim cmd As Renci.SshNet.SshCommand

Private Sub MaterialFlatButton1_Click(sender As Object, e As EventArgs) Handles MaterialFlatButton1.Click
    Using sshClient
        sshClient.Connect()
        cmd = sshClient.RunCommand("reboot")
        sshClient.Disconnect()
    End Using
End Sub

When I press the button, the error I get is:

当我按下按钮时,我得到的错误是:

An unhandled exception of type 'Renci.SshNet.Common.SshConnectionException' occurred in Renci.SshNet.dll

Renci.SshNet.dll 中发生类型为“Renci.SshNet.Common.SshConnectionException”的未处理异常

Additional information: An established connection was aborted by the software in your host machine.

附加信息:已建立的连接被主机中的软件中止。

Is there anything I would need to change with my code?

我的代码有什么需要改变的吗?

采纳答案by Kirk Woll

I encountered this same problem myself. tl;dr: upgrade to the beta version of SSH.NET:

我自己也遇到了同样的问题。tl; dr:升级到 SSH.NET 的测试版

Install-Package SSH.NET -Pre

To Elaborate Further

进一步细化

If you examine your logs on your linux server immediately after your unsuccessful attempt to connect:

如果在尝试连接失败后立即检查 linux 服务器上的日志:

grep 'sshd' /var/log/auth.log

You'll probably see a line like this:

你可能会看到这样的一行:

sshd[2467]: fatal: mm_answer_moduli: bad parameters: 2048 2048 1024

sshd[2467]:致命:mm_answer_moduli:错误参数:2048 2048 1024

Recent versions of ssh server has become more strict, and the current (stable) version of SSH.NET does not support this. More specifically, it has become more strict by requiring a larger key size than 1024. You can see the bug report on codeplex. And here'sa person experiencing a similar problem for whom upgrading also worked.

最近版本的 ssh 服务器变得更加严格,并且当前(稳定)版本的 SSH.NET 不支持这一点。更具体地说,它通过要求比 1024 更大的密钥大小而变得更加严格。您可以在 codeplex 上查看错误报告。而这里的遇到了类似的问题对他们来说也升级工作的人。

回答by Marcos Brigante

If you are the server administrator, there is the option of limiting the Key Exchange Algorithms to diffie-hellman-group1-sha1. It is important to notice that this algorithm is not considered safe anymore, but Open SSH supports it.

如果您是服务器管理员,则可以选择将密钥交换算法限制为diffie-hellman-group1-sha1。需要注意的是,这种算法不再被认为是安全的,但 Open SSH 支持它。

To do that, go to your linux server and edit the file

为此,请转到您的 linux 服务器并编辑文件

/etc/ssh/sshd_config

/etc/ssh/sshd_config

Add the lines (somewhere in the middle of the file)

添加行(文件中间的某处)

# Disable all Kex Algorithms but the one defined below (so Renci SSH.NET 2013 works)
KexAlgorithms diffie-hellman-group1-sha1

Restart ssh:

重启ssh:

sudo service ssh stop
sudo service ssh start

Now SSH.NET will work, but to connect to the server via ssh (terminal), you should define the protocol on you client config file:

现在 SSH.NET 可以工作了,但是要通过 ssh(终端)连接到服务器,您应该在客户端配置文件中定义协议:

Edit the file (I'm using ssh on cygwin, other clients are beyond my knowledge)

编辑文件(我在 cygwin 上使用 ssh,我不知道其他客户端)

~/.ssh/config

~/.ssh/config

Add the line

添加行

KexAlgorithms +diffie-hellman-group1-sha1

Just to conclude, I ended up not using this solution because the latest filezilla does not connects with the server, and I have a user using it to upload files.

总结一下,我最终没有使用这个解决方案,因为最新的 filezilla 没有连接到服务器,而且我有一个用户使用它来上传文件。

回答by user1424311

The RunCommand method expects a response from the connected device. Running the reboot, poweroff, or shutdown commands will not allow for a response from the device and causes an SshConnectionException to occur.

RunCommand 方法需要来自连接设备的响应。运行reboot、poweroff 或shutdown 命令将不允许来自设备的响应并导致发生SshConnectionException。

I wrapped this command in a try/catch as this exception is expected. Here's an example in C#:

我将此命令包装在 try/catch 中,因为预期会出现此异常。这是 C# 中的一个示例:

try
{
    sshClient.RunCommand("reboot");
}
catch (SshConnectionException exception)
{
    // do something
}