Read a txt file (Ping Application)

0

I have a txt file that each line contains the ip plus the server name:

Example: Ip; servername (1st Line) Ip; servername (2nd Line) Ip; servername (3rd Line)

What I intended was for my program to read this file and to ping each ip contained in the txt, the problem is that in addition to the ip the name is also with you on the same line. The server name should only be used in the ConsoleWriteLine to know which server is Timeout or Success

My code at the moment is this:

andthetxtfileisorganizedinthefollowingway:

    
asked by anonymous 29.11.2018 / 18:11

1 answer

0

Here's an example to test the ips (with server names) on each line of the file, as you specified:

using System;
using System.Linq;
using System.Net.NetworkInformation;

namespace ConsoleTestePing
{
    class Program
    {
        static void Main(string[] args)
        {
            var startTimeSpan = TimeSpan.Zero; //Começar a contar a partir do 0
            var periodTimeSpan = TimeSpan.FromMinutes(5); //Periodo de tempo do intervalo (neste caso a cada 5 minutos)


            var ips = System.IO.File.ReadAllLines(@"D:\IPS.txt");


            var timer = new System.Threading.Timer((e) =>
            {
                PingnosIps(ips);

            }, null, startTimeSpan, periodTimeSpan);

            Console.ReadLine();
        }

        static void PingnosIps(string[] ips) //Método Ping
        {
            var servidores = ips.ToList().Select(o => new { Ip = o.Split(';')[0], NameServer = o.Split(';')[1] });

            foreach (var serv in servidores)
            {
                String ip = serv.Ip;
                Ping ping = new Ping();
                PingReply reply = ping.Send(ip); //Faz ping a string ip (que contem o ip no exemplo)
                string status = reply.Status.ToString(); //Transforma o estado da resposta para string
                Console.WriteLine($"Servidor: {serv.NameServer} IP: {serv.Ip} Status: {status}");
            }
        }
    }
}

I hope I have helped!

    
29.11.2018 / 19:55