private void funcGetTCPPorts() { TcpConnectionInformation[] tcpconact = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpConnections(); IPEndPoint[] tcpconlist = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners(); //Console.WriteLine("Test"); Parallel.ForEach(tcpconact, obj => { NetObj temp = new NetObj(); temp.setLEP(obj.LocalEndPoint.Address.ToString(), obj.LocalEndPoint.Address.IsIPv4MappedToIPv6, obj.LocalEndPoint.Address.IsIPv6LinkLocal, obj.LocalEndPoint.Address.IsIPv6Multicast, obj.LocalEndPoint.Address.IsIPv6SiteLocal, obj.LocalEndPoint.Address.IsIPv6Teredo, obj.LocalEndPoint.Address.ScopeId.ToString(), obj.LocalEndPoint.AddressFamily.ToString(), obj.LocalEndPoint.Port.ToString()); temp.setREP(obj.RemoteEndPoint.Address.ToString(), obj.RemoteEndPoint.Address.IsIPv4MappedToIPv6, obj.RemoteEndPoint.Address.IsIPv6LinkLocal, obj.RemoteEndPoint.Address.IsIPv6Multicast, obj.RemoteEndPoint.Address.IsIPv6SiteLocal, obj.RemoteEndPoint.Address.IsIPv6Teredo, obj.RemoteEndPoint.Address.ScopeId.ToString(), obj.RemoteEndPoint.AddressFamily.ToString(), obj.RemoteEndPoint.Port.ToString()); temp.setState(obj.State.ToString()); ActiveTCPPorts.Add(temp); }); }
private void StringArrayToNetObjList(string[] rows) { Parallel.ForEach(rows, row => { //Eliminates the default NetStat Rows if ((row != null) && (row != "") && (!(row.Contains("Active"))) && (!(row.Contains("Proto")))) { string[] temparray = row.Split(' '); if (temparray.Any()) { int count = 0; string Protocol = ""; string LocalAddress = ""; string RemoteAddress = ""; string State = ""; string ProcessName = ""; string PID = ""; Parallel.ForEach(temparray, temp => { //So many blank strings are in the array. This regex filters out any useless strings. //netstat -ano will return 4 good strings per line, therefore every 4 elements from //parsedContent equate to a row. if (Regex.Matches(temp, @"[a-zA-Z0-9:*]").Count > 0) { if (count == 0) { Protocol = temp; } else if (count == 1) { LocalAddress = temp; } else if (count == 2) { RemoteAddress = temp; } //Every fourth entry (ElementAt[3]) should be the TCP State or PID. else if ((count == 3) && (!(temp.Contains("LIS"))) && (!(temp.Contains("EST"))) && (!(temp.Contains("CLO"))) && (!(temp.Contains("TIME"))) && (!(temp.Contains("SYN"))) && (!(temp.Contains("LAS"))) && (!(temp.Contains("FIN")))) { PID = temp; ProcessName = GetProcessName(temp); } else if (count == 3) { State = temp; } //Every fifth entry should be a PID. else if ((count == 4) && (!(temp.Contains("LIS"))) && (!(temp.Contains("EST"))) && (!(temp.Contains("CLO"))) && (!(temp.Contains("TIME"))) && (!(temp.Contains("SYN"))) && (!(temp.Contains("LAS"))) && (!(temp.Contains("FIN")))) { PID = temp; ProcessName = GetProcessName(temp); } count++; } }); NetObj nob = new NetObj(Protocol, LocalAddress, RemoteAddress, State, ProcessName, PID); NetworkProcList.Add(nob); } } }); }