原始套接字编程(四)

来源:互联网 发布:斗鱼装机王哥淘宝店 编辑:程序博客网 时间:2024/05/17 07:52

Tracert命令实现

1.包含库,

#include <iphlpapi.h>

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

2.初始化环境

3.定义扫描的起始IP

4.定义扫描的结束IP

5.在线程里面进行循环扫描

6.构建mac地址,并发送arp数据包

代码:TracertCmd.cpp

// TracertCmd.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <WinSock2.h>#include <iphlpapi.h>#pragma comment(lib, "iphlpapi.lib")#pragma comment(lib, "ws2_32.lib")DWORD WINAPI ThreadProc(_In_ LPVOID lpParameter){//接收参数IPULONG ipSend = (ULONG)lpParameter;//构建mac地址UCHAR ucMac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};ULONG ulSize = 6;in_addr ip;ip.S_un.S_addr = ipSend;//发送ARP数据包int nRet = SendARP(ipSend, 0, ucMac, &ulSize);if (NO_ERROR == nRet){printf("发现存活主机:%s\n", inet_ntoa(ip));}return 0;}int _tmain(int argc, _TCHAR* argv[]){WSADATA wsd = {};WSAStartup(MAKEWORD(2, 2), &wsd);//检查版本号if (LOBYTE(wsd.wVersion != 2) &&HIBYTE(wsd.wVersion != 2)){printf("初始化失败!");return 0;}//定义要ip地址结构//IPAddr ipSend;in_addr ipStart, ipEnd;//开始IPipStart.S_un.S_addr = inet_addr("192.168.1.2");//结束IPipEnd.S_un.S_addr = inet_addr("192.168.1.254");for (in_addr i = ipStart; i.S_un.S_addr != ipEnd.S_un.S_addr; ++i.S_un.S_un_b.s_b4){//采用多线程进行扫描CreateThread(NULL, NULL, ThreadProc, (LPVOID)i.S_un.S_addr, 0, 0);}system("pause");return 0;}


原创粉丝点击