Exemplo n.º 1
0
        public void Execute(List<string> hosts, int timeout = 120)
        {
            Console.Clear();

            if (!CheckConsoleWidth()) {
                Console.WriteLine("Console window width to small.");
            }

            Console.WriteLine($"MultiPing running with Timeout: {timeout} Interval: {_timer.Interval}");
            Console.WriteLine("Press ESC to quit.");

            CreateHeader();

            int i = 0;

            foreach (var host in hosts)
            {
                _customPingReplies[host] = new CustomPingReply();
                var customPingReply = _customPingReplies[host];
                customPingReply.Ping.PingCompleted += PingOnPingCompleted;
                customPingReply.Timeout = timeout;
                customPingReply.Index = i++;
                customPingReply.Host = host;
            }

            PingHosts();
            _timer.Enabled = true;
        }
Exemplo n.º 2
0
        private void UpdateConsole(CustomPingReply customPingReply)
        {
            lock (_consoleLock)
            {
                var consoleDefaultColor = Console.ForegroundColor;
                const int pad = 19;

                customPingReply.Count++;
                var value = string.Empty;
                switch (customPingReply.Reply?.Status)
                {
                    case IPStatus.Success:
                        Console.ForegroundColor = ConsoleColor.DarkGreen;
                        value = "Ok".PadRight(pad);
                        break;
                    case IPStatus.DestinationNetworkUnreachable:
                    case IPStatus.DestinationHostUnreachable:
                    case IPStatus.DestinationProtocolUnreachable:
                    case IPStatus.DestinationPortUnreachable:
                    case IPStatus.NoResources:
                    case IPStatus.BadOption:
                    case IPStatus.HardwareError:
                    case IPStatus.BadRoute:
                    case IPStatus.TtlExpired:
                    case IPStatus.TtlReassemblyTimeExceeded:
                    case IPStatus.ParameterProblem:
                    case IPStatus.SourceQuench:
                    case IPStatus.BadDestination:
                    case IPStatus.DestinationUnreachable:
                    case IPStatus.BadHeader:
                    case IPStatus.UnrecognizedNextHeader:
                    case IPStatus.IcmpError:
                    case IPStatus.DestinationScopeMismatch:
                    case IPStatus.Unknown:
                    case IPStatus.PacketTooBig:
                        Console.ForegroundColor = ConsoleColor.Red;
                        customPingReply.FailCount++;
                        value = "Failed".PadRight(pad);
                        break;
                    case IPStatus.TimeExceeded:
                    case IPStatus.TimedOut:
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        customPingReply.TimeoutCount++;
                        value = "Timeout".PadRight(pad);
                        break;
                    default:
                        Console.ForegroundColor = ConsoleColor.Red;
                        break;
                }

                var i = customPingReply.Index;

                var currentColor = Console.ForegroundColor;
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.CursorLeft = 0;
                Console.CursorTop = RowOffset + i;
                var output = _customPingReplies.Keys.ElementAt(i);
                Console.Write(output.PadRight(pad));
                Console.ForegroundColor = currentColor;

                Console.CursorLeft = Column;
                Console.CursorTop = RowOffset + i;
                output = string.Format($"{value}");
                Console.Write(output.PadRight(pad));

                Console.CursorLeft = Column * 2;
                Console.CursorTop = RowOffset + i;
                output = string.Format($"{customPingReply.Reply?.RoundtripTime}");
                Console.Write(output.PadRight(pad));

                Console.CursorLeft = Column * 3;
                Console.CursorTop = RowOffset + i;
                output = string.Format($"{customPingReply.Count - customPingReply.FailCount - customPingReply.TimeoutCount}");
                Console.Write(output.PadRight(pad));

                Console.CursorLeft = Column * 4;
                Console.CursorTop = RowOffset + i;
                currentColor = Console.ForegroundColor;
                if (customPingReply.TimeoutCount > 0)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                }
                output = string.Format($"{customPingReply.TimeoutCount}");
                Console.Write(output.PadRight(pad));
                Console.ForegroundColor = currentColor;

                Console.CursorLeft = Column * 5;
                Console.CursorTop = RowOffset + i;
                currentColor = Console.ForegroundColor;
                if (customPingReply.FailCount > 0)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                }
                output = string.Format($"{customPingReply.FailCount}");
                Console.Write(output.PadRight(pad));
                Console.ForegroundColor = currentColor;

                Console.ForegroundColor = consoleDefaultColor;
                Console.WriteLine("");
                Console.WriteLine("");
            }
        }