Click here to Skip to main content
15,895,740 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi every body

I'm using this library to connect to the Linux commands I run and run but
Some commands have problems

For example, I have a problem running these commands:

top and top -n 1

error:TERM environment variable not set.


C#
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

C#
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; }
    }
Posted

1 solution

log98 wrote:
error:TERM environment variable not set.
This is a shell message. Linux needs to know which terminal type you are logged in from, in order to display any output. This is usually set automatically by the default, or user, login script, that is run when you first login to the system. I'm not sure what the above code is trying to do, but you need to find some way of getting this set before the actual command runs.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900