|
@@ -17,26 +17,35 @@ using System.Reflection;
|
|
|
|
|
|
namespace ConnectionStat
|
|
|
{
|
|
|
- class Program
|
|
|
+ internal class Program
|
|
|
{
|
|
|
- static void Main(string[] args)
|
|
|
+ private static void Main(string[] args)
|
|
|
{
|
|
|
var all = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpConnections();
|
|
|
|
|
|
if (all.Length == 0)
|
|
|
{
|
|
|
- Console.WriteLine("GetActiveTcpConnections return 0 items.");
|
|
|
+ Console.WriteLine("Unexpected: GetActiveTcpConnections return 0 items.");
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- var allDic = all.GroupBy(p => p.State).Select(p => new { State = p.Key, Count = p.Count() }).Where(p => p.Count != 0).ToDictionary(p => p.State, p => p.Count);
|
|
|
+ var allDic = all.GroupBy(p => p.State)
|
|
|
+ .Select(p => new { State = p.Key, Count = p.Count() })
|
|
|
+ .ToDictionary(p => p.State, p => p.Count);
|
|
|
|
|
|
- if (args.Length == 0)
|
|
|
- {
|
|
|
- PrintConnectionStat(allDic, "All");
|
|
|
- }
|
|
|
- else
|
|
|
+ PrintConnectionStat(allDic, "<ALL IN THIS HOST>");
|
|
|
+
|
|
|
+ var topRemote = all.GroupBy(p => p.RemoteEndPoint.Address)
|
|
|
+ .Select(p => new { Address = p.Key, Count = p.Count() })
|
|
|
+ .OrderByDescending(p => p.Count)
|
|
|
+ .FirstOrDefault();
|
|
|
+
|
|
|
+ if(topRemote!=null) Console.WriteLine($" Top Remote Address: {topRemote.Address}, Connections: {topRemote.Count}");
|
|
|
+
|
|
|
+ if (args.Length != 0)
|
|
|
{
|
|
|
+ var dicTotal = new Dictionary<TcpState, int>();
|
|
|
+
|
|
|
foreach (var argString in args)
|
|
|
{
|
|
|
if (!int.TryParse(argString, out var port)) continue;
|
|
@@ -45,19 +54,28 @@ namespace ConnectionStat
|
|
|
var portDic = filterByPort.GroupBy(p => p.State).Select(p => new { State = p.Key, Count = p.Count() }).Where(p => p.Count != 0).ToDictionary(p => p.State, p => p.Count);
|
|
|
|
|
|
PrintConnectionStat(portDic, "Port " + port);
|
|
|
+
|
|
|
+ foreach (var kvp in portDic)
|
|
|
+ {
|
|
|
+ if (dicTotal.ContainsKey(kvp.Key)) dicTotal[kvp.Key] += kvp.Value;
|
|
|
+ else dicTotal[kvp.Key] = kvp.Value;
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
+ if (dicTotal.Count > 1) PrintConnectionStat(dicTotal, "<SUM OF LISTED PORTS>");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private static void PrintConnectionStat(Dictionary<TcpState, int> allDic, string label)
|
|
|
{
|
|
|
- Console.WriteLine($" * Connections of {label}");
|
|
|
+ Console.Write($" * Connections of {label}");
|
|
|
+ Console.Write($" TOTAL={allDic.Sum(p => p.Value)}");
|
|
|
foreach (var kvp in allDic)
|
|
|
{
|
|
|
- Console.Write($" {kvp.Key}={kvp.Value}");
|
|
|
+ Console.Write($" {kvp.Key}={kvp.Value}");
|
|
|
}
|
|
|
- Console.WriteLine($" TOTAL={allDic.Sum(p => p.Value)}");
|
|
|
+
|
|
|
Console.WriteLine();
|
|
|
}
|
|
|
}
|
|
|
-}
|
|
|
+}
|