12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Net.NetworkInformation;
- namespace PingList
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine("本程序用来批量Ping IP地址列表并返回结果");
- Console.WriteLine("By 281Shenjian");
-
- if (!System.IO.File.Exists("IPs.txt"))
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("\n没有找到文件IPs.txt,请新建一个文本文件,每个IP为一行,再运行本程序。");
- Console.WriteLine("\n按任意键退出!");
- Console.ReadKey();
- return;
- }
- Console.WriteLine("读取列表...");
- var list = System.IO.File.ReadAllLines("IPs.txt");
- StringBuilder sb = new StringBuilder();
- foreach (var item in list)
- {
- var r = ping(item, 1000);
- if (r == 1000)
- {
- Console.WriteLine(item + "......NOT Response");
- sb.AppendLine(item + "\tNOT Response");
- }
- else if (r == 99999)
- {
- Console.WriteLine(item + "......Err");
- sb.AppendLine(item + "\tErr");
- }
- else
- {
- Console.WriteLine(item + "......OK");
- sb.AppendLine(item + "\tOK");
- }
- }
- Console.WriteLine("All Done!");
- System.IO.File.WriteAllText("result.txt", sb.ToString());
- System.Diagnostics.Process.Start("result.txt");
- }
- static Ping pingSender = new Ping();
- static long ping(string ip,int timeout)
- {
- try
- {
- var pingReply = pingSender.Send(ip, timeout);
- if (pingReply.Status == IPStatus.Success)
- {
- return pingReply.RoundtripTime;
- }
- else
- {
- return timeout;
- }
- }
- catch (Exception err)
- {
- return 99999;
- }
- }
- }
- }
|