最近公司网络不稳定,不定期、不定时发生阻断现象。
为查找阻断点,做了一个定时tracert程序,30秒触发一次,记录路由情况。
如图:
![](https://i-blog.csdnimg.cn/blog_migrate/d24c838b892ace7e67b8ffd091ff9021.jpeg)
程序关键代码如下:
public string lianjieleixing = "", shujulianjie = "", yuedian = "", jiange = "",bid="";
private void Form1_Load(object sender, EventArgs e)
{
lianjieleixing = System.Configuration.ConfigurationManager.AppSettings["lianjieleixing"];
shujulianjie = System.Configuration.ConfigurationManager.AppSettings["shujulianjie"];
yuedian = System.Configuration.ConfigurationManager.AppSettings["yuedian"];
jiange = System.Configuration.ConfigurationManager.AppSettings["jiange"];
if (lianjieleixing == "ACCESS")
{
radioButton1.Checked = true;
}
else
{
radioButton2.Checked = true;
}
textBoxLianjie.Text = shujulianjie;
textBoxYuedianshu.Text = yuedian;
textBoxjiange.Text = jiange;
{
lianjieleixing = System.Configuration.ConfigurationManager.AppSettings["lianjieleixing"];
shujulianjie = System.Configuration.ConfigurationManager.AppSettings["shujulianjie"];
yuedian = System.Configuration.ConfigurationManager.AppSettings["yuedian"];
jiange = System.Configuration.ConfigurationManager.AppSettings["jiange"];
if (lianjieleixing == "ACCESS")
{
radioButton1.Checked = true;
}
else
{
radioButton2.Checked = true;
}
textBoxLianjie.Text = shujulianjie;
textBoxYuedianshu.Text = yuedian;
textBoxjiange.Text = jiange;
}
static byte[] PING_BUFFER = new byte[] { 0 };
static int g_nTimeout = 3000;
private void btnRun_Click(object sender, EventArgs e)
{
tracertrun();
timer1.Interval = Convert.ToInt32(jiange)*1000;
timer1.Enabled=true;
}
private void benjiIP()
{
textBoxjieguo.Text = "";
foreach (IpInfo ipf in GetIpInfo())
{
textBoxjieguo.Text += "物理地址:" + ipf.MACAddress + ";IP地址:" + ipf.IPAddress + "\r\n";
}
}
#region 获取 IP 地址信息
/// <summary>
/// 获取 IP 地址信息
/// </summary>
/// <returns></returns>
public static List<IpInfo> GetIpInfo()
{
//定义范型
List<IpInfo> ipinfos = new List<IpInfo>();
static int g_nTimeout = 3000;
private void btnRun_Click(object sender, EventArgs e)
{
tracertrun();
timer1.Interval = Convert.ToInt32(jiange)*1000;
timer1.Enabled=true;
}
private void benjiIP()
{
textBoxjieguo.Text = "";
foreach (IpInfo ipf in GetIpInfo())
{
textBoxjieguo.Text += "物理地址:" + ipf.MACAddress + ";IP地址:" + ipf.IPAddress + "\r\n";
}
}
#region 获取 IP 地址信息
/// <summary>
/// 获取 IP 地址信息
/// </summary>
/// <returns></returns>
public static List<IpInfo> GetIpInfo()
{
//定义范型
List<IpInfo> ipinfos = new List<IpInfo>();
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
try
{
if ((bool)mo["IPEnabled"] == true)
{
string mac = mo["MacAddress"].ToString();
System.Array ar = (System.Array)(mo.Properties["IpAddress"].Value);
string ip = ar.GetValue(0).ToString();
ipinfos.Add(new IpInfo(ip, mac));
}
}
catch { }
}
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
try
{
if ((bool)mo["IPEnabled"] == true)
{
string mac = mo["MacAddress"].ToString();
System.Array ar = (System.Array)(mo.Properties["IpAddress"].Value);
string ip = ar.GetValue(0).ToString();
ipinfos.Add(new IpInfo(ip, mac));
}
}
catch { }
}
return ipinfos;
}
#endregion
private void timer1_Tick(object sender, EventArgs e)
{
tracertrun();
}
private void tracertrun()
{
benjiIP();
string strip = textBoxIP.Text.Trim();
string strip0 = textBoxjieguo.Text;
if (strip0.Length > 255)
strip0 = strip0.Substring(0, 254);
if (lianjieleixing == "ACCESS")
{
try
{
if (checkBox1.Checked)
{
OleDbConnection con = new OleDbConnection(shujulianjie);
using (OleDbCommand command = new OleDbCommand("", con))
{
DateTime d = DateTime.Now;
command.CommandText = "insert into zongbiao (jianceshijian,zhujiIP,mubiaoIP) values (@jianceshijian,@zhujiIP,@mubiaoIP) ";
command.Parameters.AddWithValue("@jianceshijian", d.ToString("yyyyMMdd HH:mm:ss"));
command.Parameters.AddWithValue("@zhujiIP", strip0);
command.Parameters.AddWithValue("@mubiaoIP", strip);
con.Open();
command.ExecuteNonQuery();
command.Parameters.Clear();
command.CommandText = "select id from zongbiao where jianceshijian=@jianceshijian ";
command.Parameters.AddWithValue("@jianceshijian", d.ToString("yyyyMMdd HH:mm:ss"));
bid = command.ExecuteScalar().ToString();
con.Close();
}
}
tracert(strip);
}
catch
{
MessageBox.Show("ACCESS数据连接错误!");
return;
}
}
else
{
try
{
if (checkBox1.Checked)
{
SqlConnection con = new SqlConnection(shujulianjie);
using (SqlCommand command = new SqlCommand("", con))
{
DateTime d = DateTime.Now;
command.CommandText = "insert into zongbiao (jianceshijian,zhujiIP,mubiaoIP) values (@jianceshijian,@zhujiIP,@mubiaoIP) ";
command.Parameters.AddWithValue("@jianceshijian", d.ToString("yyyyMMdd HH:mm:ss"));
command.Parameters.AddWithValue("@zhujiIP", strip0);
command.Parameters.AddWithValue("@mubiaoIP", strip);
con.Open();
command.ExecuteNonQuery();
command.Parameters.Clear();
command.CommandText = "select id from zongbiao where jianceshijian=@jianceshijian ";
command.Parameters.AddWithValue("@jianceshijian", d.ToString("yyyyMMdd HH:mm:ss"));
bid = command.ExecuteScalar().ToString();
con.Close();
}
}
tracert(strip);
}
catch
{
MessageBox.Show("SQL数据连接错误!");
return;
}
}
}
}
#endregion
private void timer1_Tick(object sender, EventArgs e)
{
tracertrun();
}
private void tracertrun()
{
benjiIP();
string strip = textBoxIP.Text.Trim();
string strip0 = textBoxjieguo.Text;
if (strip0.Length > 255)
strip0 = strip0.Substring(0, 254);
if (lianjieleixing == "ACCESS")
{
try
{
if (checkBox1.Checked)
{
OleDbConnection con = new OleDbConnection(shujulianjie);
using (OleDbCommand command = new OleDbCommand("", con))
{
DateTime d = DateTime.Now;
command.CommandText = "insert into zongbiao (jianceshijian,zhujiIP,mubiaoIP) values (@jianceshijian,@zhujiIP,@mubiaoIP) ";
command.Parameters.AddWithValue("@jianceshijian", d.ToString("yyyyMMdd HH:mm:ss"));
command.Parameters.AddWithValue("@zhujiIP", strip0);
command.Parameters.AddWithValue("@mubiaoIP", strip);
con.Open();
command.ExecuteNonQuery();
command.Parameters.Clear();
command.CommandText = "select id from zongbiao where jianceshijian=@jianceshijian ";
command.Parameters.AddWithValue("@jianceshijian", d.ToString("yyyyMMdd HH:mm:ss"));
bid = command.ExecuteScalar().ToString();
con.Close();
}
}
tracert(strip);
}
catch
{
MessageBox.Show("ACCESS数据连接错误!");
return;
}
}
else
{
try
{
if (checkBox1.Checked)
{
SqlConnection con = new SqlConnection(shujulianjie);
using (SqlCommand command = new SqlCommand("", con))
{
DateTime d = DateTime.Now;
command.CommandText = "insert into zongbiao (jianceshijian,zhujiIP,mubiaoIP) values (@jianceshijian,@zhujiIP,@mubiaoIP) ";
command.Parameters.AddWithValue("@jianceshijian", d.ToString("yyyyMMdd HH:mm:ss"));
command.Parameters.AddWithValue("@zhujiIP", strip0);
command.Parameters.AddWithValue("@mubiaoIP", strip);
con.Open();
command.ExecuteNonQuery();
command.Parameters.Clear();
command.CommandText = "select id from zongbiao where jianceshijian=@jianceshijian ";
command.Parameters.AddWithValue("@jianceshijian", d.ToString("yyyyMMdd HH:mm:ss"));
bid = command.ExecuteScalar().ToString();
con.Close();
}
}
tracert(strip);
}
catch
{
MessageBox.Show("SQL数据连接错误!");
return;
}
}
}
private void btnStop_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
}
{
timer1.Enabled = false;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
timer1.Enabled = false;
Application.Exit();
}
{
timer1.Enabled = false;
Application.Exit();
}
public struct ICMP_PARAM
{
internal long m_nSendTicks;
internal IPAddress m_IPAddress;
internal PingOptions m_PingOptions;
}
{
internal long m_nSendTicks;
internal IPAddress m_IPAddress;
internal PingOptions m_PingOptions;
}
public void icmp_PingCompleted(object sender, PingCompletedEventArgs e)
{
ICMP_PARAM param = (ICMP_PARAM)e.UserState;
if (lianjieleixing == "ACCESS")
{
OleDbConnection con = new OleDbConnection(shujulianjie);
using (OleDbCommand command = new OleDbCommand("", con))
{
long nDeltaMS = (DateTime.Now.Ticks - param.m_nSendTicks) / TimeSpan.TicksPerMillisecond;
string strt = ((e.Reply.Status == IPStatus.TimedOut) ? "*" : nDeltaMS.ToString() + "毫秒");
textBoxjieguo.Text += "越点" + param.m_PingOptions.Ttl.ToString() + "," + strt + "," + e.Reply.Address.ToString() + "\r\n";
command.CommandText = "insert into jilu (bid,xuhao,fanhuishijian,ip,zhu) values (@bid,@xuhao,@fanhuishijian,@ip,@zhu) ";
command.Parameters.AddWithValue("@bid", bid);
command.Parameters.AddWithValue("@xuhao", param.m_PingOptions.Ttl);
command.Parameters.AddWithValue("@fanhuishijian", strt);
command.Parameters.AddWithValue("@ip", e.Reply.Address.ToString());
if (param.m_IPAddress.Equals(e.Reply.Address))
{
textBoxjieguo.Text += ",已到达目标地址!\r\n";
if (checkBox1.Checked)
{
command.Parameters.AddWithValue("@zhu", "已到达目标地址!");
con.Open();
command.ExecuteNonQuery();
con.Close();
}
return;
}
{
ICMP_PARAM param = (ICMP_PARAM)e.UserState;
if (lianjieleixing == "ACCESS")
{
OleDbConnection con = new OleDbConnection(shujulianjie);
using (OleDbCommand command = new OleDbCommand("", con))
{
long nDeltaMS = (DateTime.Now.Ticks - param.m_nSendTicks) / TimeSpan.TicksPerMillisecond;
string strt = ((e.Reply.Status == IPStatus.TimedOut) ? "*" : nDeltaMS.ToString() + "毫秒");
textBoxjieguo.Text += "越点" + param.m_PingOptions.Ttl.ToString() + "," + strt + "," + e.Reply.Address.ToString() + "\r\n";
command.CommandText = "insert into jilu (bid,xuhao,fanhuishijian,ip,zhu) values (@bid,@xuhao,@fanhuishijian,@ip,@zhu) ";
command.Parameters.AddWithValue("@bid", bid);
command.Parameters.AddWithValue("@xuhao", param.m_PingOptions.Ttl);
command.Parameters.AddWithValue("@fanhuishijian", strt);
command.Parameters.AddWithValue("@ip", e.Reply.Address.ToString());
if (param.m_IPAddress.Equals(e.Reply.Address))
{
textBoxjieguo.Text += ",已到达目标地址!\r\n";
if (checkBox1.Checked)
{
command.Parameters.AddWithValue("@zhu", "已到达目标地址!");
con.Open();
command.ExecuteNonQuery();
con.Close();
}
return;
}
if (param.m_PingOptions.Ttl >= Convert.ToInt32(yuedian))
{
textBoxjieguo.Text += ",已达到最大跃点计数!\r\n";
if (checkBox1.Checked)
{
command.Parameters.AddWithValue("@zhu", "已达到最大跃点计数!");
con.Open();
command.ExecuteNonQuery();
con.Close();
}
return;
}
if (checkBox1.Checked)
{
command.Parameters.AddWithValue("@zhu", "");
con.Open();
command.ExecuteNonQuery();
con.Close();
}
//
}
}
else
{
SqlConnection con = new SqlConnection(shujulianjie);
using (SqlCommand command = new SqlCommand("", con))
{
long nDeltaMS = (DateTime.Now.Ticks - param.m_nSendTicks) / TimeSpan.TicksPerMillisecond;
string strt = ((e.Reply.Status == IPStatus.TimedOut) ? "*" : nDeltaMS.ToString() + "毫秒");
textBoxjieguo.Text += "越点" + param.m_PingOptions.Ttl.ToString() + "," + strt + "," + e.Reply.Address.ToString() + "\r\n";
command.CommandText = "insert into jilu (bid,xuhao,fanhuishijian,ip,zhu) values (@bid,@xuhao,@fanhuishijian,@ip,@zhu) ";
command.Parameters.AddWithValue("@bid", bid);
command.Parameters.AddWithValue("@xuhao", param.m_PingOptions.Ttl);
command.Parameters.AddWithValue("@fanhuishijian", strt);
command.Parameters.AddWithValue("@ip", e.Reply.Address.ToString());
if (param.m_IPAddress.Equals(e.Reply.Address))
{
textBoxjieguo.Text += ",已到达目标地址!\r\n";
if (checkBox1.Checked)
{
command.Parameters.AddWithValue("@zhu", "已到达目标地址!");
con.Open();
command.ExecuteNonQuery();
con.Close();
}
return;
}
{
textBoxjieguo.Text += ",已达到最大跃点计数!\r\n";
if (checkBox1.Checked)
{
command.Parameters.AddWithValue("@zhu", "已达到最大跃点计数!");
con.Open();
command.ExecuteNonQuery();
con.Close();
}
return;
}
if (checkBox1.Checked)
{
command.Parameters.AddWithValue("@zhu", "");
con.Open();
command.ExecuteNonQuery();
con.Close();
}
//
}
}
else
{
SqlConnection con = new SqlConnection(shujulianjie);
using (SqlCommand command = new SqlCommand("", con))
{
long nDeltaMS = (DateTime.Now.Ticks - param.m_nSendTicks) / TimeSpan.TicksPerMillisecond;
string strt = ((e.Reply.Status == IPStatus.TimedOut) ? "*" : nDeltaMS.ToString() + "毫秒");
textBoxjieguo.Text += "越点" + param.m_PingOptions.Ttl.ToString() + "," + strt + "," + e.Reply.Address.ToString() + "\r\n";
command.CommandText = "insert into jilu (bid,xuhao,fanhuishijian,ip,zhu) values (@bid,@xuhao,@fanhuishijian,@ip,@zhu) ";
command.Parameters.AddWithValue("@bid", bid);
command.Parameters.AddWithValue("@xuhao", param.m_PingOptions.Ttl);
command.Parameters.AddWithValue("@fanhuishijian", strt);
command.Parameters.AddWithValue("@ip", e.Reply.Address.ToString());
if (param.m_IPAddress.Equals(e.Reply.Address))
{
textBoxjieguo.Text += ",已到达目标地址!\r\n";
if (checkBox1.Checked)
{
command.Parameters.AddWithValue("@zhu", "已到达目标地址!");
con.Open();
command.ExecuteNonQuery();
con.Close();
}
return;
}
if (param.m_PingOptions.Ttl >= Convert.ToInt32(yuedian))
{
textBoxjieguo.Text += ",已达到最大跃点计数!\r\n";
if (checkBox1.Checked)
{
command.Parameters.AddWithValue("@zhu", "已达到最大跃点计数!");
con.Open();
command.ExecuteNonQuery();
con.Close();
}
return;
}
if (checkBox1.Checked)
{
command.Parameters.AddWithValue("@zhu", "");
con.Open();
command.ExecuteNonQuery();
con.Close();
}
//
}
}
Ping icmp = (Ping)sender;
param.m_PingOptions.Ttl++;
param.m_nSendTicks = DateTime.Now.Ticks;
icmp.SendAsync(param.m_IPAddress, g_nTimeout, PING_BUFFER, param.m_PingOptions, param);
return ;
}
private void tracert(string szDomain)
{
ICMP_PARAM param = new ICMP_PARAM();
param.m_PingOptions = new PingOptions(1, false);
if (!IPAddress.TryParse(szDomain, out param.m_IPAddress))
{
// 解析域名
try
{
Regex regEx = new Regex("\\d+\\.\\d+\\.\\d+\\.\\d+");
IPHostEntry hostEntry = Dns.GetHostEntry(szDomain);
foreach (IPAddress ipAddr in hostEntry.AddressList)
{
if (regEx.IsMatch(ipAddr.ToString()))
{
param.m_IPAddress = ipAddr;
break;
}
}
{
textBoxjieguo.Text += ",已达到最大跃点计数!\r\n";
if (checkBox1.Checked)
{
command.Parameters.AddWithValue("@zhu", "已达到最大跃点计数!");
con.Open();
command.ExecuteNonQuery();
con.Close();
}
return;
}
if (checkBox1.Checked)
{
command.Parameters.AddWithValue("@zhu", "");
con.Open();
command.ExecuteNonQuery();
con.Close();
}
//
}
}
Ping icmp = (Ping)sender;
param.m_PingOptions.Ttl++;
param.m_nSendTicks = DateTime.Now.Ticks;
icmp.SendAsync(param.m_IPAddress, g_nTimeout, PING_BUFFER, param.m_PingOptions, param);
return ;
}
private void tracert(string szDomain)
{
ICMP_PARAM param = new ICMP_PARAM();
param.m_PingOptions = new PingOptions(1, false);
if (!IPAddress.TryParse(szDomain, out param.m_IPAddress))
{
// 解析域名
try
{
Regex regEx = new Regex("\\d+\\.\\d+\\.\\d+\\.\\d+");
IPHostEntry hostEntry = Dns.GetHostEntry(szDomain);
foreach (IPAddress ipAddr in hostEntry.AddressList)
{
if (regEx.IsMatch(ipAddr.ToString()))
{
param.m_IPAddress = ipAddr;
break;
}
}
if (param.m_IPAddress == null)
{
MessageBox.Show("地址输入错误!");
return;
}
{
MessageBox.Show("地址输入错误!");
return;
}
textBoxjieguo.Text+= "正在跟踪到[" + szDomain+"]"+ param.m_IPAddress.ToString() + "间的路由:\r\n";
}
catch (Exception ex)
{
textBoxjieguo.Text +=ex.ToString();
return;
}
}
else
{
textBoxjieguo.Text += "正在跟踪到" + param.m_IPAddress.ToString() + "间的路由:\r\n";
}
}
catch (Exception ex)
{
textBoxjieguo.Text +=ex.ToString();
return;
}
}
else
{
textBoxjieguo.Text += "正在跟踪到" + param.m_IPAddress.ToString() + "间的路由:\r\n";
}
Ping icmp = new Ping();
icmp.PingCompleted += new PingCompletedEventHandler(icmp_PingCompleted);
icmp.PingCompleted += new PingCompletedEventHandler(icmp_PingCompleted);
param.m_nSendTicks = DateTime.Now.Ticks;
icmp.SendAsync(param.m_IPAddress, g_nTimeout, PING_BUFFER, param.m_PingOptions, param);
}
icmp.SendAsync(param.m_IPAddress, g_nTimeout, PING_BUFFER, param.m_PingOptions, param);
}