重庆建站管理系统开发,网页游戏大全2010,品牌网站建设3小蝌蚪,网络安全哪个培训班比较好在SSH中执行长时间的命令#xff0c;SSH.NET及时在文本框中返回连续显示结果。
c# - Execute long time command in SSH.NET and display the results continuously in TextBox - Stack Overflow
博主管理了一个服务器集群#xff0c;准备上自动巡检工具#xff0c;测试在…在SSH中执行长时间的命令SSH.NET及时在文本框中返回连续显示结果。
c# - Execute long time command in SSH.NET and display the results continuously in TextBox - Stack Overflow
博主管理了一个服务器集群准备上自动巡检工具测试在C# WINFORM应用程序中获取服务器的耗时命令时需要及时的返回。 全部代码如下
using Renci.SshNet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;namespace MySecureCRT
{public partial class Form1 : Form{public Form1(){InitializeComponent(); }private void toolStripButton1_Click(object sender, EventArgs e){ new Task(() RunCommand()).Start();}private void RunCommand(){string host x.x.x.x;string username x;string password x;using (var client new SshClient(host, username, password)){client.Connect();var cmd client.CreateCommand(sh high_check.sh);var result cmd.BeginExecute();using (var reader new StreamReader( cmd.OutputStream, Encoding.UTF8, true, 1024, true)){while (!result.IsCompleted || !reader.EndOfStream){string line reader.ReadLine();if (line ! null){listBox1.Invoke((MethodInvoker)(() listBox1.Items.Add(line Environment.NewLine)));}}}cmd.EndExecute(result);}}}
}