第二连接上的ssh.net sshoperation timeout

发布于 2025-01-18 17:09:38 字数 1879 浏览 2 评论 0原文

我要以第二种形式连接到SSH。当我第一次执行Connect()命令时,一切正常。如果我关闭表单并尝试重新连接,则在调用Connect()之后将获得SshoperationTimeout。

ConnectionInfo connNfo = new ConnectionInfo(ip, 22, username,
               new AuthenticationMethod[]{
                    // Pasword based Authentication
                    new PasswordAuthenticationMethod(username,password),
               }
            )

client = new SshClient(connNfo);
try
 { 
      client.Connect();
 }
 catch 
 {
      MessageBox.Show("Error","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
      this.Close();
      return;
 }

shellStreamSSH = client.CreateShellStream("vt-100", 80, 60, 800, 600, 65536);
shellThread = new Thread(() => recvSSHData(shellStreamSSH));
shellThread.Start();

的控制台方法输出的方法:

void recvSSHData(ShellStream shellStreamSSH)
        {
            while (true)
            {
                try
                {
                    if (shellStreamSSH != null && shellStreamSSH.DataAvailable)
                    {
                        string strData = shellStreamSSH.Read();
                        appendRichTextBox(strData);
                    }
                }
                catch
                {}

                Thread.Sleep(200);
            }
        }

打印称为关闭和断开SSH客户端

private void Form3_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (shellThread != null && shellThread.IsAlive)
            {
                shellThread.Abort(); 
            }

            if (shellStreamSSH != null)
            { 
                shellStreamSSH.Close();
                shellStreamSSH.Dispose();
            }

            if (client != null && client.IsConnected)
            {
                client.Disconnect();
                client.Dispose();
            }
        }

I'm connecting to the ssh on a second form. When I first execute the connect() command, everything works fine. If I close the form and try to reconnect I get SshOperationTimeout after the method connect() is called.

ConnectionInfo connNfo = new ConnectionInfo(ip, 22, username,
               new AuthenticationMethod[]{
                    // Pasword based Authentication
                    new PasswordAuthenticationMethod(username,password),
               }
            )

client = new SshClient(connNfo);
try
 { 
      client.Connect();
 }
 catch 
 {
      MessageBox.Show("Error","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
      this.Close();
      return;
 }

shellStreamSSH = client.CreateShellStream("vt-100", 80, 60, 800, 600, 65536);
shellThread = new Thread(() => recvSSHData(shellStreamSSH));
shellThread.Start();

Method to print the output of the console

void recvSSHData(ShellStream shellStreamSSH)
        {
            while (true)
            {
                try
                {
                    if (shellStreamSSH != null && shellStreamSSH.DataAvailable)
                    {
                        string strData = shellStreamSSH.Read();
                        appendRichTextBox(strData);
                    }
                }
                catch
                {}

                Thread.Sleep(200);
            }
        }

Method called to close and disconnect the ssh client:

private void Form3_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (shellThread != null && shellThread.IsAlive)
            {
                shellThread.Abort(); 
            }

            if (shellStreamSSH != null)
            { 
                shellStreamSSH.Close();
                shellStreamSSH.Dispose();
            }

            if (client != null && client.IsConnected)
            {
                client.Disconnect();
                client.Dispose();
            }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文