資源簡介
C#利用SharpPcap發送以太網報文(以太網原始幀),軟件基本流程:從文件讀取要發送的幀內容,然后找到電腦上的以太網卡,接下來利用SharpPcap來循環發送。
vs2008工程,全部源代碼,可編譯和測試。
以太網幀內容可以自己構造任意的幀類型和內容;也可以使用wireshark抓包,然后利用本軟件來重放。
wireshark抓包后取數據方法:在wireshark的收到的幀上點右鍵,選擇 復制 -> 為一個HEX流,然后粘貼到一個空文件內,命名為packet.dat,這樣本軟件就會讀取packet.dat內的內容并循環發送了。
vs2008工程,全部源代碼,可編譯和測試。
以太網幀內容可以自己構造任意的幀類型和內容;也可以使用wireshark抓包,然后利用本軟件來重放。
wireshark抓包后取數據方法:在wireshark的收到的幀上點右鍵,選擇 復制 -> 為一個HEX流,然后粘貼到一個空文件內,命名為packet.dat,這樣本軟件就會讀取packet.dat內的內容并循環發送了。

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
using?SharpPcap.LibPcap;
using?SharpPcap.WinPcap;
using?SharpPcap;
using?System.Net.Sockets;
using?System.Collections;
using?System.Net.NetworkInformation;
using?System.Threading;
using?System.IO;
namespace?LldpSend
{
????class?Program
????{
????????static?string?loadDevice()//?獲取網卡方法
????????{
????????????NetworkInterface[]?adapters?=?NetworkInterface.GetAllNetworkInterfaces();
????????????string?netinterface?=?null;
????????????try
????????????{
????????????????foreach?(NetworkInterface?adap?in?adapters)
????????????????{
????????????????????if?(adap.NetworkInterfaceType?==?NetworkInterfaceType.Ethernet)
????????????????????{
????????????????????????Console.Out.WriteLine(“netport?name:“?+?adap.Name);
????????????????????????netinterface?=?adap.Name;//保存網口
????????????????????????//device?=?i;//找到的第一個網口
????????????????????????//deviceMac?=?adap.GetPhysicalAddress().ToString();//得到設備mac,后續設置iP地址等信息的時候需要
????????????????????????break;
????????????????????}
????????????????}
????????????}
????????????catch?(Exception?ex)
????????????{
????????????????Console.Out.WriteLine(“錯誤:初始化沒有找到以太網卡“);
????????????????Console.Out.WriteLine(ex.Message?+?“\n“?+?ex.StackTrace);
????????????}
????????????return?netinterface;
????????}
????????static?NetworkInterface?getNetworkDevice(string?name)//?獲取指定網卡方法
????????{
????????????NetworkInterface[]?adapters?=?NetworkInterface.GetAllNetworkInterfaces();
????????????NetworkInterface?netinterface?=?null;
????????????try
????????????{
????????????????foreach?(NetworkInterface?adap?in?adapters)
????????????????{
????????????????????if?(adap.NetworkInterfaceType?==?NetworkInterfaceType.Ethernet)
????????????????????{
????????????????????????if?(adap.Name.Equals(name))
????????????????????????{
????????????????????????????Console.Out.WriteLine(“netport?name:“?+?adap.Name);
????????????????????????????netinterface?=?adap;//保存網口
????????????????????????????break;
????????????????????????}
????????????????????}
????????????????}
????????????}
????????????catch?(Exception?ex)
????????????{
????????????????Console.Out.WriteLine(“錯誤:初始化沒有找到以太網卡,“?+?ex.Message);
????????????????Console.Out.WriteLine(ex.Message?+?“\n“?+?ex.StackTrace);
????????????}
????????????return?netinterface;
????????}
????????static?string?read_packet_file(string?filename)
????????{
????????????//文件路徑
????????????string?filePath?=?filename;
????????????try
????????????{
????????????????if?(File.Exists(filePath))
????????????????{
????????????????????string?str?=?File.ReadAllText(filePath);
????????????????????return?str;
????????????????}
????????????????else
????????????????{
????????????????????return?null;
????????????????}
????????????}
????????????catch?(Exception?ex)
????????????{
????????????????Console.WriteLine(“讀取幀文件失敗:“+ex.Message);
????????????????return?null;
??????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
????..A..H.??????7168??2020-01-13?12:29??LldpSend.suo
?????文件???????8192??2020-01-13?12:50??LldpSend\bin\Debug\LldpSend.exe
?????文件??????17920??2020-01-13?12:50??LldpSend\bin\Debug\LldpSend.pdb
?????文件??????14328??2020-01-13?12:51??LldpSend\bin\Debug\LldpSend.vshost.exe
?????文件????????490??2009-06-11?05:14??LldpSend\bin\Debug\LldpSend.vshost.exe.manifest
?????文件????????723??2020-01-13?12:50??LldpSend\bin\Debug\packet.dat
?????文件?????196608??2013-01-14?19:59??LldpSend\bin\Debug\PacketDotNet.dll
?????文件?????476563??2013-01-14?19:59??LldpSend\bin\Debug\PacketDotNet.xm
?????文件??????72704??2013-01-14?19:59??LldpSend\bin\Debug\SharpPcap.dll
?????文件?????186730??2013-01-14?19:59??LldpSend\bin\Debug\SharpPcap.xm
?????文件?????196608??2013-01-14?19:59??LldpSend\dll\PacketDotNet.dll
?????文件?????476563??2013-01-14?19:59??LldpSend\dll\PacketDotNet.xm
?????文件??????72704??2013-01-14?19:59??LldpSend\dll\SharpPcap.dll
?????文件????????492??2013-01-14?19:59??LldpSend\dll\SharpPcap.dll.config
?????文件?????186730??2013-01-14?19:59??LldpSend\dll\SharpPcap.xm
?????文件???????2965??2020-01-13?12:47??LldpSend\LldpSend.csproj
?????文件????????496??2020-01-13?12:51??LldpSend\obj\Debug\LldpSend.csproj.FileListAbsolute.txt
?????文件???????8192??2020-01-13?12:50??LldpSend\obj\Debug\LldpSend.exe
?????文件??????17920??2020-01-13?12:50??LldpSend\obj\Debug\LldpSend.pdb
?????文件???????6033??2020-01-13?12:47??LldpSend\obj\Debug\ResolveAssemblyReference.cache
?????文件???????7175??2020-01-13?12:50??LldpSend\Program.cs
?????文件???????1366??2020-01-13?12:29??LldpSend\Properties\AssemblyInfo.cs
?????文件????????914??2020-01-13?12:29??LldpSend.sln
?????目錄??????????0??2020-01-13?12:29??LldpSend\obj\Debug\TempPE
?????目錄??????????0??2020-01-13?12:56??LldpSend\bin\Debug
?????目錄??????????0??2020-01-13?12:50??LldpSend\obj\Debug
?????目錄??????????0??2020-01-13?12:29??LldpSend\bin
?????目錄??????????0??2020-01-13?12:47??LldpSend\dll
?????目錄??????????0??2020-01-13?12:29??LldpSend\obj
?????目錄??????????0??2020-01-13?12:29??LldpSend\Properties
............此處省略4個文件信息
評論
共有 條評論