C# 使用ping命令

方法一:调用cmd 的ping命令

private static string CmdPing(string strIp)

{

Process p = new Process(); p.StartInfo.FileName = "cmd.exe";//设定程序名

p.StartInfo.UseShellExecute = false; //关闭Shell的使用

p.StartInfo.RedirectStandardInput = true;//重定向标准输入

p.StartInfo.RedirectStandardOutput = true;//重定向标准输出

p.StartInfo.RedirectStandardError = true;//重定向错误输出

p.StartInfo.CreateNoWindow = true;//设置不显示窗口

string pingrst; p.Start(); p.StandardInput.WriteLine("ping " + strIp);

p.StandardInput.WriteLine("exit");

string strRst = p.StandardOutput.ReadToEnd();

if (strRst.IndexOf("(0% loss)") != -1)

{

pingrst = "连接";

}

else if (strRst.IndexOf("Destination host unreachable.") != -1)

{

pingrst = "无法到达目的主机";

}

else if (strRst.IndexOf("Request timed out.") != -1)

{

pingrst = "超时";

}

else if (strRst.IndexOf("Unknown host") != -1)

{

pingrst = "无法解析主机";

}

else

{

pingrst = strRst;

}

p.Close();

return pingrst;

}

方法二:使用C#中的ping 类

private void displayReply(PingReply reply) //显示结果

  {

Ping p1 = new Ping(); //只是演示,没有做错误处理

  PingReply reply = p1.Send("填写ip地址");

  StringBuilder sbuilder ;

  if (reply.Status == IPStatus.Success)

  {

  sbuilder = new StringBuilder();

  sbuilder.Append(string.Format("Address: {0} ", reply.Address.ToString ()));

  sbuilder.Append(string.Format("RoundTrip time: {0} ", reply.RoundtripTime));

  sbuilder.Append(string.Format("Time to live: {0} ", reply.Options.Ttl));

  sbuilder.Append(string.Format("Don‘t fragment: {0} ", reply.Options.DontFragment));

  sbuilder.Append(string.Format("Buffer size: {0} ", reply.Buffer.Length));

  response.write(sbuilder.ToString());

  }

  else if (reply.Status == IPStatus.TimeOut)  

{

response.write("超时");

}else{

response.write("失败");

}

}

C# 使用ping命令

时间: 2024-07-30 20:13:16

C# 使用ping命令的相关文章

docker run常用命令及 解决 ubuntu镜像无法识别 ifconfig ping 命令

docker run -it     docker 前端启动 container容器           -d             后端启动 container容器           -p             固定端口映射            -P             不固定端口映射           --name         给生成的容器起名字docker ps:默认显示正在运行的container       ps -a 显示所有的container容器docker r

Docker的ubuntu镜像安装的容器无ifconfig和ping命令的解决

Docker的Ubuntu镜像安装的容器无ifconfig命令和ping命令 解决: apt-get update apt install net-tools       # ifconfig apt install iputils-ping     # ping

[小菜随笔]python tkinter实现简单的ping命令

本文主要是介绍python图形界面上的按键与实际功能的对接,其实编程掌握了基础之后的学习应该都是靠自己去挖掘其他的 在网上发现多半教程都是2的,故本文使用的是python3.5,其实也没什么区别,就有一些支持库改变而已 首先我们使用python去编写一个简单的窗口程序,带一个按键,如下图 from tkinter import * #导入tk包 import os #导入os包,方便执行cmd命令,网上还有其他方法执行 test = Tk() #创建一个窗口程序 test.title('ping

Swift和C混合Socket编程实现简单的ping命令

这个是用Mac下的Network Utility工具实现ping命令,用Wireshark抓取的ICMP数据包: 发送ICMP数据包内容 接受ICMP数据包内容 一.icmp结构 要真正了解ping命令实现原理,就要了解ping命令所使用到的TCP/IP协议.ICMP(Internet Control Message,网际控制报文协议)是为网关和目标主机而提供的一种差错控制机制,使它们在遇到差错时能把错误报告给报文源发方.ICMP协议是IP层的 一个协议,但是由于差错报告在发送给报文源发方时可能

ping命令扫描局域网内的主机

linux: 禁ping vim /proc/sys/net/ipv4/icmp_echo_ignore_all 0 代表允许 1代表禁止 ping.sh #!/bin/bash for i in {2..5}; do   host=122.152.172.$i  ping -c2  $host  >/dev/null  if [ $? = 0 ]  then      echo "122.152.172.$i is connected"  else  echo "12

每天一个linux命令(54):ping命令

Linux系统的ping命令是常用的网络命令,它通常用来测试与目标主机的连通性,我们经常会说“ping一下某机器,看是不是开着”.不能打开网页时会说“你先ping网关地址192.168.1.1试试”.它通过发送ICMP ECHO_REQUEST数据包到网络主机(send ICMP ECHO_REQUEST to network hosts),并显示响应情况,这样我们就可以根据它输出的信息来确定目标主机是否可访问(但这不是绝对的).有些服务器为了防止通过ping探测到,通过防火墙设置了禁止ping

C#中的Ping命令

1.Ping命令基础知识 在网络中Ping 命令是一个十分好用的TCP/IP工具,它主要的功能是用来检测网络的连通情况和分析网络速度.我们可以用ping来给网络上指定IP的计算机发送一个数据包,而对方就要返回一个同样大小的数据包,根据返回的数据包就可以判定对方机器的存在. 在命令提示符中输入Ping /?,可以看到ping的所有参数: ping [-t] [-a] [-n count] [-l length] [-f] [-i ttl] [-v tos] [-r count] [-s count

Linux和Windows下ping命令详解

转:http://linux.chinaitlab.com/command/829332.html 一.Linux下的ping参数 用途 发送一个回送信号请求给网络主机. 语法 ping [ -d] [ -D ] [ -n ] [ -q ] [ -r] [ -v] [ \ -R ] [ -a addr_family ] [ -c Count ] [ -w timeout ] [ -f | -i \ Wait ] [ -l Preload ] [ -p Pattern ] [ -s PacketS

ssh命令、ping命令、traceroute 命令所使用的协议

在Node reboot or eviction: How to check if yourprivate interconnect CRS can transmit network heartbeats (文档 ID 1445075.1)中有这么一句话: The script in here performs the network connectivity check usingssh. This check complements ping or traceroute since ssh

PING命令入门详解(转载)

本文转自http://www.linkwan.com/gb/tech/htm/928.htm 1.Ping的基础知识 ping命令相信大家已经再熟悉不过了,但是能把ping的功能发挥到最大的人却并不是很多,当然我也并不是说我可以让ping发挥最大的功能,我也只不过经常用ping这个工具,也总结了一些小经验,现在和大家分享一下. Ping是潜水艇人员的专用术语,表示回应的声纳脉冲,在网络中Ping 是一个十分好用的TCP/IP工具.它主要的功能是用来检测网络的连通情况和分析网络速度. Ping有好