ARP扫描局域网主机ip和MACc#Word文档下载推荐.docx
《ARP扫描局域网主机ip和MACc#Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《ARP扫描局域网主机ip和MACc#Word文档下载推荐.docx(14页珍藏版)》请在冰点文库上搜索。
速度和稳定性,需要一个小技巧。
原理很简单:
一个网段的IP总是连续的。
利用这个原理
可以大大简化计算。
举例如下:
本机的IP是10.128.9.230,子网掩码为255.255.252.0;
那么本网段的可用IP范围是10.128.8.0---10.128.11.255。
这里面的规律是很有趣的,大
家可以算算看。
第三步:
利用上步所得的结果构造ARP包,获取相应的MAC;
第四步:
利用多线程技术,快速实现ARP的运行;
第五步:
将返回的结果利用委托技术显示到主窗口上。
SectionIIITestCasesandResultsAnalysis
SectionIVConclusion
通过本次实验,我对C#、多线程、委托、整个程序的运行次序有了更深入的理解,感
谢老师给予的指导,我会通过学习C#了解C++的运行方式,因为他们的模式太相似了。
SectionVReferences
C#多线程计算机扫描电脑爱好者
C#.net实训编程XX文库
C#如何理解多线程和委托XX文库
C#数据类型转换XX文库
C++和C#互调用DLL豆丁文档
C#rawsocket网络封包监视CSDN
SectionVIAppendix
usingSystem;
usingSystem.Management;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Threading;
//使用多线程的声明(准确的说是要在main主线程中建立新的线程时要进行的引
用说明)
usingSystem.Runtime.InteropServices;
//使用DllImport的空间引用声明
usingSystem.Net;
//对IP相关类的引用声明
namespacemytest
{
publicpartialclassForm1:
Form
[DllImport("
Iphlpapi.dll"
)]
publicstaticexternuintSendARP(uintDestIP,uintSrcIP,refulongpMacAddr,refuint
PhyAddrLen);
publicstaticintNum=0;
byte[]submask={0,0,0,0};
byte[]localip={0,0,0,0};
byte[]netip={0,0,0,0};
int[]temp={0,0,0,0};
byte[]destip={0,0,0,0};
publicstaticstringIP;
publicstaticstringMac;
publicstaticstringItemFlag;
//Threadthread1;
//在主线程中声明线程1
//Threadthread2;
//在主线程中声明线程2
delegatevoiddaililist();
//主线程之外的线程要调用main线程中建立的控件,需要使用委托
的方式,此处是委托的定义
publicdelegatevoidUpdateList(stringip,stringresult);
publicdelegatevoidUpdateLabel();
//daililistlist1;
//委托实例化,即建立委托对应的事件
publicForm1()
InitializeComponent();
//list1=newdaililist(updatelist);
//委托处理的方法,这里updatelist是一个方法,
这个句子的意思是把updatelist这个方法交给了list1这个委托实例
button2.Enabled=false;
button4.Enabled=false;
}
privatevoidbutton1_Click(objectsender,EventArgse)
/*thread1=newThread(numadd);
thread2=newThread(numadd);
thread1.Start();
thread2.Start();
*/
//listBox1.Items.Add(GetMac("
10.128.11.254"
));
label6.Text="
系统正在搜索,请等待......"
;
baseinfo(listBox1,listBox2);
GetDestIp(localip,submask,listBox3);
label5.Text="
当前网段内可用的IP共"
+listBox3.Items.Count.ToString()+"
种!
"
;
button1.Enabled=false;
button4.Enabled=true;
系统搜索完毕!
publicvoidbaseinfo(ListBoxs1,ListBoxs2)//获取本机ip、子网掩码、本段网络号基本信
息
ManagementClassmc=newManagementClass("
Win32_NetworkAdapterConfiguration"
);
ManagementObjectCollectionnics=mc.GetInstances();
foreach(ManagementObjectnicinnics)
if(Convert.ToBoolean(nic["
ipEnabled"
])==true)
s1.Items.Add((nic["
IPAddress"
]asString[])[0]);
s2.Items.Add((nic["
IPSubnet"
s1.SelectedIndex=0;
//选中ip列表首选项
s2.SelectedIndex=0;
//选中mask列表首选项
IPAddressip=IPAddress.Parse(s1.SelectedItem.ToString());
//将ip字符串格式转换成
ip格式
localip=ip.GetAddressBytes();
//将ip格式转换成字节数组,便于运算
ip=IPAddress.Parse(s2.SelectedItem.ToString());
//将子网掩码
submask=ip.GetAddressBytes();
netip=NetIPDeal(localip,submask);
publicbyte[]NetIPDeal(byte[]localip,byte[]submask)//获取网络号
byte[]result={0,0,0,0};
int[]temp={0,0,0,0};
for(inti=0;
i<
4;
i++)
temp[i]=(int)localip[i]&
(int)submask[i];
result[i]=(byte)temp[i];
returnresult;
publicvoidGetDestIp(byte[]localip,byte[]submask,ListBoxResultShow)//获取网段IP,
只要给定本机IP和子网掩码,可以迅速确定网段所有IP
int[]interval={0,0,0,0};
//存储本网段子网掩码和全掩码(255.255.255.255)的码距
byte[]top={0,0,0,0};
//记录本网段可用IP的上限IP
byte[]bottom={0,0,0,0};
//记录本网段可用IP的下限IP
//存贮临时的网络号
i++)//计算本网段子网掩码和全掩码(255.255.255.255)的码距
if(submask[i]!
=255)
interval[i]=255-(int)submask[i]+1;
bytetemp;
temp=localip[i];
if(interval[i]!
=0)
inttemp_result;
temp_result=(int)temp&
(int)(submask[i]);
=256)
while(temp_result==(int)netip[i])
temp++;
top[i]=(byte)((int)temp-1);
bottom[i]=(byte)((int)top[i]-interval[i]+1);
else
top[i]=254;
bottom[i]=0;
top[i]=(byte)temp;
bottom[i]=top[i];
for(bytei=bottom[0];
=top[0];
for(bytej=bottom[1];
j<
=top[1];
j++)
for(bytek=bottom[2];
k<
=top[2];
k++)
for(bytet=bottom[3];
t<
=top[3];
t++)
stringdest;
dest=i.ToString()+"
."
+j.ToString()+"
+k.ToString()
+"
+t.ToString();
ResultShow.Items.Add(dest);
/*publicvoidupdatelist()
listBox4.Items.Add(Num);
}*/
/*publicvoidnumadd()
Num++;
listBox4.Invoke(list1);
//这是自己建立的线程要调用的方法,但是listbox1是main主线程
建立的控件,自定义的线程无法直接访问,上面已经定义了委托,此处应用listbox1的invoke
//方法把所有对listbox1的操作都交给委托list1去处理。
//到此我们理一下思路,建立线程thread1、thread2,这两个线程都指向了numadd这个方法,
首先都执行Num++,然后执行listBox1.Invoke(list1),意思是说将要对listbox1操作,
//但是具体操作在list1这个委托当中,然后这个委托指向了updatelist并执行,至此
thread1、thread2都完成了对listbox1的操作!
/*publicstaticstringGetMac(stringp_Id)
IPAddress_Address;
if(!
IPAddress.TryParse(p_Id,out_Address))return"
uintDestIP=System.BitConverter.ToUInt32(_Address.GetAddressBytes(),0);
ulongpMacAddr=0;
uintPhyAddrLen=6;
uinterror_code=SendARP(DestIP,0,refpMacAddr,refPhyAddrLen);
byte[]_Bytes1=BitConverter.GetBytes(pMacAddr);
returnBitConverter.ToString(_Bytes1,0,6);
voidUpdateMyLabel()
//label6.Text="
voidUpdateMyList(stringip,stringresult)//委托入口程序
lock(listBox4)
if(result!
=ItemFlag)
listBox4.Items.Add(ip+"
MAC"
+result);
ItemFlag=result;
//progressBar1.Value++;
publicvoidGetMac()
IPAddress.TryParse(IP,out_Address))return;
UpdateListshowmac=newUpdateList(UpdateMyList);
UpdateLabelupmylabel=newUpdateLabel(UpdateMyLabel);
try//捕获程序异常
//发送ARP
包,获取MAC
Mac=BitConverter.ToString(_Bytes1,0,6);
catch
Mac="
00-00-00-00-00-00"
if(showmac!
=null)
listBox4.Invoke(showmac,IP,Mac);
//激活委托,执行委托入口程序
//label5.Invoke(upmylabel);
privatevoidbutton4_Click_1(objectsender,EventArgse)//扫描给定的IP
intcount;
//获取IP总数,准备建立线程数
inti;
//禁用扫描按钮
count=listBox3.Items.Count;
系统正在扫描,请等待....."
Thread[]myscan=newThread[count];
//定义线程数
for(i=0;
count;
listBox3.SelectedIndex=i;
IP=listBox3.Text;
//ygh.showmac=newUpdateList(UpdateMyList);
myscan[i]=newThread(newThreadStart(GetMac));
//建立线程入口
myscan[i].Start();
//启动线程
系统扫描完毕!
button2.Enabled=true;
privatevoidbutton2_Click(objectsender,EventArgse)//清除四个列表信息
listBox1.Items.Clear();
listBox2.Items.Clear();
listBox3.Items.Clear();
listBox4.Items.Clear();
button1.Enabled=true;