MAC地址获取厂商信息及远程主机共享文件夹获取

xingyun86 2021-4-23 870

MAC地址获取厂商信息及远程主机共享文件夹获取

#pragma once
#include <winsock2.h>
#include <lm.h>

#pragma comment(lib, "netapi32.lib")
#include <vector>
#include <unordered_map>
#include <Utils.h>

    static size_t string_split_to_vector(std::vector<std::string>& sv, const std::string& strData, const std::string& strSplitter, std::string::size_type stPos = 0)
    {
        std::string strTmp = ("");
        std::string::size_type stIdx = 0;
        std::string::size_type stSize = strData.length();
        while ((stPos = strData.find(strSplitter, stIdx)) != std::string::npos)
        {
            strTmp = strData.substr(stIdx, stPos - stIdx);
            if (!strTmp.length())
            {
                strTmp = ("");
            }
            sv.push_back(strTmp);
            stIdx = stPos + strSplitter.length();
        }
        if (stIdx < stSize)
        {
            strTmp = strData.substr(stIdx, stSize - stIdx);
            if (!strTmp.length())
            {
                strTmp = ("");
            }
            sv.push_back(strTmp);
        }
        return sv.size();
    }
    
#include <string>
#include <fstream>
#include <streambuf>
#define FILE_READER(F, M) std::string((std::istreambuf_iterator<char>(std::ifstream(F, M | std::ios::in).rdbuf())), std::istreambuf_iterator<char>())
#define FILE_WRITER(D, S, F, M) std::ofstream(F, M | std::ios::out).write((D), (S))

class CMacMap {
public:
    CMacMap() {
        InitKVMap();
    }
public:
    std::unordered_map<std::string, std::string> m_kvMap = {};
public:
    void InitKVMap()
    {
        //http://standards-oui.ieee.org/oui/oui.txt
        std::string s = FILE_READER("oui.txt", std::ios::binary);
        std::vector<std::string> sv = {  };
        CUtils::string_split_to_vector(sv, s, "\r\n");
        if (!sv.empty())
        {
            for (auto it : sv)
            {
                if (it.find(CUtils::ANSIToUTF8("base 16")) != std::string::npos)
                {
                    size_t pos = it.rfind('\x09');
                    if (pos != std::string::npos && (pos + 1) < it.length())
                    {
                        m_kvMap.emplace(it.substr(0, 6), it.substr(pos + 1));
                    }
                }
            }
        }
    }
    static void manufacturer_search(std::unordered_map<std::string, std::string>& sMap)
    {
        for (auto &it : sMap)
        {
            auto itf = CMacMap::Inst()->m_kvMap.find(it.first);
            if (itf != CMacMap::Inst()->m_kvMap.end())
            {
                it.second = itf->second;
                std::cout << itf->second << std::endl;
            }
        }
    }
public:
    static CMacMap* Inst() {
        static CMacMap CMacMapInstance;
        return &CMacMapInstance;
    }
};
__inline static
void share_search(std::unordered_map<std::string, std::vector<std::string>> & svMap)
{
    PSHARE_INFO_1 BufPtr = nullptr, pInfo = nullptr;

    NET_API_STATUS res;

    DWORD dwLevel = 1, dwPrefMaxLen = -1, dwEntriesRead = 0, dwTotalEntries = 0, dwResumeHandle = 0;
    svMap = {
        {"192.168.1.40",{} },
        {"192.168.1.241",{} },
        {"192.168.1.88",{} },
        {"192.168.1.68",{} },
        {"192.168.1.51",{} },
    };
    for (auto & it : svMap)
    {
        do
        {
            res = NetShareEnum((LPTSTR)CUtils::A_To_T(it.first).c_str(), dwLevel, (LPBYTE*)&BufPtr, dwPrefMaxLen, &dwEntriesRead, &dwTotalEntries, &dwResumeHandle);
            if (res == ERROR_SUCCESS || res == ERROR_MORE_DATA)
            {
                pInfo = BufPtr;
                for (int i = 0; i < dwEntriesRead; i++)
                {
                    if (pInfo->shi1_type == STYPE_DISKTREE)
                    {
                        it.second.emplace_back(CUtils::T_To_A(pInfo->shi1_netname));
                    }

                    pInfo++;
                }
                NetApiBufferFree(BufPtr);
            }
        } while (res == ERROR_MORE_DATA);
    }
    for (auto it : svMap)
    {
        std::cout << "[" << it.first << "]" << std::endl;
        for (auto iit : it.second)
        {
            std::cout << iit << std::endl;
        }
    }
}


×
打赏作者
最新回复 (0)
只看楼主
全部楼主
返回