读取相关连接的物理地址

Posted 我没货,只剩下水了

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了读取相关连接的物理地址相关的知识,希望对你有一定的参考价值。

利用上次的匿名管道就可以读取cmd命令的相关信息

将其写成类CmdInfoToPile     

CmdInfoToPipe.h

 1 #ifndef NETINFO_CMDINFOTOPIPE_H_
 2 #define NETINFO_CMDINFOTOPIPE_H_
 3 #include <windows.h>
 4 #include <stdio.h>
 5 #include <string>
 6 #include <vector>
 7 
 8 class CmdInfoToPipe
 9 {
10 public:
11     CmdInfoToPipe();
12     ~CmdInfoToPipe();
13     DWORD getCmdInfo(char *cmd);//匿名管道读取cmd信息
14     std::vector<std::string> get_Mac_Address(std::string strconnect = "本地连接", std::string straddress = "物理地址");//读取物理地址
15 private:
16     std::string strcmd_;
17     std::string cmd_;
18 };
19 #endif//CmdInfoToPipe.h

CmdInfoToPipe.cpp

 1 #include "CmdInfoToPipe.h"
 2 
 3 
 4 CmdInfoToPipe::CmdInfoToPipe()
 5 {
 6     cmd_ = "Cmd.exe /C ";
 7     strcmd_ = "";
 8 }
 9 
10 
11 CmdInfoToPipe::~CmdInfoToPipe()
12 {
13 }
14 
15 std::vector<std::string>  CmdInfoToPipe::get_Mac_Address(std::string strconnect, std::string straddress){
16     int connect = 0;
17     int address = 0;
18     printf("%s %s\n", strconnect.c_str(), straddress.c_str());
19     std::vector<std::string> vstr;
20 
21     while ((connect = strcmd_.find(strconnect, connect)) != std::string::npos){//可能有多个这种连接
22         if((address = strcmd_.find(straddress, connect + 1)) != std::string::npos){
23             if ((address = strcmd_.find("-", address + 1)) != std::string::npos){
24                 vstr.push_back(strcmd_.substr(address - 2, 17));
25                 connect = address + 16;
26             }
27         }
28     }
29     
30     return vstr;
31 }
32 DWORD CmdInfoToPipe::getCmdInfo(char *cmd){
33     cmd_ +=std::string(cmd);
34     char mycmd[100];
35     sprintf_s(mycmd, "%s", cmd_.c_str());
36     char Buffer[4096];
37     STARTUPINFO sInfo;
38     PROCESS_INFORMATION pInfo;
39     SECURITY_ATTRIBUTES sa;
40     HANDLE hRead, hWrite;
41     DWORD bytesRead;
42     sa.nLength = sizeof(SECURITY_ATTRIBUTES);
43     sa.lpSecurityDescriptor = NULL;
44     sa.bInheritHandle = TRUE;
45 
46     if (!CreatePipe(&hRead, &hWrite, &sa, 0)) //创建匿名管道
47     {
48         return GetLastError();
49     }
50 
51     GetStartupInfo(&sInfo);
52     sInfo.cb = sizeof(sInfo);
53     sInfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
54     sInfo.wShowWindow = SW_HIDE;
55     sInfo.hStdError = hWrite;   //将管道的写端交给子进程
56     sInfo.hStdOutput = hWrite;
57     memset(&pInfo, 0, sizeof(pInfo));
58 
59     if (!CreateProcess(NULL, mycmd, NULL, NULL, TRUE, 0, NULL, NULL, &sInfo, &pInfo)) //创建子进程
60     {
61         CloseHandle(hWrite);
62         CloseHandle(hRead);
63         return GetLastError();
64     }
65     CloseHandle(hWrite); //关闭父进程的写端
66     strcmd_ = "";
67 
68     for (int i = 0;; ++i)
69     {
70         if (!ReadFile(hRead, Buffer, sizeof(Buffer) - 1, &bytesRead, NULL)) //读取内容
71         {
72             break;
73         }
74         Buffer[bytesRead] = 0;
75         strcmd_ += std::string(Buffer);//防止出现查找字符分散在各个BUFFER中
76     }
77 
78     WaitForSingleObject(pInfo.hProcess, INFINITE);
79     CloseHandle(hRead);
80     return GetLastError();
81 }

测试代码

 1 #include <CmdInfoToPipe.h>
 2 #include <cstdio>
 3 #include<vector>
 4 #include<string>
 5 int main()
 6 {
 7     char *mycmd = "ipconfig/all";
 8     CmdInfoToPipe pCItP;
 9     pCItP.getCmdInfo(mycmd);
10     std::vector<std::string> vtr = pCItP.get_Mac_Address("无线网络连接");
11     for (auto i : vtr){
12         printf("%s\n", i.c_str());
13     }
14     system("pause");
15     return 0;
16 }

 

以上是关于读取相关连接的物理地址的主要内容,如果未能解决你的问题,请参考以下文章

php怎样获取客户端电脑物理地址

什么是逻辑地址,什么是物理地址,它们之间有什么关系

TLB与内存寻址,内存读取,虚拟内存的相关原理

linux进程内存相关

根据图片的url地址下载图片到本地保存代码片段

操作系统 内存地址(逻辑地址线性地址物理地址)概念