Read a txt file (Ping Application)

Asked

Viewed 61 times

1

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

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

What I intended was that my program would read that file and run a ping to each ip contained in txt, the problem is that besides ip the name is also with you in the same line. The server name should only be used in Consolewriteline to know which server is "Timeout" or "Success"

my code right now is this: inserir a descrição da imagem aqui

and the txt file is organized as follows: inserir a descrição da imagem aqui

1 answer

0

Below is 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’ve helped!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.