This project is read-only.

Using ShellStream with "top" command

Nov 30, 2012 at 5:03 PM

Hey all,

I'm trying to get the results of the "top" command with this library, but having some issues.  If you're not familiar with the top command it creates a live updating output on the unix console of the CPU and memory usage of the running processes on the unix server.

Its hard to get that data using the library because there's nothing to really "expect"... I cant use the Expect function to wait for a prompt, because said prompt will never come until you issue a cancel command "CTRL-C" in unix console... any tips?

Current code:

string reply = String.Empty;
string serverIP = StringEnum.GetStringValue(server);
Renci.SshNet.PasswordConnectionInfo connectionInfo = new Renci.SshNet.PasswordConnectionInfo(serverIP, "username", "password");

string command = "top";
string unixResults = String.Empty;

using (var client = new Renci.SshNet.SshClient(connectionInfo))
{
    client.Connect();

    using (var shellStream = client.CreateShellStream("dumb", 0, 0, 0, 0, 1024))
    {
        // Wait for prompt
        reply = shellStream.Expect(new Regex(@">"));

        shellStream.WriteLine(command);
        reply = shellStream.Expect(new Regex(@">"));
    }
}

Coordinator
Dec 20, 2012 at 2:24 PM

Hey,

 

What you can try to do is to simply read it as a stream without expecting anything and see if it works for you.

 

Thanks,

Oleg

Jun 26, 2013 at 5:58 PM
Edited Jun 26, 2013 at 5:58 PM
If you want to display top's output nicely and allow for interaction, you'll need to look into ANSI codes. See http://ascii-table.com/ansi-escape-sequences.php
The easiest way to implement an ANSI capable terminal on Windows is through a console application. Console applications have a cursor that you may position freely, they support the notion of columns/lines and let you choose foreground/background colors on a per-character basis.