Example #1
0
File: p2t.cs Project: vsoul-km/p2t
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                DisplayHelp();
                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Application is running without any arguments. Starting an interactive mode.");
                Console.ResetColor();
                Console.ForegroundColor = ConsoleColor.DarkGray;
                Console.WriteLine("Please enter parameters following this step-by-step procedure:");
                Console.ResetColor();
                Console.WriteLine();

                while (true)
                {
                    Console.Write("Enter the IP address or the hostname to ping: ");
                    string pingAddress = Console.ReadLine();
                    if (string.IsNullOrEmpty(pingAddress))
                    {
                        continue;
                    }
                    if (pingAddress.Contains(" "))
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("Wrong address.");
                        Console.ResetColor();
                        continue;
                    }

                    if (!ValidateAddress.Ip(pingAddress))
                    {
                        if (!ValidateAddress.HostName(pingAddress))
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.WriteLine("Cannot resolve the address to IP address.");
                            Console.ResetColor();
                            continue;
                        }
                    }

                    CommandLineArguments.Address = pingAddress;
                    break;
                }

                while (true)
                {
                    Console.Write("Enter the interval between RTT in ms (default is 500 ms): ");
                    string pingInterval = Console.ReadLine();
                    if (string.IsNullOrEmpty(pingInterval))
                    {
                        break;
                    }
                    if (int.TryParse(pingInterval, out int result))
                    {
                        CommandLineArguments.PingRttInterval = result;
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("Wrong interval between RTT.");
                        Console.ResetColor();
                        continue;
                    }
                    break;
                }

                while (true)
                {
                    Console.Write("Do you want to log ping to file (y/n)?: ");
                    string enableLogAnswer = Console.ReadLine();
                    if (enableLogAnswer != "y" && enableLogAnswer != "n")
                    {
                        continue;
                    }
                    if (enableLogAnswer == "y")
                    {
                        CommandLineArguments.LogEnabled = true;
                    }
                    break;
                }
            }

            if (args.Length > 0)
            {
                UtilityArguments arguments = new UtilityArguments(args);

                if (args[0].StartsWith("/?") || args[0].StartsWith("?"))
                {
                    DisplayHelp();
                    Environment.Exit(0);
                }

                if (args[0].StartsWith("-"))
                {
                    DisplayHelp();
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Wrong address. Please use the correct address.");
                    Console.ResetColor();
                    Environment.Exit(0);
                }
                else
                {
                    CommandLineArguments.Address = args[0];
                }

                if (!string.IsNullOrEmpty(arguments.L))
                {
                    if (int.TryParse(arguments.L, out int result))
                    {
                        CommandLineArguments.PacketSize = result;
                    }
                    else
                    {
                        DisplayHelp();
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("Error parsing argument -l");
                        Console.WriteLine(" Wrong packet payload size.");
                        Console.ResetColor();
                        Environment.Exit(0);
                    }
                }

                if (!string.IsNullOrEmpty(arguments.C))
                {
                    if (int.TryParse(arguments.C, out int result))
                    {
                        CommandLineArguments.PingCount = result;
                    }
                    else
                    {
                        DisplayHelp();
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("Error parsing argument -c");
                        Console.WriteLine(" Wrong number of ping echo requests.");
                        Console.ResetColor();
                        Environment.Exit(0);
                    }
                }

                if (!string.IsNullOrEmpty(arguments.W))
                {
                    if (int.TryParse(arguments.W, out int result))
                    {
                        CommandLineArguments.PingTimeout = result;
                    }
                    else
                    {
                        DisplayHelp();
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("Error parsing argument -w");
                        Console.WriteLine(" Wrong timeout.");
                        Console.ResetColor();
                        Environment.Exit(0);
                    }
                }

                if (!string.IsNullOrEmpty(arguments.I))
                {
                    if (int.TryParse(arguments.I, out int result))
                    {
                        CommandLineArguments.PingRttInterval = result;
                    }
                    else
                    {
                        DisplayHelp();
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("Error parsing argument -i");
                        Console.WriteLine(" Wrong RTT interval.");
                        Console.ResetColor();
                        Environment.Exit(0);
                    }
                }

                if (arguments.T & (string.IsNullOrEmpty(arguments.Tt) || string.IsNullOrEmpty(arguments.Tc)))
                {
                    DisplayHelp();
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Telegram Bot access token and Telegram Chat ID are not defined! Please define them using -tt and -tc arguments.");
                    Console.ResetColor();
                    Environment.Exit(0);
                }

                if (!string.IsNullOrEmpty(arguments.Tt) & string.IsNullOrEmpty(arguments.Tc))
                {
                    DisplayHelp();
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Telegram Chat ID is not defined! Please define it using -tc argument.");
                    Console.ResetColor();
                    Environment.Exit(0);
                }

                if (!string.IsNullOrEmpty(arguments.Tc) & string.IsNullOrEmpty(arguments.Tt))
                {
                    DisplayHelp();
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Telegram Bot access token is not defined! Please define it using -tt argument.");
                    Console.ResetColor();
                    Environment.Exit(0);
                }

                if ((arguments.Ta || arguments.Te) & (string.IsNullOrEmpty(arguments.Tt) || string.IsNullOrEmpty(arguments.Tc)))
                {
                    DisplayHelp();
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Telegram Bot access token or Telegram Chat ID is not defined! Please define them using -tt and -tc arguments.");
                    Console.ResetColor();
                    Environment.Exit(0);
                }

                CommandLineArguments.DoNotFragment = arguments.F;
                CommandLineArguments.FollowTheName = arguments.Follow;
                CommandLineArguments.LogEnabled    = arguments.Log;
                CommandLineArguments.AddDate       = arguments.D;

                if (!string.IsNullOrEmpty(arguments.Tt) & !string.IsNullOrEmpty(arguments.Tc))
                {
                    CommandLineArguments.UsingTelegram = true;
                }

                CommandLineArguments.TelegramBotToken = arguments.Tt;
                CommandLineArguments.TelegramChatId   = arguments.Tc;

                if (arguments.Ta)
                {
                    CommandLineArguments.TelegramSendAll    = arguments.Ta;
                    CommandLineArguments.TelegramSendErrors = false;
                }

                if (arguments.Te)
                {
                    CommandLineArguments.TelegramSendErrors = arguments.Te;
                }
            }

            //validate the address
            //logic: if address is IP address - continue pinging using the IP address;
            //       if address is name - try to resolve it to IP address and continue pinging using the first IP address from the resolved array of IP's;
            //       if both unsuccessful - terminate the program with the error code -1
            bool addressIsIp = ValidateAddress.Ip(CommandLineArguments.Address);

            if (addressIsIp)
            {
                Variables.Address = CommandLineArguments.Address;
                //disable FollowTheName option and set hostname to empty because using an ip address
                CommandLineArguments.FollowTheName = false;
                Variables.HostName = "";
            }
            else
            {
                bool addressIsHostName = ValidateAddress.HostName(CommandLineArguments.Address);

                if (addressIsHostName)
                {
                    Variables.Address  = CommandLineArguments.Address;
                    Variables.HostName = CommandLineArguments.Address;

                    //change the global input address (from Command Line arguments) to the ip address => do not allow the ping module to resolve the hostname to an ip address
                    if (!CommandLineArguments.FollowTheName)
                    {
                        string singleIp = ResolveHostname.GetSingleIp(CommandLineArguments.Address);
                        if (!string.IsNullOrEmpty(singleIp))
                        {
                            Variables.Address = singleIp;
                        }
                    }
                }
                else
                {
                    string textHostNotFound = "Can't resolve the hostname '" + CommandLineArguments.Address + "' to an IP address.";
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(textHostNotFound);
                    Console.ResetColor();
                    Environment.Exit(-1);
                }
            }

            //save a reference so it does not get GC'd
            _consoleHandler = ConsoleCtrlCheck;

            //set handler that will trap exit
            SetConsoleCtrlHandler(_consoleHandler, true);

            Ping ping = new Ping(CommandLineArguments);

            ping.StartPing();

            Result();
            Console.WriteLine();
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey(true);
        }