如何在C#中从AIX topas命令结果中剥离ANSI转义代码 [英] How to strip ANSI escape codes from AIX topas command result in C#

查看:41
本文介绍了如何在C#中从AIX topas命令结果中剥离ANSI转义代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用C#控制台应用程序捕获AIX实时信息,例如CPU,内存,IO负载,因为我想在第三方定制仪表板中显示该信息.运行topas命令后,我需要定期捕获以下全部文本:

I'm trying to capture AIX real-time information like CPU, memory, IO load with a C# console application because I would like to show that information in a third part custom dashboard. After running the command topas, I need to capture periodically the whole following text:

我试图使用在某个论坛中发现的以下代码捕获它:

I tried to capture it with the following code that I have found out in some forum:

using Renci.SshNet;
using System;
using System.IO;
using System.Threading;

namespace SSH_check_commands
{
public class MyAsyncInfo
{
    public MyAsyncInfo(Byte[] array, ShellStream stream)
    {
        ByteArray = array;
        Stream = stream;
    }

    public Byte[] ByteArray { get; set; }
    public ShellStream Stream { get; set; }
}

class Program
{
    private static ShellStream stream;
    private static SshClient client;
    private static byte[] _data = new byte[2048];
    static void Main(string[] args)
    {
        client = new SshClient("host", "user", "password");
        client.Connect();
        stream = client.CreateShellStream(@"xterm", 80, 24, 800, 600, 1024);
        stream.DataReceived += StartAsyncRead;
        stream.Write("bash\n");
        Thread.Sleep(5000);
        stream.Write("topas -i 10\n");
        Console.ReadLine();
    }

    private static void StartAsyncRead(object sender, EventArgs e)
    {
        try
        {
            stream.BeginRead(_data, 0, _data.Length, OnReadCompletion, new MyAsyncInfo(_data, stream));

        }
        catch (Exception exception)
        {
            Console.WriteLine(exception);
        }
    }

    private static void OnReadCompletion(IAsyncResult ar)
    {
        try
        {
            var mai = (MyAsyncInfo)ar.AsyncState;
            int datalen = mai.Stream.EndRead(ar);
            string line = client.ConnectionInfo.Encoding.GetString(mai.ByteArray, 0, datalen);
            Console.Write(line);
        }
        catch (Exception exception)
        {
            Console.WriteLine(exception);
        }
    }
    public static string SendCommand(string cmd, ShellStream sh)
    {
        StreamReader reader = null;
        try
        {
            reader = new StreamReader(sh);
            StreamWriter writer = new StreamWriter(sh);
            writer.AutoFlush = true;
            writer.WriteLine(cmd);
            while (sh.Length == 0)
            {
                Thread.Sleep(1000);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("exception: " + ex.ToString());
        }
        return reader.ReadToEnd();
    }
}
}

但是我无法解析结果,因为它不是结构化的:

But I can't parse the result because it's not structured:

能请你帮我吗?

解决方法:

我已经改变了解决问题的方法.我无法通过ssh流捕获整个文本,因为我仅收到更改的字符.因此,我定期运行以下ssh命令将nmon内容保存在文件中(称为 hostname_YYMMDD_H24_mm.nmon ):

I have changed the approach to the problem. I could not catch whole text with ssh streaming because I receive only changed chars. Hence periodically I run the following ssh command to save nmon content in a file (called hostname_YYMMDD_H24_mm.nmon):

nmon -f -c 1

使用cat命令之后,我可以读取文件,提取内容,对其进行转换并将其加载到我的仪表板中.

After with the cat command I can read the file, extract the content, transform it and load it in my dashboard.

推荐答案

结果是结构化的.这些是 ANSI转义代码.您可以解析这些内容,就像您的SSH终端客户端解析那些内容以显示漂亮的输出一样.

The results are structured. Those are ANSI escape codes. You can parse those, the same way your SSH terminal client parses those to display the nice output.

但这是一项艰巨的任务.参见基于SSH.NET的彩色终端仿真器.

But that's a huge task. See SSH.NET based colored terminal emulator.

尽管我会说,首先解析试图供人使用的命令的输出是一个坏主意.当然,还有另一种以更易于解析的格式检索相同信息的方法.但这是另一个网站(例如超级用户)的问题.

Though I'd say that trying to parse an output of a command that is intended for a human use is a bad idea, in the first place. For sure there's another way to retrieve the same information in a format that's easier to parse. But that's a question for another site (for example Super User).

如果只想剥离ANSI转义码,则可以使用正则表达式,如如何删除^[,以及使用Linux Shell脚本编写的文件中的所有转义序列:

If you just want to strip the ANSI escape codes, you can use a regular expression, as shown in How to remove ^[, and all of the escape sequences in a file using linux shell scripting:

s = new Regex(@"\x1B\[[^@-~]*[@-~]").Replace(s, "");

这篇关于如何在C#中从AIX topas命令结果中剥离ANSI转义代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆