获取 IP 详细信息并 ping 网关






1.73/5 (8投票s)
这个程序获取所有 IP 详情并 ping 网关。
引言
每当我们遇到任何与互联网相关的问题时,我们都会在 cmd 模式和 IE 浏览器之间切换,以监控网络状态。这个应用程序解析所有网络接口的详细信息,并 ping 所有网关。
为什么它有用,它解决了什么问题等。
到目前为止,要获取 IP 详情,我们必须在命令提示符和浏览器之间切换,并不断输入命令。这个应用程序非常方便,可以解析系统的 IP 属性,并可以 ping 所有网关。
使用代码
这段代码很实用,可以整合到任何现有代码中。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.NetworkInformation;
using System.Diagnostics;
namespace Netmon
{
public partial class Netmon : Form
{
public Netmon()
{
InitializeComponent();
}
private void Start_Click(object sender, EventArgs e)
{
{
richTextBox2.Text = "\n";
//Gateway IP
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in adapters)
{
IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
GatewayIPAddressInformationCollection addresses =
adapterProperties.GatewayAddresses;
if (addresses.Count > 0)
{
foreach (GatewayIPAddressInformation address in addresses)
{
richTextBox2.Text += "Gateway Address | " +
address.Address.ToString() + "\r\n";
// Pinging
IPAddress addr = address.Address;
Ping pingSender = new Ping();
PingOptions options = new PingOptions();
// Use the default Ttl value which is 128,
// but change the fragmentation behavior.
options.DontFragment = true;
// Create a buffer of 32 bytes of data to be transmitted.
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 120;
int count = 0; float Success = 0; float loop = 0;
for (int i = 0; i < 25; )
{
PingReply reply =
pingSender.Send(addr, timeout, buffer, options);
{
//richTextBox2.Text += "\n" + "Address: " +
// reply.Address.ToString() + "\t";
try
{
richTextBox2.Text += "\n" + "RoundTrip time: " +
reply.RoundtripTime +
" ms" + "\t";
}
catch
{
richTextBox2.Text += "\n" + "RoundTrip time: " +
" " + " ms" + "\t";
}
try
{
richTextBox2.Text += "Time to live(TTL): " +
reply.Options.Ttl + "\t";
}
catch
{
richTextBox2.Text += "Time to live(TTL): " +
" " + "\t";
}
//richTextBox2.Text += "Don't fragment: " +
// reply.Options.DontFragment + "\t";
try
{
richTextBox2.Text += "Status : " +
reply.Status + "\t";
}
catch
{
richTextBox2.Text += "Status : " +
reply.Status + "\t";
}
count += Convert.ToInt32(reply.RoundtripTime);
if (reply.Status == IPStatus.Success)
{ Success += 1; }
}
i++;
}
loop += 1;
richTextBox2.Show();
}
richTextBox2.Text += "\n" + "Avg. Roundtime : " +
count / loop+" ms";
float AvgSuccess = ((Success / loop) * 100);
richTextBox2.Text += "\n" + "Avg. Success (%) : " +
AvgSuccess + "%";
}
}
}
}
}
private void Netmon_Load(object sender, EventArgs e)
{
{
// Initialize Variables
richTextBox1.Text = "";
string iphostname = "";
try
{
iphostname = Dns.GetHostName(); // Resolving Host name
IPHostEntry ipentry = Dns.GetHostEntry(iphostname);
IPAddress[] addr = ipentry.AddressList;// Resolving IP Addresses
for (int i = 0; i < addr.Length; i++)
{
try
{
richTextBox1.Text += "IP Address | " +
Convert.ToString(addr[i]) + "\r\n"; }
catch
{
richTextBox1.Text += "IP Address | " + "\r\n";
}
}
richTextBox1.Text += "Hostname | " +
ipentry.HostName + "\r\n";
}
catch
{
richTextBox1.Text += "Hostname | " + "\r\n";
}
// More Details
try
{
// Getting DNS Addresses
for (int i = 0; i < 3;i++ )
richTextBox1.Text += "DNS Address | " +
System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces(
)[i].GetIPProperties().DnsAddresses[0].ToString() + "\r\n";
}
catch
{
richTextBox1.Text += "DNS Address | " + "\r\n";
}
try
{
// Getting Network Type Description
for (int i = 0; i < 2; i++)
richTextBox1.Text += "Description | " +
System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces(
)[i].Description.ToString() + "\r\n";
}
catch
{
richTextBox1.Text += "Description | " + "\r\n";
}
try
{
// Getting the Details of Connection
for (int i = 0; i < 2; i++)
richTextBox1.Text += "Name | " +
System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces(
)[i].Name.ToString() + "\r\n";
}
catch
{
richTextBox1.Text += "Name | " + "\r\n";
}
try
{
// Operational Status
for (int i = 0; i < 2; i++)
richTextBox1.Text += "Operation Status | " +
System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces(
)[i].OperationalStatus.ToString() + "\r\n";
}
catch
{
richTextBox1.Text += "Operation Status | " + "\r\n";
}
try
{
// Getting DHCP Details
for (int i = 0; i < 3; i++)
richTextBox1.Text += "DHCP | " +
System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces(
)[i].GetIPProperties().DhcpServerAddresses[0].ToString() + "\r\n";
}
catch
{
richTextBox1.Text += "DHCP | " + "\r\n";
}
try
{
// Getting DNS Details
for (int i = 0; i < 3; i++)
richTextBox1.Text += "DNS Suffix | " +
System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces(
)[i].GetIPProperties().DnsSuffix.ToString() + "\r\n";
}
catch
{
richTextBox1.Text += "DNS Suffix | " + "\r\n";
}
try
{
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in adapters)
{
IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
GatewayIPAddressInformationCollection addresses =
adapterProperties.GatewayAddresses;
if (addresses.Count > 0)
{
foreach (GatewayIPAddressInformation address in addresses)
{
richTextBox1.Text += "Gateway Address | " +
address.Address.ToString() + "\r\n";
}
}
}
}
catch
{
richTextBox1.Text += "Gateway Address | " + "\r\n";
}
}
}
}
// END
与其从头开始编写类,不如使用 .NET 框架,从而降低代码的复杂性。
关注点
以前当我尝试使用 .NET 内置功能实现一些代码时,很难找到它们,最后当我得到线索并使用试错法时,我才能获得所有详细信息。
历史
这是我创建的基本版本,可能不是最优的。
欢迎提出改进建议。