阅读:4513回复:6
如何获得ip和mac地址
各位大虾,要获得本网段的所有ip和mac地址应采用什么方法?据说
可以采用ARP协议获得,那么具体应怎样实现?请说得详细一点? 另外,有没有一个Api函数可以直接获得本机的ip和mac地址。 小弟谢了先。 |
|
最新喜欢:cyliu |
沙发#
发布于:2001-04-12 08:19
看看tcp/ip详解 卷一 协议 就行了。
|
|
|
板凳#
发布于:2001-04-12 08:40
如果你不是要求有很快的响应的话,可以作一个监测程序,监测
所有的Frame, 然后用程序进行分析即可。前提是网段中没有交换机。 至如ARP, 其原理正如PING. |
|
|
地板#
发布于:2001-04-12 09:43
Unix下好象没有标准的取MAC的函数存在于Socket中,Win中不知有没?Sniffit源马里有个函数是较为通用的可以拿来用。要不然只有分析Frame中的数据,针对ARP取出来就行。
|
|
地下室#
发布于:2001-12-21 13:57
Unix下好象没有标准的取MAC的函数存在于Socket中,Win中不知有没?Sniffit源马里有个函数是较为通用的可以拿来用。要不然只有分析Frame中的数据,针对ARP取出来就行。 Win中有标准API可以取本机MAC。在DOS(有NDIS)和WIN下可以取本 机MAC地址表(多个网卡)。 Delphi代码: http://www.torry.net/samples/samples/hard/getmacad.zip 本网段中的MAC就不容易取。 可以发ICMP包,查询整个网段,但不少防火墙可以不回应你的ICMP。 |
|
5楼#
发布于:2001-12-21 17:02
Win中有个叫IPHELP的API可以取本机所有网卡和MODEM的MAC。以及MAC和IP的对应关系,基本上就和ARP的功能差不多,详情请查《Windows 网络编程》的附录。
|
|
|
6楼#
发布于:2001-12-21 19:45
取本地的,在98和WIN2000下可以,NT下不行
/******************************************************************************\\ * This is a part of the Microsoft Source Code Samples. * Copyright 1996 - 1998 Microsoft Corporation. * All rights reserved. * This source code is only intended as a supplement to * Microsoft Development Tools and/or WinHelp documentation. * See these sources for detailed information regarding the * Microsoft samples programs. \\******************************************************************************/ /* Module Name: Ipconfig.cpp Abstract: This module illustrates how to programmatically retrieve IP configuration information similar to the IPCONFIG.EXE utility. It demonstrates how to use the IP Helper APIs GetNetworkParams() and GetAdaptersInfo(). To execute this application, simply build the application using the Microsoft Visual C++ nmake.exe program generation utility to make an executable ipconfig.exe. After the build is complete, simply execute the resulting ipconfig.exe program. Author: Jim Ohlund 21-Apr-98 Revision History: */ #include <windows.h> #include <iphlpapi.h> #include <stdio.h> #include <time.h> void main(void) { DWORD Err; PFIXED_INFO pFixedInfo; DWORD FixedInfoSize = 0; PIP_ADAPTER_INFO pAdapterInfo, pAdapt; DWORD AdapterInfoSize; PIP_ADDR_STRING pAddrStr; // // Get the main IP configuration information for this machine using a FIXED_INFO structure // if ((Err = GetNetworkParams(NULL, &FixedInfoSize)) != 0) { if (Err != ERROR_BUFFER_OVERFLOW) { printf(\"GetNetworkParams sizing failed with error %d\\n\", Err); return; } } // Allocate memory from sizing information if ((pFixedInfo = (PFIXED_INFO) GlobalAlloc(GPTR, FixedInfoSize)) == NULL) { printf(\"Memory allocation error\\n\"); return; } if ((Err = GetNetworkParams(pFixedInfo, &FixedInfoSize)) == 0) { printf(\"\\tHost Name . . . . . . . . . : %s\\n\", pFixedInfo->HostName); printf(\"\\tDNS Servers . . . . . . . . : %s\\n\", pFixedInfo->DnsServerList.IpAddress.String); pAddrStr = pFixedInfo->DnsServerList.Next; while(pAddrStr) { printf(\"%52s\\n\", pAddrStr->IpAddress.String); pAddrStr = pAddrStr->Next; } printf(\"\\tNode Type . . . . . . . . . : \"); switch (pFixedInfo->NodeType) { case 1: printf(\"%s\\n\", \"Broadcast\"); break; case 2: printf(\"%s\\n\", \"Peer to peer\"); break; case 4: printf(\"%s\\n\", \"Mixed\"); break; case 8: printf(\"%s\\n\", \"Hybrid\"); break; default: printf(\"\\n\"); } printf(\"\\tNetBIOS Scope ID. . . . . . : %s\\n\", pFixedInfo->ScopeId); printf(\"\\tIP Routing Enabled. . . . . : %s\\n\", (pFixedInfo->EnableRouting ? \"yes\" : \"no\")); printf(\"\\tWINS Proxy Enabled. . . . . : %s\\n\", (pFixedInfo->EnableProxy ? \"yes\" : \"no\")); printf(\"\\tNetBIOS Resolution Uses DNS : %s\\n\", (pFixedInfo->EnableDns ? \"yes\" : \"no\")); } else { printf(\"GetNetworkParams failed with error %d\\n\", Err); return; } // // Enumerate all of the adapter specific information using the IP_ADAPTER_INFO structure. // Note: IP_ADAPTER_INFO contains a linked list of adapter entries. // AdapterInfoSize = 0; if ((Err = GetAdaptersInfo(NULL, &AdapterInfoSize)) != 0) { if (Err != ERROR_BUFFER_OVERFLOW) { printf(\"GetAdaptersInfo sizing failed with error %d\\n\", Err); return; } } // Allocate memory from sizing information if ((pAdapterInfo = (PIP_ADAPTER_INFO) GlobalAlloc(GPTR, AdapterInfoSize)) == NULL) { printf(\"Memory allocation error\\n\"); return; } // Get actual adapter information if ((Err = GetAdaptersInfo(pAdapterInfo, &AdapterInfoSize)) != 0) { printf(\"GetAdaptersInfo failed with error %d\\n\", Err); return; } pAdapt = pAdapterInfo; while (pAdapt) { switch (pAdapt->Type) { case MIB_IF_TYPE_ETHERNET: printf(\"\\nEthernet adapter \"); break; case MIB_IF_TYPE_TOKENRING: printf(\"\\nToken Ring adapter \"); break; case MIB_IF_TYPE_FDDI: printf(\"\\nFDDI adapter \"); break; case MIB_IF_TYPE_PPP: printf(\"\\nPPP adapter \"); break; case MIB_IF_TYPE_LOOPBACK: printf(\"\\nLoopback adapter \"); break; case MIB_IF_TYPE_SLIP: printf(\"\\nSlip adapter \"); break; case MIB_IF_TYPE_OTHER: default: printf(\"\\nOther adapter \"); } printf(\"%s:\\n\\n\", pAdapt->AdapterName); printf(\"\\tDescription . . . . . . . . : %s\\n\", pAdapt->Description); printf(\"\\tPhysical Address. . . . . . : \"); for (UINT i=0; i<pAdapt->AddressLength; i++) { if (i == (pAdapt->AddressLength - 1)) printf(\"%.2X\\n\",(int)pAdapt->Address); else printf(\"%.2X-\",(int)pAdapt->Address); } printf(\"\\tDHCP Enabled. . . . . . . . : %s\\n\", (pAdapt->DhcpEnabled ? \"yes\" : \"no\")); pAddrStr = &(pAdapt->IpAddressList); while(pAddrStr) { printf(\"\\tIP Address. . . . . . . . . : %s\\n\", pAddrStr->IpAddress.String); printf(\"\\tSubnet Mask . . . . . . . . : %s\\n\", pAddrStr->IpMask.String); pAddrStr = pAddrStr->Next; } printf(\"\\tDefault Gateway . . . . . . : %s\\n\", pAdapt->GatewayList.IpAddress.String); pAddrStr = pAdapt->GatewayList.Next; while(pAddrStr) { printf(\"%52s\\n\", pAddrStr->IpAddress.String); pAddrStr = pAddrStr->Next; } printf(\"\\tDHCP Server . . . . . . . . : %s\\n\", pAdapt->DhcpServer.IpAddress.String); printf(\"\\tPrimary WINS Server . . . . : %s\\n\", pAdapt->PrimaryWinsServer.IpAddress.String); printf(\"\\tSecondary WINS Server . . . : %s\\n\", pAdapt->SecondaryWinsServer.IpAddress.String); struct tm *newtime; // Display coordinated universal time - GMT newtime = gmtime(&pAdapt->LeaseObtained); printf( \"\\tLease Obtained. . . . . . . : %s\", asctime( newtime ) ); newtime = gmtime(&pAdapt->LeaseExpires); printf( \"\\tLease Expires . . . . . . . : %s\", asctime( newtime ) ); pAdapt = pAdapt->Next; } } |
|