直接上代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//RunCmd2("cmd", "pin );
for( int i = 1; i <= 255; i++)
runcmd("192.168.1."+i.ToString());
}
private void runcmd(string host)
{
//------------使用ping类------
//string host = "www.baidu.com";
Ping p1 = new Ping();
//PingReply reply = p1.Send(host); //发送主机名或Ip地址
PingReply reply = p1.Send(host,200); //发送主机名或Ip地址
StringBuilder sbuilder;
Console.WriteLine("------------------------------" + host);
if (reply.Status == IPStatus.Success)
{
sbuilder = new StringBuilder();
sbuilder.AppendLine(string.Format("Address: {0} ", reply.Address.ToString()));
sbuilder.AppendLine(string.Format("RoundTrip time: {0} ", reply.RoundtripTime));
sbuilder.AppendLine(string.Format("Time to live: {0} ", reply.Options.Ttl));
sbuilder.AppendLine(string.Format("Don't fragment: {0} ", reply.Options.DontFragment));
sbuilder.AppendLine(string.Format("Buffer size: {0} ", reply.Buffer.Length));
Console.WriteLine(sbuilder.ToString());
}
else if (reply.Status == IPStatus.TimedOut)
{
Console.WriteLine("超时");
}
else
{
Console.WriteLine("失败");
}
}
}
}
运行结果如下图
特此记录
anlog
2023年1月1日22点22分