private static List <NICInformation> GetIpList(IEnumerable <IPAddress> ips)
    {
        var ipAddresses = ips.Select(x => x.ToString()).ToList();
        var result      = new List <NICInformation>();

        foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
        {
            if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 ||
                ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
            {
                foreach (UnicastIPAddressInformation ip in ni.GetIPProperties().UnicastAddresses)
                {
                    if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
                    {
                        var ipStr = ip.Address.ToString();
                        if (ipAddresses.Contains(ipStr))
                        {
                            var nic = new NICInformation();
                            nic.IPv4      = ip.Address.ToString();
                            nic.Mask      = ip.IPv4Mask.ToString();
                            nic.DnsSuffix = ni.GetIPProperties().DnsSuffix;
                            var gateway = ni.GetIPProperties().GatewayAddresses.FirstOrDefault();
                            if (gateway != null)
                            {
                                nic.Gateway = gateway.Address.ToString();
                            }
                            result.Add(nic);
                        }
                    }
                }
            }
        }

        return(result);
    }
Beispiel #2
0
        static void LoopIt()
        {
            Console.WriteLine($"Reporting data to {_infoMonitorUrl}");
            while (_running)
            {
                //GET DATA HERE. This is just an example, you can use your own object in your data collection agent
                var data = new Information()
                {
                    Computer  = ComputerInformation.Get(),
                    Processes = ProcessInformation.Get(),
                    RDPUsers  = RDPUserInformation.Get(),
                    NICs      = NICInformation.Get()
                };


                string json   = JsonSerializer.Serialize(data);
                var    sender = WebRequest.Create(_infoMonitorUrl);
                sender.Method        = "POST";
                sender.ContentType   = "application/json";
                sender.ContentLength = json.Length;
                using (var sw = new StreamWriter(sender.GetRequestStream()))
                {
                    sw.Write(json);
                    sw.Close();
                }
                try
                {
                    var response = (HttpWebResponse)sender.GetResponse();
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        Console.WriteLine($"{DateTime.Now}\tUpdate sent!");
                    }
                    else
                    {
                        Console.WriteLine($"{DateTime.Now}\tServer replied {response.StatusCode}");
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"{DateTime.Now}\t{ex.GetType().FullName}: {ex.Message}");
                }
                Thread.Sleep(_updateDelayMs);
            }
        }