gpt4 book ai didi

c# - 如何通过SSH正确运行 “top”命令?

转载 作者:行者123 更新时间:2023-12-02 14:39:14 24 4
gpt4 key购买 nike

我正在使用此库连接到运行的Linux命令,但是
有些命令有问题

例如,我在运行以下命令时遇到问题:

顶部和顶部-n 1

error:TERM environment variable not set


private void button2_Click(object sender, EventArgs e)
{
Renci.SshNet.SshClient sshClient = new Renci.SshNet.SshClient("192.168.150.128", "reza", "1");
sshClient.Connect();
var command = sshClient.RunCommand("top");

var line = command.Result.Split('\n');
List<ServerStatusCpu> serverstatus = new List<ServerStatusCpu>();
for (int i = 3; i < line.Length - 1; i++)
{
var li = line[i];
var words = li.Split(' ');
List<string> fillterwords = new List<string>();

foreach (var w in words)
{
if (w != "")
{
fillterwords.Add(w);
}
}

ServerStatusCpu serverStatus = new ServerStatusCpu();
serverStatus.Time = fillterwords[0];
serverStatus.TimeType = fillterwords[1];
serverStatus.Name = fillterwords[2];
serverStatus.UserCpuTime = float.Parse(fillterwords[3].Replace("%", ""));
serverStatus.UserNiceCpuTime = float.Parse(fillterwords[4].Replace("%", ""));
serverStatus.SystemCpuTime = float.Parse(fillterwords[5].Replace("%", ""));
serverStatus.IoWaitCpuTime = float.Parse(fillterwords[6].Replace("%", ""));
serverStatus.IrqCpuTime = float.Parse(fillterwords[7].Replace("%", ""));
serverStatus.SoftwareIrqCpuTime = float.Parse(fillterwords[8].Replace("%", ""));
serverStatus.StealCpuTime = float.Parse(fillterwords[9].Replace("%", ""));
serverStatus.GuestCpuTime = float.Parse(fillterwords[10].Replace("%", ""));
serverStatus.IdleCpuTime = float.Parse(fillterwords[11].Replace("%", ""));

serverstatus.Add(serverStatus);
}
dataGridView1.DataSource = serverstatus;
}


class ServerStatusCpu
{
public class ServerStatusCpu
{
public string Time { get; set; }

public string TimeType { get; set; }

public string Name { get; set; }

public float UserCpuTime { get; set; }

public float SystemCpuTime { get; set; }

public float UserNiceCpuTime { get; set; }

public float IdleCpuTime { get; set; }

public float IoWaitCpuTime { get; set; }

public float IrqCpuTime { get; set; }

public float SoftwareIrqCpuTime { get; set; }

public float StealCpuTime { get; set; }

public float GuestCpuTime { get; set; }
}
}

最佳答案

您遇到的问题与服务器不知道将输入重定向到哪个TERM有关。

请看看http://www.linuxquestions.org/questions/programming-9/term-environment-variable-not-set-593180/

您需要先设置TERM变量,然后才能获取任何输出。

关于c# - 如何通过SSH正确运行 “top”命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16543780/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com