2012-10-05 48 views
0

我tshark的出把給我的IP的表,我想向他們展示在decending秩序的最佳方式,這是我的代碼誰得到的過程輸出:什麼是解析這個過程輸出(tshark的putput)

_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% 

回答

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); 

請注意,我不在家,現在,所以我一直沒能嘗試編譯上面的代碼,但我相當確信其中大部分應該工作。讓我知道。

乾杯。

+0

我必須更改ip地址字符串,現在我得到錯誤:「串」不包含「的TryParse」的定義,並沒有擴展方法「的TryParse」接受型「字符串」的第一個參數可以發現(你缺少using指令或程序集引用?) – user1710944