当我的 ssh 客户端与 ssh 服务器断开连接时,如何减少 ssh.net 超时? [英] How do I decrease the ssh.net timeout when my ssh client disconnects from the ssh server?

查看:25
本文介绍了当我的 ssh 客户端与 ssh 服务器断开连接时,如何减少 ssh.net 超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ssh.net 用 C# 编写了一个小程序,该程序连接到 SSH 服务器,进行一些更改,然后重新启动 SSH 服务器上的 SSHD 服务.

I wrote a small program in C# using ssh.net that connects to an SSH server, makes some changes, and restarts the SSHD service on the SSH server.

不幸的是,SSH 服务器正在缓慢的嵌入式系统上运行,并且 SSHD 服务的重新启动速度不够快,无法无缝地重新启动(无需断开客户端连接).SSH 服务实际上需要大约一分钟才能重新启动.这很好.

Unfortunately, the SSH server is being run on a slow embeded system, and the SSHD service is not restarted quickly enough to do so seamlessly (without disconnecting the client). The SSH service actually takes about a minute to restart. This is fine.

我的代码如下:

Console.WriteLine("Restarting SSHD service on modem...");

try
{
    using (var client = new SshClient(IPAddress, "root", "PASSWORD"))
    {
        client.ConnectionInfo.Timeout = TimeSpan.FromSeconds(10);
        client.Connect();

        client.RunCommand("service sshd restart");

        client.Disconnect();
    }
}
catch (System.Net.Sockets.SocketException)
{
    //We got disconnected because we just restarted the sshd service
    //This is expected
    Console.WriteLine("\tSuccess");
}
catch
{
    //We got disconnected for some other reason
    Console.WriteLine("\tIt appears there was a problem restarting the sshd service");
}

我遇到的问题是 ssh 客户端需要一分钟甚至更长的时间才能确定 SSH 服务器不再响应,并抛出 System.Net.Sockets.SocketException.有什么办法可以缩短这个超时时间?我不希望它重新连接 - 我只是希望它更快地抛出异常.这是某种 ssh.net 特定的超时值,还是 System.Net.Sockets 超时?

The issue I'm having is that the ssh client takes up to a minute, or sometimes longer, to figure out that the SSH server is no longer responding, and throw a System.Net.Sockets.SocketException. Is there any way to shorten this timeout? I don't want it to reconnect - I just want it to throw the exception sooner. Would this be some sort of ssh.net specific timeout value, or a System.Net.Sockets timeout?

推荐答案

感谢Hang的建议,以下代码解决了问题:

Thanks to Hang's suggestions, the following code has solved the problem:

Console.WriteLine("Restarting SSHD service on modem...");

try
{
    var client = new SshClient(IPAddress, "root", "PASSWORD")
    client.ConnectionInfo.Timeout = TimeSpan.FromSeconds(10);
    client.Connect();

    client.RunCommand("service sshd restart >/dev/null 2>&1 &");
    Console.WriteLine("\tSuccess");
}
catch (System.Net.Sockets.SocketException)
{
    //We got disconnected because we just restarted the sshd service
    //This is expected
    Console.WriteLine("\tSuccess");
}
catch
{
    //We got disconnected for some other reason
    Console.WriteLine("\tIt appears there was a problem restarting the sshd service");
}

需要注意的是:

  • RunCommand 行现在使用 >/dev/null 2>&1 来摆脱 stdout 和 stderr
  • RunCommand 行现在使用 & 在后台运行命令
  • try 块现在包含一个 Console.WriteLine("\tSuccess");,因为这是 RunCommand() - 它会继续运行而不是抛出任何异常
  • 不再有 client.Disconnect()using 语句,因此没有任何尝试断开与 ssh 服务器的连接 - 代码会继续运行
  • The RunCommand line now uses >/dev/null 2>&1 to get rid of stdout and stderr
  • The RunCommand line now uses & to run the command in the background
  • The try block now contains a Console.WriteLine("\tSuccess");, because this is the most likely result of the RunCommand() - the fact that it will keep going instead of throwing any exceptions
  • There is no longer a client.Disconnect() or a using statement, so nothing attempts to disconnect from the ssh server - the code just keeps going

这在我的情况下有效的原因是因为我的程序在此之后基本上什么都不做 - 它只是退出.其他人可能想等到 SshClient 再次可用,并正确断开连接以清理和释放资源.

The reason this works in my case is because my program basically does nothing else after this - it just exits. Others may want to wait until the SshClient becomes available again, and disconnect properly to clean up and release the resources.

这篇关于当我的 ssh 客户端与 ssh 服务器断开连接时,如何减少 ssh.net 超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆