#include <WinSock2.h> #include <Iphlpapi.h> #include <iostream> using namespace std; #pragma comment(lib,"Iphlpapi.lib") //需要添加Iphlpapi.lib库
int main(int argc, char* argv[]) { //PIP_ADAPTER_INFO结构体指针存储本机网卡信息 PIP_ADAPTER_INFO pIpAdapterInfo = new IP_ADAPTER_INFO(); //得到结构体大小,用于GetAdaptersInfo参数 unsigned long stSize = sizeof(IP_ADAPTER_INFO); //调用GetAdaptersInfo函数,填充pIpAdapterInfo指针变量;其中stSize参数既是一个输入量也是一个输出量 int nRel = GetAdaptersInfo(pIpAdapterInfo, &stSize); //记录网卡数量 int netCardNum = 0; //记录每张网卡上的IP地址数量 int IPnumPerNetCard = 0; if (ERROR_BUFFER_OVERFLOW == nRel) { //如果函数返回的是ERROR_BUFFER_OVERFLOW //则说明GetAdaptersInfo参数传递的内存空间不够,同时其传出stSize,表示需要的空间大小 //这也是说明为什么stSize既是一个输入量也是一个输出量 //释放原来的内存空间 delete pIpAdapterInfo; //重新申请内存空间用来存储所有网卡信息 pIpAdapterInfo = (PIP_ADAPTER_INFO)new BYTE[stSize]; //再次调用GetAdaptersInfo函数,填充pIpAdapterInfo指针变量 nRel = GetAdaptersInfo(pIpAdapterInfo, &stSize); } if (ERROR_SUCCESS == nRel) { //输出网卡信息 //可能有多网卡,因此通过循环去判断 while (pIpAdapterInfo) { cout << "网卡数量:" << ++netCardNum << endl; cout << "网卡名称:" << pIpAdapterInfo->AdapterName << endl; cout << "网卡描述:" << pIpAdapterInfo->Description << endl; switch (pIpAdapterInfo->Type) { case MIB_IF_TYPE_OTHER: cout << "网卡类型:" << "OTHER" << endl; break; case MIB_IF_TYPE_ETHERNET: cout << "网卡类型:" << "ETHERNET" << endl; break; case MIB_IF_TYPE_TOKENRING: cout << "网卡类型:" << "TOKENRING" << endl; break; case MIB_IF_TYPE_FDDI: cout << "网卡类型:" << "FDDI" << endl; break; case MIB_IF_TYPE_PPP: printf("PP\n"); cout << "网卡类型:" << "PPP" << endl; break; case MIB_IF_TYPE_LOOPBACK: cout << "网卡类型:" << "LOOPBACK" << endl; break; case MIB_IF_TYPE_SLIP: cout << "网卡类型:" << "SLIP" << endl; break; default:
break; } cout << "网卡MAC地址:"; for (DWORD i = 0; i < pIpAdapterInfo->AddressLength; i++) if (i < pIpAdapterInfo->AddressLength - 1) { printf("%02X-", pIpAdapterInfo->Address[i]); } else { printf("%02X\n", pIpAdapterInfo->Address[i]); } cout << "网卡IP地址如下:" << endl; //可能网卡有多IP,因此通过循环去判断 IP_ADDR_STRING *pIpAddrString = &(pIpAdapterInfo->IpAddressList); IPnumPerNetCard =0; do { cout << "该网卡上的IP数量:" << ++IPnumPerNetCard << endl; cout << "IP 地址:" << pIpAddrString->IpAddress.String << endl; cout << "子网地址:" << pIpAddrString->IpMask.String << endl; cout << "网关地址:" << pIpAdapterInfo->GatewayList.IpAddress.String << endl; pIpAddrString = pIpAddrString->Next; } while (pIpAddrString); pIpAdapterInfo = pIpAdapterInfo->Next; cout << "--------------------------------------------------------------------" << endl; }
} //释放内存空间 if (pIpAdapterInfo) { delete pIpAdapterInfo; }
return 0; }
//-----------------------------------------------------------------扫描网段
#include <winsock2.h> #include <ws2tcpip.h> #include <stdio.h> #include <string> #include<iostream> using namespace std;
// link with ws2_32.lib #pragma comment(lib, "Ws2_32.lib") void IPAddressParse(string &IpAddress, const unsigned int numericIp) { string temp; temp += to_string(numericIp >> 24); temp += '.'; temp += to_string((numericIp & 0x00ff0000) >> 16); temp += '.'; temp += to_string((numericIp & 0x0000ff00) >> 8); temp += '.'; temp += to_string(numericIp & 0x000000ff); IpAddress = temp; } int main() {
//----------------------------------------- // Declare and initialize variables WSADATA wsaData = { 0 }; int iResult = 0;
DWORD dwRetval;
struct sockaddr_in saGNI; char hostname[NI_MAXHOST]; char servInfo[NI_MAXSERV]; u_short port = 5000;
// Validate the parameters /* if (argc != 1) { printf("usage: %s IPv4 address\n", argv[0]); printf(" to return hostname\n"); printf(" %s 127.0.0.1\n", argv[0]); return 1; }*/ // Initialize Winsock iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); if (iResult != 0) { printf("WSAStartup failed: %d\n", iResult); return 1; } //----------------------------------------- // Set up sockaddr_in structure which is passed // to the getnameinfo function saGNI.sin_family = AF_INET; saGNI.sin_port = htons(port); unsigned int startIp = 0xc0a80002; unsigned int endIp = 0xc0a800fe; unsigned int i; string ip_in; for (i = startIp; i <= endIp; i++) { IPAddressParse(ip_in, i); saGNI.sin_addr.s_addr = inet_addr(ip_in.c_str()); //----------------------------------------- // Call getnameinfo dwRetval = getnameinfo((struct sockaddr *) &saGNI, sizeof(struct sockaddr), hostname, NI_MAXHOST, servInfo, NI_MAXSERV, NI_NUMERICSERV);
if (dwRetval != 0) { printf("getnameinfo failed with error # %ld\n", WSAGetLastError()); } else { printf("getnameinfo returned hostname = %s\n", hostname); } } WSACleanup(); }
//
//获取arp缓存-----------------------
#include <stdlib.h>
int main()
{ system("arp -a >ipArp.txt");
return 0;
}
int main() { FILE *fp; char buf[3072] = {0}; if ((fp = _popen("arp -a", "r")) == NULL) { perror("Fail to popen\n"); exit(1); } while (fgets(buf,3072,fp)!=NULL) { printf("%s",buf); } _pclose(fp); return 0;
}
//-------------------------------
#ifndef UNICODE #define UNICODE #endif #pragma comment(lib, "mpr.lib") //#pragma comment(lib, "Ws2_32.lib") #pragma warning(disable : 4996)
#include <windows.h> #include <stdio.h> #include <winnetwk.h> //#include <Winsock2.h>
BOOL WINAPI EnumerateFunc(LPNETRESOURCE lpnr); void DisplayStruct(int i, LPNETRESOURCE lpnrLocal);
BOOL WINAPI EnumerateFunc(LPNETRESOURCE lpnr) { DWORD dwResult, dwResultEnum; HANDLE hEnum; DWORD cbBuffer = 16384; // 16K is a good size DWORD cEntries = -1; // enumerate all possible entries LPNETRESOURCE lpnrLocal; // pointer to enumerated structures DWORD i; // // Call the WNetOpenEnum function to begin the enumeration. // dwResult = WNetOpenEnum(RESOURCE_GLOBALNET, // all network resources RESOURCETYPE_ANY, // all resources 0, // enumerate all resources lpnr, // NULL first time the function is called &hEnum); // handle to the resource
if (dwResult != NO_ERROR) { printf("WnetOpenEnum failed with error %d\n", dwResult); return FALSE; } // // Call the GlobalAlloc function to allocate resources. // lpnrLocal = (LPNETRESOURCE)GlobalAlloc(GPTR, cbBuffer); if (lpnrLocal == NULL) { printf("WnetOpenEnum failed with error %d\n", dwResult); // NetErrorHandler(hwnd, dwResult, (LPSTR)"WNetOpenEnum"); return FALSE; }
do { // // Initialize the buffer. // ZeroMemory(lpnrLocal, cbBuffer); // // Call the WNetEnumResource function to continue // the enumeration. // dwResultEnum = WNetEnumResource(hEnum, // resource handle &cEntries, // defined locally as -1 lpnrLocal, // LPNETRESOURCE &cbBuffer); // buffer size // // If the call succeeds, loop through the structures. // if (dwResultEnum == NO_ERROR) { for (i = 0; i < cEntries; i++) { // Call an application-defined function to // display the contents of the NETRESOURCE structures. // DisplayStruct(i, &lpnrLocal[i]);
// If the NETRESOURCE structure represents a container resource, // call the EnumerateFunc function recursively.
if (RESOURCEUSAGE_CONTAINER == (lpnrLocal[i].dwUsage & RESOURCEUSAGE_CONTAINER)) // if(!EnumerateFunc(hwnd, hdc, &lpnrLocal[i])) if (!EnumerateFunc(&lpnrLocal[i])) printf("EnumerateFunc returned FALSE\n"); // TextOut(hdc, 10, 10, "EnumerateFunc returned FALSE.", 29); } } // Process errors. // else if (dwResultEnum != ERROR_NO_MORE_ITEMS) { printf("WNetEnumResource failed with error %d\n", dwResultEnum);
// NetErrorHandler(hwnd, dwResultEnum, (LPSTR)"WNetEnumResource"); break; } } // // End do. // while (dwResultEnum != ERROR_NO_MORE_ITEMS); // // Call the GlobalFree function to free the memory. // GlobalFree((HGLOBAL)lpnrLocal); // // Call WNetCloseEnum to end the enumeration. // dwResult = WNetCloseEnum(hEnum);
if (dwResult != NO_ERROR) { // // Process errors. // printf("WNetCloseEnum failed with error %d\n", dwResult); // NetErrorHandler(hwnd, dwResult, (LPSTR)"WNetCloseEnum"); return FALSE; }
return TRUE; }
void DisplayStruct(int i, LPNETRESOURCE lpnrLocal) { printf("NETRESOURCE[%d] Scope: ", i); switch (lpnrLocal->dwScope) { case (RESOURCE_CONNECTED): printf("connected\n"); break; case (RESOURCE_GLOBALNET): printf("all resources\n"); break; case (RESOURCE_REMEMBERED): printf("remembered\n"); break; default: printf("unknown scope %d\n", lpnrLocal->dwScope); break; }
printf("NETRESOURCE[%d] Type: ", i); switch (lpnrLocal->dwType) { case (RESOURCETYPE_ANY): printf("any\n"); break; case (RESOURCETYPE_DISK): printf("disk\n"); break; case (RESOURCETYPE_PRINT): printf("print\n"); break; default: printf("unknown type %d\n", lpnrLocal->dwType); break; }
printf("NETRESOURCE[%d] DisplayType: ", i); switch (lpnrLocal->dwDisplayType) { case (RESOURCEDISPLAYTYPE_GENERIC): printf("generic\n"); break; case (RESOURCEDISPLAYTYPE_DOMAIN): printf("domain\n"); break; case (RESOURCEDISPLAYTYPE_SERVER): printf("server\n"); break; case (RESOURCEDISPLAYTYPE_SHARE): printf("share\n"); break; case (RESOURCEDISPLAYTYPE_FILE): printf("file\n"); break; case (RESOURCEDISPLAYTYPE_GROUP): printf("group\n"); break; case (RESOURCEDISPLAYTYPE_NETWORK): printf("network\n"); break; default: printf("unknown display type %d\n", lpnrLocal->dwDisplayType); break; }
printf("NETRESOURCE[%d] Usage: 0x%x = ", i, lpnrLocal->dwUsage); if (lpnrLocal->dwUsage & RESOURCEUSAGE_CONNECTABLE) printf("connectable "); if (lpnrLocal->dwUsage & RESOURCEUSAGE_CONTAINER) printf("container "); printf("\n");
printf("NETRESOURCE[%d] Localname: %S\n", i, lpnrLocal->lpLocalName); printf("NETRESOURCE[%d] Remotename: %S\n", i, lpnrLocal->lpRemoteName); char szHostName[200]; hostent *host; in_addr *ptr; if (lpnrLocal->lpRemoteName) { CString strFullName = lpnrLocal->lpRemoteName; if (0 == strFullName.Left(2).Compare(_T("\\\\"))) strFullName = strFullName.Right(strFullName.GetLength() - 2); gethostname(szHostName, strlen(szHostName)); USES_CONVERSION; char *pchar = T2A(strFullName); host = gethostbyname(pchar); if (host != NULL) { ptr = (in_addr *) host->h_addr_list[0]; string str = ""; for (int n = 0; n < 4; n++) { CString addr; if (n > 0) { str += "."; } int value = (unsigned int)((unsigned char*)host->h_addr_list[0])[n]; char p[20]; sprintf_s(p, "%d", value); str.append(p); } printf("NETRESOURCE[%d] IP: %s\n", i, str.c_str()); } else { printf("NETRESOURCE[%d] IP: cannot get ipadress\n", i); } } printf("NETRESOURCE[%d] Comment: %S\n", i, lpnrLocal->lpComment); printf("NETRESOURCE[%d] Provider: %S\n", i, lpnrLocal->lpProvider); printf("\n"); }