如何使用 C 程序获取机器的 MAC 地址?
这个问题与这个问题完全相同:如何使用 C 程序获取计算机的 MAC 地址?
“我正在 Ubuntu 上工作。如何使用 C 程序获取计算机的 MAC 地址或接口(如 eth0)。”
现在,我通常不接触 C...但在这种情况下我必须这样做。由于我真的不知道以下代码(取自上面链接的答案)中发生了什么,因此我需要一些帮助。
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/if.h>
#include <netdb.h>
#include <stdio.h>
int main()
{
struct ifreq s;
int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
strcpy(s.ifr_name, "eth0");
if (0 == ioctl(fd, SIOCGIFHWADDR, &s)) {
int i;
for (i = 0; i < 6; ++i)
printf(" %02x", (unsigned char) s.ifr_addr.sa_data[i]);
puts("\n");
return 0;
}
return 1;
}
我需要一个将 MAC 地址作为字符串返回的函数,而不是打印 MAC 地址的函数。你知道,像这样:
const char * gettaStringFromNativeCode(void)
{
return "This is a string";
}
这将与 Mozilla Chromeless 一起使用,它使用 Firefox 的新 JCTYPES,例如 < a href="https://github.com/mozilla/chromeless/tree/master/examples/jsctypes" rel="nofollow noreferrer">这个。
基本上,我想做这样的事情(借用 C#):
// Using "string" here because its pseudo-code and I don't know what i'm doing. :-)
string getMAC()
{
struct ifreq s;
int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
strcpy(s.ifr_name, "eth0");
var macAddress = string.Empty; // yah, this is actually C#
if (0 == ioctl(fd, SIOCGIFHWADDR, &s)) {
int i;
for (i = 0; i < 6; ++i)
// yah, this is a bit of C# too.
macAddress += string.Format(" %02x", (unsigned char) s.ifr_addr.sa_data[i]) );
}
return macAddress;
}
This question is exactly like this question: How to get MAC address of your machine using a C program?
"I am working on Ubuntu. How can I get MAC address of my machine or an interface say eth0 using C program."
Now, I don't usually touch C...but in this case I have to. Since I don't really know what is going on in the following code, which was taken from the answer linked to above, I need some help.
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/if.h>
#include <netdb.h>
#include <stdio.h>
int main()
{
struct ifreq s;
int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
strcpy(s.ifr_name, "eth0");
if (0 == ioctl(fd, SIOCGIFHWADDR, &s)) {
int i;
for (i = 0; i < 6; ++i)
printf(" %02x", (unsigned char) s.ifr_addr.sa_data[i]);
puts("\n");
return 0;
}
return 1;
}
Instead of a function that prints the MAC Address I need a function that returns it as a string. You know, like this:
const char * gettaStringFromNativeCode(void)
{
return "This is a string";
}
This is to be used with Mozilla Chromeless, which uses Firefox's new JCTYPES like this.
Basically, I'm looking to do something like this (borrowing from C#):
// Using "string" here because its pseudo-code and I don't know what i'm doing. :-)
string getMAC()
{
struct ifreq s;
int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
strcpy(s.ifr_name, "eth0");
var macAddress = string.Empty; // yah, this is actually C#
if (0 == ioctl(fd, SIOCGIFHWADDR, &s)) {
int i;
for (i = 0; i < 6; ++i)
// yah, this is a bit of C# too.
macAddress += string.Format(" %02x", (unsigned char) s.ifr_addr.sa_data[i]) );
}
return macAddress;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)