0

我的 Tshark 输出给了我 IP 表,我想按降序显示它们,这是我获得进程输出的代码:

    _process = new ProcessStartInfo(_tsharkPath);
    _process.Arguments = (string.Format("-r {1}{0}{1}  -q -z ip_hosts,tree ", _filePath, "\""));
    _process.WindowStyle = ProcessWindowStyle.Hidden;
    _process.UseShellExecute = false;
    _process.RedirectStandardOutput = true;
    _process.RedirectStandardError = true;
    _process.CreateNoWindow = true;
    _process.UseShellExecute = false;
    _process.ErrorDialog = false;
    Process tsharkProcess = Process.Start(_process);

    StreamReader reader = tsharkProcess.StandardOutput;
    tsharkProcess.WaitForExit(10000);
    List<string> list = new List<string>();
    SortedDictionary<string, string> table = new SortedDictionary<string, string>();

    while (!reader.EndOfStream)
    {
        string read = reader.ReadLine();
        list.Add(read);
    }

我收到了我列表中的表格

这是我的表,我只想重视 IP 和 precent:

===================================================================
 IP Addresses                                 value             rate         percent
-------------------------------------------------------------------
 IP Addresses                                31584       0.322127
  159.0.89.20                                  6002       0.061215          19.00%
  192.168.0.100                               31288       0.319108          99.06%
  60.240.244.115                                 16       0.000163           0.05%
  204.83.232.29                                2698       0.027517           8.54%
  177.16.241.40                                2499       0.025487           7.91%
  90.231.37.56                                  848       0.008649           2.68%
  117.204.2.26                                  172       0.001754           0.54%
  69.247.52.146                                1909       0.019470           6.04%
  188.126.75.143                                 75       0.000765           0.24%
  173.212.107.113                                 8       0.000082           0.03%
  114.47.15.111                                  26       0.000265           0.08%
  219.192.199.2                                   2       0.000020           0.01%
  59.97.56.175                                  986       0.010056           3.12%
  50.53.9.249                                  2376       0.024233           7.52%
  76.18.194.203                                 123       0.001254           0.39%
  41.233.19.39                                  428       0.004365           1.36%
  203.84.186.210                                659       0.006721           2.09%
  58.40.202.85                                    2       0.000020           0.01%
  88.160.36.13                                    2       0.000020           0.01%
  117.199.195.22                                432       0.004406           1.37%
  112.202.141.240                               443       0.004518           1.40%
  46.240.116.228                                  7       0.000071           0.02%
  41.46.174.7                                  1259       0.012841           3.99%
  112.135.159.14                                  1       0.000010           0.00%
  201.215.244.25                                 66       0.000673           0.21%
  223.67.159.170                                  2       0.000020           0.01%
  67.204.32.252                                 626       0.006385           1.98%
  2001:0:9d38:6ab8:aa:a1a:b048:3f7d             286       0.002917           0.91%
  2001:0:9d38:953c:2481:166c:90bb:db19           50       0.000510           0.16%
  2607:5300:10:101::1:209                         8       0.000082           0.03%
  83.248.5.18                                     6       0.000061           0.02%
  98.221.248.250                                 80       0.000816           0.25%
  83.100.249.165                                 60       0.000612           0.19%
  89.254.132.166                                 14       0.000143           0.04%
  119.50.230.109                                 40       0.000408           0.13%
  190.193.144.107  

                          33       0.000337           0.10%
4

1 回答 1

0

Regular Expressions会让你在这里的生活更轻松。首先,定义要捕获的数据的基本结构:

private struct TSharkOutput
{
    public IPAddress ip;
    public uint value;
    public decimal rate;
    public decimal percent;
};

您可以根据需要添加/删除部分并更改数据类型。

接下来是处理这个问题的正则表达式。我将 IPAddress 匹配的首当其冲留给了内部类。

var regTshark = new Regex(@"^\s*(?<ip>[\d.:]+)\s+(?<val>\d+)\s+(?<rate>\d+\.\d+)\s+(?<perc>\d+\.\d+)%", RegexOptions.Compiled);

现在对于您的解析功能,我会做这样的事情;

_process = new ProcessStartInfo(_tsharkPath);
_process.Arguments = (string.Format("-r {1}{0}{1}  -q -z ip_hosts,tree ", _filePath, "\""));
_process.WindowStyle = ProcessWindowStyle.Hidden;
_process.UseShellExecute = false;
_process.RedirectStandardOutput = true;
_process.RedirectStandardError = true;
_process.CreateNoWindow = true;
_process.UseShellExecute = false;
_process.ErrorDialog = false;

var tsharkProcess = Process.Start(_process);
var reader = tsharkProcess.StandardOutput;
tsharkProcess.WaitForExit(10000);
var list = new List<TSharkOutput>();
var regTshark = new Regex(@"^\s*(?<ip>[\d.:]+)\s+(?<val>\d+)\s+(?<rate>\d+\.\d+)\s+(?<perc>\d+\.\d+)%", RegexOptions.Compiled);
Match current;
string line = "";

while (!reader.EndOfStream)
{
    line = reader.ReadLine();
    if ((current = regTshark.Match(line)).Success)
    {
        var shark = new TSharkOutput()
        {
            value = uint.Parse(current.Groups["val"].Value),
            rate = decimal.Parse(current.Groups["rate"].Value),
            percent = decimal.Parse(current.Groups["perc"].Value)
        };

        if (IPAddress.TryParse(current.Groups["ip"].Value, out shark.ip))
            list.Add(shark); // all good add to the list.

    }
}
// sorted is an IEnumerable<TSharkOutput> which has been ordered by the percentage in descending order
var sorted = list.OrderByDescending(ts => ts.percent);

请注意,我现在不在家,所以我无法尝试编译上面的代码,但我相当有信心它的大部分应该可以工作。让我知道。

干杯。

于 2012-10-05T05:45:59.290 回答