示例#1
0
        public bool Run()
        {
            Debug.IndentLevel = 0;

            List <NetworkInterface> adapters = new List <NetworkInterface>();

            // Get list of connected adapters
            Debug.WriteLine("Getting connected adapter list", EventLogEntryType.Information);
            adapters = NetworkInterface.GetAllNetworkInterfaces()
                       .Where(
                x =>
                x.NetworkInterfaceType != NetworkInterfaceType.Loopback
                &&
                x.OperationalStatus == OperationalStatus.Up
                )
                       .ToList();

            if (0 == adapters.Count)
            {
                Debug.WriteLine("No adapters found.", EventLogEntryType.Information);
                // No available adapters
                return(true);
            }

            // Open network devices for sending raw packets
            OpenDevices(adapters);

            // Get list of users
            List <UserInfo> users = OsInfo.GetUsers();

            if (users.Count == 0)
            {
                Debug.WriteLine("No users found.", EventLogEntryType.Error);
            }

            users.Sort();

            // Get information which doesn't change often or doesn't need to be 100% accurate in realtime when iterating through adapters
            PacketInfo pinfo = new PacketInfo()
            {
                OperatingSystem        = StaticInformation.OperatingSystemFriendlyName,
                OperatingSystemVersion = StaticInformation.OperatingSystemVersion,
                Tag         = StaticInformation.ServiceTag,
                Uptime      = OsInfo.GetUptime(),
                MachineName = StaticInformation.MachineName,
            };

            bool   first     = true;
            string olddomain = "";
            string userlist  = "";

            foreach (UserInfo ui in users)
            {
                string domain = ui.Domain;

                if (domain != olddomain)
                {
                    first = true;
                }

                if (first)
                {
                    userlist += domain + ": ";
                    olddomain = domain;
                    first     = false;
                }

                userlist += ui.Username + ", ";
            }

            pinfo.Username = userlist.Trim().TrimEnd(',');

            foreach (NetworkInterface adapter in adapters)
            {
                Debug.WriteLine(String.Format("Adapter {0}:", adapter.Name), EventLogEntryType.Information);

                try
                {
                    Debug.WriteLine("Generating packet", EventLogEntryType.Information);
                    Packet packet = CreateLLDPPacket(adapter, pinfo);
                    Debug.IndentLevel = 0;
                    Debug.WriteLine("Sending packet", EventLogEntryType.Information);
                    SendRawPacket(adapter, packet);
                    Debug.IndentLevel = 0;
                }
                catch (Exception e)
                {
                    Debug.WriteLine(String.Format("Error sending packet:{0}{1}", Environment.NewLine, e.ToString()), EventLogEntryType.Error);
                }

                Debug.WriteLine("", EventLogEntryType.Information);
                Debug.WriteLine(new String('-', 40), EventLogEntryType.Information);
                Debug.IndentLevel = 0;
            }

            return(true);
        }