您的位置:程序门 -> .net技术 -> c#



c#查询交换机的方法


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


c#查询交换机的方法
发表于:2007-01-05 11:56:37 楼主
大家好:
        以前我用255个线程扫描局域网主机,功能是实现了,但是,速度不快!我想用c#直接查询交换机的方法来获取局域网主机信息,怎么样能实现呢?望各位大虾指点!小弟不胜感激!
发表于:2007-01-05 12:32:521楼 得分:0
除非交换机厂商预留这个功能接口给你,不然基本没指望的。
如果你的交换机够高级的话直接telnet到交换机上查看mac   table,会有一些基本信息的,应该能满足你。
发表于:2007-01-05 12:43:272楼 得分:0
能不能把你用255个线程扫描局域网主机的代码贴出来?学习学习!
发表于:2007-01-06 22:02:433楼 得分:0
我做的是一个局域网信息交流工具,代码很多,为了讨论需要,我只是摘取多线程扫描局域网ip部分和大家讨论!程序肯定不优化,希望大家别嘲笑在下!

private   void   getlanhost()
{
//获取局域网主机,并加入到   combobox1中
//templocalip   =   dns.gethostbyname(localhostname).addresslist[0].tostring();
//此处不直接使用“localhostip”是因为,它在后面可能有变化!故而用templocalip;
strlanipfront   =   templocalip.substring(0,templocalip.lastindexof( ". "));//取出本地ip地址的前三段
combobox1.text=   " ";
combobox1.items.clear();
mythread   =   new   thread[255];
for(int   i=0;i <255;i++)//启用255个线程扫描局域网主机ip,及机器名
{
mythread[i]   =   new   thread(new   threadstart(scan));
mythread[i].name   =   i.tostring();
mythread[i].start();
if(!mythread[i].join(120))
{
mythread[i].abort();
}
}
}

private   void   scan()
{
//扫描lan_ip的方法
ipaddress   lanip   =   ipaddress.parse(strlanipfront   +   ". "   +   thread.currentthread.name.tostring());
iphostentry   lanhostdns   =   null;
//此处不直接使用“localhostip”是因为,它在后面可能有变化!故而用templocalip;
if(lanip.tostring()   !=   templocalip)   //不扫描本机
{
try
{
lanhostdns   =   dns.gethostbyaddress(lanip);
lock(combobox1)
{
combobox1.items.add( "lan: "   +   lanip.tostring()   +   "( "   +   lanhostdns.hostname.tostring()   +   ") ");
}
}
catch
{}
}
}
发表于:2007-01-06 22:36:524楼 得分:0
哪有这样扫描的,一般用arp扫描,一秒钟可以全扫描到
发表于:2007-01-10 21:21:095楼 得分:0
arp扫描?好啊!朋友,能给出示例代码吗?谢谢!
发表于:2007-01-10 21:56:506楼 得分:0
arp扫描?
没听说过啊,算我不懂arp协议,说来听听...
发表于:2007-01-11 00:28:467楼 得分:0
交换机支持http方式查询的话干脆冒充浏览器直接去取这一页,然后分析得到的html
发表于:2007-01-11 13:11:018楼 得分:0
下面的做法参考一下。另外不需要255个线程,有一个线程扫描就可以了。255个线程光创建和调度就要消耗很多系统资源,可能是性能不高的一个原因哦!

public   class   clsarp
{
#region   apis
[dllimport( "iphlpapi.dll ",   callingconvention   =   callingconvention.cdecl)]
private   static   extern   int32   sendarp(uint32   udwdestip,uint32   udwsrcip,byte[]   pmacaddr,ref   int32   phyaddrlen);

private   const   int32   number_of_physical_address_bytes   =   6;
#endregion

public   clsarp()
{
}

public   static   byte[]   getcomputermacaddr(uint32   dwip)
{
byte[]   abmacaddr   =   null;

int32   dwphyaddrlen   =   0;

try
{
abmacaddr   =   new   byte[number_of_physical_address_bytes];
dwphyaddrlen   =   abmacaddr.length;

if   (sendarp(dwip,0,abmacaddr,   ref   dwphyaddrlen)   !=   0)
{
//get   error!reset   byte   array   to   null
abmacaddr   =   null;
}
}
catch
{
abmacaddr   =   null;
}

return   abmacaddr;
}
}
发表于:2007-01-12 00:43:019楼 得分:0
不懂就在网上搜,这协议是公开的,又不是秘密,至于如何发送arp协议数据,建议你用winpcap的函数库,免费开原的,codeproject上有对winpcap的c#包装类库,很方便,对一个机子发送arp请求包,如果它开机并在网络上的话,会立即向你发送一个应答包,可以根据收到的应答包来搜索所有的机器,由于绝大部分防火墙没有拦截arp协议的功能,所以这个方法可以穿透防火墙,效果最好。


发表于:2007-01-20 11:09:4810楼 得分:0
恩,谢谢zhouhongyun(最终幻想)朋友!另外,我注册了找个社区,好像就发了一个帖子,不能在发帖了,只能回帖,这是怎么的?


快速检索

最新资讯
热门点击