std::string getMacAddress()
{
std::string cmd = "ifconfig";
std::string macAddress;
// 执行系统命令
FILE* pipe = popen(cmd.c_str(), "r");
if (!pipe) {
std::cerr << "执行命令失败!" << std::endl;
return "";
}
// 读取命令输出
char buffer[128];
while (!feof(pipe)) {
if (fgets(buffer, 128, pipe) != nullptr) {
std::string output(buffer);
// 在输出中查找MAC地址
size_t found = output.find("ether");
if (found != std::string::npos) {
size_t start = found + 6; // "ether"后面的位置
size_t end = output.find(" ", start); // 下一个空格的位置
macAddress = output.substr(start, end - start);
break;
}
}
}
std::string result = macAddress;
for (char& c : result)
{
if (std::islower(c)) // 检查字符是否为小写字母
{
c = std::toupper(c); // 将小写字母转换为大写字母
}
}
// 关闭管道
pclose(pipe);
return result;
}
02-21
1341
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-28
2794
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
05-31