Linux C++ python 获取指定网卡的mac

python: 

import os

if __name__ == '__main__':
    for line in os.popen("/sbin/ifconfig"):
        print(line)
        if "ens33" in line:
            mac = line.split()[4]
            mac = mac.replace(":", "")

    print("mac = ", mac)

c++

h:
#ifndef GETMAC_H
#define GETMAC_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

class GetMac
{
public:
    static GetMac& instance() {
        static GetMac *pthis = NULL;
        if(!pthis) {
            pthis = new GetMac();
        }
        return *pthis;
    }

    string get_mac(void);

protected:
    GetMac();
};

#endif // GETMAC_H

cpp:
#include "getmac.h"

string GetMac::get_mac()
{
    char mac[18] = {0};
    struct ifreq ifreq;
    int sock;

    if((sock = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
        cout<<"socket error"<
#include "getmac.h"

using namespace std;

int main()
{
    cout << "Hello World!" << endl;

    cout<

 

你可能感兴趣的:(Python)