[原创]局域网自动扫描存活主机

本帖最后由 vfox 于 2011-12-29 11:39 编辑

0x1 来90sec有一段时间了,在这里先感谢90sec为大家提供一个这么好的交流环境。祝90sec越办越好。

0x2 进入主题。跟大家分享一个自己写的局域网存活主机扫描工具。
工具实现以下功能:

   1 自动计算当前局域网IP段 
   2 多线程扫描
   3 输出每个存活主机的IP和MAC地址,以及存活的总数。

0x3 代码如下:
/////////////////////////////////////////
//自动扫描局域网存活主机
//优点:不用输入ip
//缺点:如果遇到主机多IP只能扫描其中一个
//by vfox time:2011-12-29
////////////////////////////////////////

#include <windows.h>
#include <stdio.h>
#include <iphlpapi.h>

#pragma comment (lib,"ws2_32.lib")  
#pragma comment (lib,"iphlpapi.lib")


// 线程参数结构体 


struct CThreadParam
{
UINT uIp; // 整数的ip地址
HANDLE hEventCopy; // 赋值参数的事件
};

CRITICAL_SECTION cs; // 打印时用到的临界区
UINT uTotal = 0; // 输出一共存活的主机

/// 用户扫描单个主机的线程函数
DWORD WINAPI LanScan(LPVOID param)
{
HRESULT result;
CThreadParam cParam;
ULONG c[2] = {0},len=6;

cParam = *(CThreadParam *)param;
SetEvent(cParam.hEventCopy);

// 这句关键
result = SendARP(htonl(cParam.uIp),NULL,c,&len);        //发送ARP包

// 没有错误就输出
if(result == NO_ERROR)
{
	UINT i;
	BYTE *g = (BYTE *)&c;
	in_addr target_addr;

	target_addr.S_un.S_addr=htonl((cParam.uIp));

	if (len) 
	{
		::EnterCriticalSection(&cs);
		printf("host %s living, max address:", inet_ntoa(target_addr));
		for (i = 0; i &lt; (int) len; i++)
		{
			if (i == (len - 1))
			{
				printf("%.2X\n", (int) g[i]);
			}
			else
			{
				printf("%.2X-", (int) g[i]);
			}
		}
		uTotal++;
		::LeaveCriticalSection(&cs);
	}
} 
return 0;

}

int main()
{
// 初始化socket
WSADATA data;
WORD wVersion = MAKEWORD(2,2);
WSAStartup(wVersion,&data);

hostent *pLocalHost;
HANDLE hEvent;

// 获得本机IP结构
pLocalHost = ::gethostbyname("");


// 这样获得是网络字节序
ULONG ulIpAddress = (*(struct in_addr *)*(pLocalHost-&gt;h_addr_list)).S_un.S_addr;
PIP_ADAPTER_INFO pAdapterInfo=NULL;
ULONG ulLen=0;

// 为适配器结构申请内存
::GetAdaptersInfo(pAdapterInfo,&ulLen);
pAdapterInfo=(PIP_ADAPTER_INFO)::GlobalAlloc(GPTR,ulLen);

// 初始化线程里用到的参数
::InitializeCriticalSection(&cs);
hEvent = ::CreateEventW(NULL, FALSE, FALSE, NULL);
CThreadParam cParam;
//取得本地适配器结构信息
if(::GetAdaptersInfo(pAdapterInfo,&ulLen)==ERROR_SUCCESS)
{
	while (pAdapterInfo!=NULL)
	{

		if (::inet_addr(pAdapterInfo-&gt;IpAddressList.IpAddress.String) == ulIpAddress)
		{
			// 这里要转换为主机字节序
			ULONG ulIpMask = ntohl(::inet_addr(pAdapterInfo-&gt;IpAddressList.IpMask.String));
			// 与获得网络号
			ULONG ulNetName = ntohl(ulIpAddress) & ulIpMask;
			// 取非减2获得这个网段的主机数
			UINT unNum = ~ulIpMask;
			UINT nNumofHost = unNum - 2;
			// 循环把主机的IP带入线程进行扫描
			UINT i;
			HANDLE hThread;
			DWORD wThread;
			for (i = 0; i &lt; nNumofHost; i++)
			{
				// 存的都是主机字节序
				cParam.uIp = ulNetName + i + 1;
				cParam.hEventCopy = hEvent;
				hThread = CreateThread(NULL, 0, LanScan, (LPVOID)&cParam, 0, &wThread);
				::WaitForSingleObject(hEvent, INFINITE);
			}
			break;
		} 
		pAdapterInfo = pAdapterInfo-&gt;Next;
	}
}

// 休息5秒,等待线程执行结束
Sleep(5000);
printf("\ntotal = %d\n", uTotal);
return 0;

}
0x4 效果截图
LanScan.png

0x5 因为有注释就不过多解释了。在VS2008下编译的。如果用vc6.0要加SDK才有相关的头文件。
这里有编译好的LanScan.7z

PS:如有问题,请跟帖。

**回复 2# kylin **

感谢指正。

加个参数吧 自己添加要扫描的ip段

**回复 4# 夜叉 **

好的。下个版本加进去。

比较方便,win7下肿么会自己关闭?

不错。。。。。。。。。·

确实很不错 留着了

不错,呵呵·····{:4_41:}

**回复 6# null **

这个命令行的运行后就退出了,看回显在cmd里执行吧。

代码不错,收藏了……

好东东 window编程

**回复 12# vfox **
建议输出结果到文本

**回复 12# vfox **
建议输出结果到文本

用arp包来探测还是比较全的

这东西精简好用呀

scan.c:7:21: error: windows.h: 没有那个文件或目录
scan.c:9:22: error: iphlpapi.h: 没有那个文件或目录
scan.c:18: error: expected specifier-qualifier-list before ‘UINT’
scan.c:22: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘cs’
scan.c:23: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘uTotal’
scan.c:27: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘WINAPI’
scan.c: In function ‘main’:
scan.c:74: error: ‘WSADATA’ undeclared (first use in this function)
scan.c:74: error: (Each undeclared identifier is reported only once
scan.c:74: error: for each function it appears in.)
scan.c:74: error: expected ‘;’ before ‘data’
scan.c:75: error: ‘WORD’ undeclared (first use in this function)
scan.c:75: error: expected ‘;’ before ‘wVersion’
scan.c:76: error: ‘wVersion’ undeclared (first use in this function)
scan.c:76: error: ‘data’ undeclared (first use in this function)
scan.c:79: error: ‘hostent’ undeclared (first use in this function)
scan.c:79: error: ‘pLocalHost’ undeclared (first use in this function)
scan.c:80: error: ‘HANDLE’ undeclared (first use in this function)
scan.c:80: error: expected ‘;’ before ‘hEvent’
scan.c:83: error: expected expression before ‘:’ token
scan.c:87: error: ‘ULONG’ undeclared (first use in this function)
scan.c:87: error: expected ‘;’ before ‘ulIpAddress’
scan.c:88: error: ‘PIP_ADAPTER_INFO’ undeclared (first use in this function)
scan.c:88: error: expected ‘;’ before ‘pAdapterInfo’
scan.c:89: error: expected ‘;’ before ‘ulLen’
scan.c:92: error: expected expression before ‘:’ token
scan.c:93: error: ‘pAdapterInfo’ undeclared (first use in this function)
scan.c:93: error: expected ‘;’ before ‘:’ token
scan.c:96: error: expected expression before ‘:’ token
scan.c:97: error: ‘hEvent’ undeclared (first use in this function)
scan.c:97: error: expected expression before ‘:’ token
scan.c:98: error: ‘CThreadParam’ undeclared (first use in this function)
scan.c:98: error: expected ‘;’ before ‘cParam’
scan.c:100: error: expected expression before ‘:’ token
scan.c:105: error: expected expression before ‘:’ token
scan.c:108: error: expected ‘;’ before ‘ulIpMask’
scan.c:110: error: expected ‘;’ before ‘ulNetName’
scan.c:112: error: ‘UINT’ undeclared (first use in this function)
scan.c:112: error: expected ‘;’ before ‘unNum’
scan.c:113: error: expected ‘;’ before ‘nNumofHost’
scan.c:115: error: expected ‘;’ before ‘i’
scan.c:116: error: expected ‘;’ before ‘hThread’
scan.c:117: error: ‘DWORD’ undeclared (first use in this function)
scan.c:117: error: expected ‘;’ before ‘wThread’
scan.c:118: error: ‘i’ undeclared (first use in this function)
scan.c:118: error: ‘nNumofHost’ undeclared (first use in this function)
scan.c:121: error: ‘cParam’ undeclared (first use in this function)
scan.c:121: error: ‘ulNetName’ undeclared (first use in this function)
scan.c:123: error: ‘hThread’ undeclared (first use in this function)
scan.c:123: error: ‘LanScan’ undeclared (first use in this function)
scan.c:123: error: ‘LPVOID’ undeclared (first use in this function)
scan.c:123: error: ‘wThread’ undeclared (first use in this function)
scan.c:124: error: expected expression before ‘:’ token
scan.c:134: error: ‘uTotal’ undeclared (first use in this function)

**回复 22# xiaoCon **

额,你是用什么编译的,怎么连windows.h也找不到呢,我在vc6和vs2008都正常啊,直接粘贴代码的。。

**回复 23# 海默 **

我擦。。我在linux

**回复 24# xiaoCon **

{:4_32:} 你太牛x了。、