C# ping命令的实现方法:Ping类的使用
- 本文介绍C# ping命令的实现方法,首先利用原始Socket套接字,实现ICMP协议,然后执行ping命令。最后,还可以使用C# 2.0中新增的Ping类来实现。
以下介绍C# ping命令的两种实现方法。
C# ping命令实现:利用原始Socket套接字,实现ICMP协议。
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Net;
- using System.Net.Sockets;
- namespace PingC
- {
- class ping
- {
- const int SOCKET_ERROR = -1;
- const int ICMP_ECHO = 8;
- static void Main(string[] args)
- {
- ping p = new ping();
- Console.WriteLine("请输入要 Ping 的IP或者主机名字:");
- string MyUrl = Console.ReadLine();
- Console.WriteLine("正在 Ping " + MyUrl + " ……");
- Console.Write(p.PingHost(MyUrl));
- }
- public string PingHost(string host)
- {
- // 声明 IPHostEntry
- IPHostEntry ServerHE, fromHE;
- int nBytes = 0;
- int dwStart = 0, dwStop = 0;
- //初始化ICMP的Socket
- Socket socket =
- new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);
- socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 1000);
- // 得到Server EndPoint
- try
- {
- ServerHE = Dns.GetHostByName(host);
- }
- catch (Exception)
- {
- return "没有发现主机";
- }
- // 把 Server IP_EndPoint转换成EndPoint
- IPEndPoint ipepServer = new IPEndPoint(ServerHE.AddressList[0], 0);
- EndPoint epServer = (ipepServer);
- // 设定客户机的接收Endpoint
- fromHE = Dns.GetHostByName(Dns.GetHostName());
- IPEndPoint ipEndPointFrom = new IPEndPoint(fromHE.AddressList[0], 0);
- EndPoint EndPointFrom = (ipEndPointFrom);
- int PacketSize = 0;
- IcmpPacket packet = new IcmpPacket();
- // 构建要发送的包
- packet.Type = ICMP_ECHO; //8
- packet.SubCode = 0;
- packet.CheckSum =0;
- packet.Identifier = 45;
- packet.SequenceNumber = 0;
- int PingData = 24; // sizeof(IcmpPacket) - 8;
- packet.Data = new Byte[PingData];
- // 初始化Packet.Data
- for (int i = 0; i < PingData; i++)
- {
- packet.Data[i] = (byte)'#';
- }
- //Variable to hold the total Packet size
- PacketSize = 32;
- Byte[] icmp_pkt_buffer = new Byte[PacketSize];
- Int32 Index = 0;
- //again check the packet size
- Index = Serialize(
- packet,
- icmp_pkt_buffer,
- PacketSize,
- PingData);
- //if there is a error report it
- if (Index == -1)
- {
- return "Error Creating Packet";
- }
- // convert into a UInt16 array
- //Get the Half size of the Packet
- Double double_length = Convert.ToDouble(Index);
- Double dtemp = Math.Ceiling(double_length / 2);
- int cksum_buffer_length = Index/2;
- //Create a Byte Array
- UInt16[] cksum_buffer = new UInt16[cksum_buffer_length];
- //Code to initialize the Uint16 array
- int icmp_header_buffer_index = 0;
- for (int i = 0; i < cksum_buffer_length; i++)
- {
- cksum_buffer[i] =
- BitConverter.ToUInt16(icmp_pkt_buffer, icmp_header_buffer_index);
- icmp_header_buffer_index += 2;
- }
- //Call a method which will return a checksum
- UInt16 u_cksum = checksum(cksum_buffer, cksum_buffer_length);
- //Save the checksum to the Packet
- packet.CheckSum = u_cksum;
- // Now that we have the checksum, serialize the packet again
- Byte[] sendbuf = new Byte[PacketSize];
- //again check the packet size
- Index = Serialize(
- packet,
- sendbuf,
- PacketSize,
- PingData);
- //if there is a error report it
- if (Index == -1)
- {
- return "Error Creating Packet";
- }
- dwStart = System.Environment.TickCount; // Start timing
- //send the Packet over the socket
- if ((nBytes = socket.SendTo(sendbuf, PacketSize, 0, epServer)) == SOCKET_ERROR)
- {
- return "Socket Error: cannot send Packet";
- }
- // Initialize the buffers. The receive buffer is the size of the
- // ICMP header plus the IP header (20 bytes)
- Byte[] ReceiveBuffer = new Byte[256];
- nBytes = 0;
- //Receive the bytes
- bool recd = false;
- int timeout = 0;
- //loop for checking the time of the server responding
- while (!recd)
- {
- nBytes = socket.ReceiveFrom(ReceiveBuffer, 256, 0, ref EndPointFrom);
- if (nBytes == SOCKET_ERROR)
- {
- return "主机没有响应";
- }
- else if (nBytes > 0)
- {
- dwStop = System.Environment.TickCount - dwStart; // stop timing
- return "Reply from " + epServer.ToString() + " in "
- + dwStop + "ms. Received: " + nBytes + " Bytes.";
- }
- timeout = System.Environment.TickCount - dwStart;
- if (timeout > 1000)
- {
- return "超时";
- }
- }
- //close the socket
- socket.Close();
- return "";
- }
- /// < summary>
- /// This method get the Packet and calculates the total size
- /// of the Pack by converting it to byte array
- /// < /summary>
- public static Int32 Serialize(IcmpPacket packet, Byte[] Buffer,
- Int32 PacketSize, Int32 PingData)
- {
- Int32 cbReturn = 0;
- // serialize the struct into the array
- int Index = 0;
- Byte[] b_type = new Byte[1];
- b_type[0] = (packet.Type);
- Byte[] b_code = new Byte[1];
- b_code[0] = (packet.SubCode);
- Byte[] b_cksum = BitConverter.GetBytes(packet.CheckSum);
- Byte[] b_id = BitConverter.GetBytes(packet.Identifier);
- Byte[] b_seq = BitConverter.GetBytes(packet.SequenceNumber);
- Array.Copy(b_type, 0, Buffer, Index, b_type.Length);
- Index += b_type.Length;
- Array.Copy(b_code, 0, Buffer, Index, b_code.Length);
- Index += b_code.Length;
- Array.Copy(b_cksum, 0, Buffer, Index, b_cksum.Length);
- Index += b_cksum.Length;
- Array.Copy(b_id, 0, Buffer, Index, b_id.Length);
- Index += b_id.Length;
- Array.Copy(b_seq, 0, Buffer, Index, b_seq.Length);
- Index += b_seq.Length;
- // copy the data
- Array.Copy(packet.Data, 0, Buffer, Index, PingData);
- Index += PingData;
- if (Index != PacketSize/* sizeof(IcmpPacket) */)
- {
- cbReturn = -1;
- return cbReturn;
- }
- cbReturn = Index;
- return cbReturn;
- }
- /// < summary>
- /// This Method has the algorithm to make a checksum
- /// < /summary>
- public static UInt16 checksum(UInt16[] buffer, int size)
- {
- Int32 cksum = 0;
- int counter;
- counter = 0;
- while (size > 0)
- {
- UInt16 val = buffer[counter];
- cksum += buffer[counter];
- counter += 1;
- size -= 1;
- }
- cksum = (cksum >> 16) + (cksum & 0xffff);
- cksum += (cksum >> 16);
- return (UInt16)(~cksum);
- }
- }
- /// 类结束
- /// < summary>
- /// Class that holds the Pack information
- /// < /summary>
- public class IcmpPacket
- {
- public Byte Type; // type of message
- public Byte SubCode; // type of sub code
- public UInt16 CheckSum; // ones complement checksum of struct
- public UInt16 Identifier; // identifier
- public UInt16 SequenceNumber; // sequence number
- public Byte[] Data;
- } // class IcmpPacket
- }
C# ping命令执行:执行ping命令
首先,我们用使用Process类,来创建独立的进程,导入System.Diagnostics,
using System.Diagnostics;
实例一个Process类,启动一个独立进程
Process p = new Process();
Process类有一个StartInfo属性,这个是ProcessStartInfo类,包括了一些属性和方法,
下面我们用到了他的几个属性:
设定程序名
p.StartInfo.FileName = "cmd.exe";
关闭Shell的使用
p.StartInfo.UseShellExecute = false;
重定向标准输入
p.StartInfo.RedirectStandardInput = true;
重定向标准输出
p.StartInfo.RedirectStandardOutput = true;
重定向错误输出
p.StartInfo.RedirectStandardError = true;
设置不显示窗口
p.StartInfo.CreateNoWindow = true;
上面几个属性的设置是比较关键的一步。
既然都设置好了那就启动进程吧,
p.Start();
输入要执行的命令,这里就是ping了,
p.StandardInput.WriteLine("ping -n 1 www.iwebtrados.com.cn");
p.StandardInput.WriteLine("exit");
从输出流获取命令执行结果,
string strRst = p.StandardOutput.ReadToEnd();
C# ping命令实现:利用c#2.0新增的Ping类
这里我写的是一个窗体程序。首先添加textbox,listbox,button控件,其中textbox录入域名或IP,listbox显示结果.
在button1_click事件键入
- private void button1_Click(object sender, EventArgs e)
- {
- Ping p1 = new Ping(); //只是演示,没有做错误处理
- PingReply reply = p1.Send(this.textBox1.Text);//阻塞方式
- displayReply(reply); //显示结果
- }
- private void displayReply(PingReply reply) //显示结果
- {
- 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));
- listBox1.Items.Add(sbuilder.ToString());
- }
- }
也可以做异步的处理,修改button1_click,并添加PingCompletedCallBack方法
- private void button1_Click(object sender, EventArgs e)
- {
- Ping p1 = new Ping();
- p1.PingCompleted += new PingCompletedEventHandler(this.PingCompletedCallBack);//设置PingCompleted事件处理程序
- p1.SendAsync(this.textBox1.Text, null);
- }
- private void PingCompletedCallBack(object sender, PingCompletedEventArgs e)
- {
- if (e.Cancelled)
- {
- listBox1.Items.Add("Ping Canncel");
- return;
- }
- if (e.Error != null)
- {
- listBox1.Items.Add(e.Error.Message);
- return;
- }
- StringBuilder sbuilder;
- PingReply reply = e.Reply;
- 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));
- listBox1.Items.Add(sbuilder.ToString());
- }
- }
怎么样,相比1、2方式,3是不是简单的多呀。
本文来自网络小筑的博客:《c#下实现ping操作》。
C# ping命令的实现方法:Ping类的使用相关推荐
- 计算机网络中ping命令的使用方法,ping命令的基础使用技巧
在电脑基础操作中,很多人在操作电脑中可以接触到这个ping命令.但是都是没有深入的去了解这个ping命令.对于ping命令在电脑中可以通过ping来判断及解决网络故障.我们每天都接触电脑使用互联网的人 ...
- 响应因特网端口ping命令_如何使用Ping命令识别基本的Internet问题
响应因特网端口ping命令 Next time you call your help desk, do you want to wow them with your networking knowle ...
- ping命令简单介绍+用ping简单测网速
ping命令简单介绍+用ping简单测网速 ping可以用来检查网络是否通畅或者网络连接速度,而作为网络管理员ping命令是第一个必须掌握的DOS命令.我们可以用ping来给网络上指定IP的计算机发送 ...
- 【无标题】使用Ping命令制作的批量Ping工具,可对指定Ip进行ping测试,并将结果记录到txt文件
//使用Ping命令制作的批量Ping工具,可对指定Ip进行ping测试,并将结果记录到txt文件 直接闪退出去了,不知道问题出在哪里. //代码: @if "%~1"==&quo ...
- php ping 命令注入,CTF关于ping命令注入问题
题目样式 对于看到ping或者ping命令却没有弄waf时就要想到命令注入. 具体注入方法 看到ping命令就可以利用截断来执行新的命令. 首先测试所有的截断符号: '$' ';' '|' '-' ' ...
- 全面掌握ping命令(四)ping命令常用参数
参数可以对命令的功能进行扩展,ping命令的参数比较多,常用的主要有以下几个: (1)ping IP地址 –t 连续不停对IP地址发送ICMP数据包,直到被用户以Ctrl+C中断. 如:ping 19 ...
- 全面掌握ping命令(三) ping命令防火墙设置
Ping命令利用ICMP协议工作,ICMP是一个比较复杂的协议,功能强大,也经常被黑客利用来攻击网络上的路由器和主机,所以目前的很多网络设备或防火墙都提供了禁用ICMP协议的功能.如Windows系统 ...
- ping网站服务器,ping命令的作用,ping真能检测出服务器的快慢吗??
卖服务器的时候总有客户问给个ip来ping下看看快不快~~,ping真的能测试出快不快吗? Ping是Windows系列自带的一个可执行命令.对于站长来说,利用它可以检查服务器网络是否能够连通,可以帮 ...
- python调用ping命令_python调用系统命令ping
#! /usr/bin/env python #coding=utf-8 ############# import subprocess import time import os ks=int(ti ...
最新文章
- safari浏览器横屏怎么设置_Safari浏览器的几个小技巧你掌握了吗?
- 用html编写ASCII表,[html_css]ASCII编码表
- CVE-2016-1779技术分析及其背后的故事
- Jmeter简介以及简单模拟性能测试
- PMCAFF产品经理第一课 | 「在行」价值1.5万元的强大课程体系,365天能力突围
- Fegion-4解决Fegion第一次请求timeout的问题
- SystemC 代码添加和测试方法
- 深入java虚拟机需要读吗_《深入理解Java虚拟机》读后总结(一)JVM内存模型
- 思维方式是看待事物的角度、方式和方法,它对人的言行起到决定性作用
- 【转载】素数快速打表(据说是线性复杂度)
- ubuntu skill
- DDD领域驱动设计:四层架构应用
- 力学流体simple
- 计算机win7开超级性能模式,win7系统设置最高性能的操作方法
- 集合框架--集合框架体系概述
- 芯片设计:verilog断言(SVA)语法
- 大OA核心——工作流系统(引擎)
- 服务器omv系统,在Debian上安装OpenMediaVault开源NAS系统
- 从one hot vector到Attention, Bert——NLP基本思想串连回顾
- 微机原理与接口技术 重点详解与章节总结——8086微处理器系统结构