【发布时间】:2021-11-05 01:20:45
【问题描述】:
我正在尝试制作一个连接到 SSH 服务器然后登录的程序。我已经成功创建了 tracert 或 ping 命令,但是 SSH 连接对我根本不起作用。我正在使用 SSH.NET。我的文本框没有输出。我已经听从了 StackOverflow (Execute long time command in SSH.NET and display the results continuously in TextBox) 的一些建议,但它对我不起作用。
代码如下:
private void button5_Click(object sender, EventArgs e)
{
string host = "someserverip";
string username = textBox6.Text;
string password = textBox8.Text;
string ipaddress = textBox12.Text;
using (var radius = new SshClient(host, port, username, password))
{
radius.Connect();
var cmd = radius.CreateCommand("check-host" + ipaddress);
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)
{
textBox10.Invoke(
(Action)(() =>
textBox10.AppendText(line + Environment.NewLine)));
}
}
}
cmd.EndExecute(result);
}
textBox10.ScrollBars = ScrollBars.Vertical;
}
【问题讨论】: