有没有办法通过接口获取MAC地址getifaddrs()?
我已经有了这个,获取IP地址,但我有点想念MAC.我试图寻找信息getifaddrs(),但没有任何关于MAC地址
struct ifaddrs *iflist, *iface;
if (getifaddrs(&iflist) < 0)
{
perror("getifaddrs");
}
char addrp[INET6_ADDRSTRLEN];
char macp[INET6_ADDRSTRLEN];
int i=0;
for (iface = iflist; iface; iface = iface->ifa_next)
{
int af = iface->ifa_addr->sa_family;
const void *addr;
const void *mac;
switch (af)
{
case AF_INET:
addr = &((struct sockaddr_in *)iface->ifa_addr)->sin_addr;
break;
//get mac address somehow?
default:
addr = NULL;
}
if (addr)
{
if (inet_ntop(af, addr, addrp, sizeof addrp) == NULL)
{
perror("inet_ntop");
continue; …Run Code Online (Sandbox Code Playgroud) 可能重复:
Linux/C++:获取本地计算机的IP地址
我试图获取本地计算机的所有接口的IP和MAC地址,但我不能真正成功.我需要把它放在一起,所以我知道哪个IP属于哪个MAC.
我在google上进行了很多搜索,甚至在这里,但是我找不到真正的C代码.
请你帮助我好吗?
提前致谢!我真的很感激!
我需要将两个args传递给shell脚本,这里是代码:
#include <stdio.h>
#include <stdlib.h>
void main()
{
char *script;
int lines = 1;
sprintf(script, "/...path.../line.sh %d %d", lines, lines);
system(script);
}
Run Code Online (Sandbox Code Playgroud)
该脚本运行良好,我尝试过.但我总是得到分段错误.问题是:为什么?
谢谢
怎么弄这个:
printf(" %02x:%02x:%02x:%02x:%02x:%02x",
(unsigned char)item->ifr_hwaddr.sa_data[0],
(unsigned char)item->ifr_hwaddr.sa_data[1],
(unsigned char)item->ifr_hwaddr.sa_data[2],
(unsigned char)item->ifr_hwaddr.sa_data[3],
(unsigned char)item->ifr_hwaddr.sa_data[4],
(unsigned char)item->ifr_hwaddr.sa_data[5]);
Run Code Online (Sandbox Code Playgroud)
进入此(存储在变量中,不打印它):
char mac[18];
Run Code Online (Sandbox Code Playgroud)
谢谢
我总是得到malloc()错误.这是代码:
char *data = malloc(200);
add_data(data, tableO, line);
void add_data(char *data, struct ARP_entryO *tableO, int line)
{
int i=0;
while (i < line)
{
strcat(data, tableO[i].IPaddr);
strcat(data, " ");
strcat(data, tableO[i].MACaddr);
strcat(data, " ");
i++;
}
}
Run Code Online (Sandbox Code Playgroud)
我通常发送大约50-60bytes.有什么帮助解决这个问题?
谢谢
我通过这样的套接字在网络上发送数据:(广播)
void sendBroad(char *dstIP, char *localIP, char *localMAC)
{
int sock; /* Socket */
struct sockaddr_in broadcastAddr; /* Broadcast address */
int broadcastPermission; /* Socket opt to set permission to broadcast */
unsigned int dataLen;
char data[100]={0};
strcat(data, localIP);
strcat(data, " ");
strcat(data, localMAC);
strcat(data, " ");
/* Create socket for sending/receiving datagrams */
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
perror("socket() failed");
/* Set socket to allow broadcast */
broadcastPermission = 1;
if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (void *) …Run Code Online (Sandbox Code Playgroud) 我需要同时运行两个独立的进程.一个只是侦听端口X,并在收到数据时发送数据,第二个是执行其他操作.
我试过这个:
pthread_t thread1, thread2;
int iret1, iret2;
iret1 = pthread_create( &thread1, NULL, getAddress(), NULL);
iret2 = pthread_create( &thread2, NULL, operate(), (struct IPlist) *IPlist);
在第一个我想运行get(Address)- 监听和发送部分,在第二个我需要运行operate()一个arg:*IP list //(struct IPlist *IPlist)
但是,它显示错误:
warning: passing argument 3 of ‘pthread_create’ makes pointer from integer without a cast
/usr/include/pthread.h:225: note: expected ‘void * (*)(void *)’ but argument is of type int
error: incompatible type for argument 4 of ‘pthread_create’
/usr/include/pthread.h:225: note: expected ‘void * __restrict__’ but argument is of …
我试图在Linux中的ARP表中添加ARP绑定,我的arp表看起来像这样:
IP address HW type Flags HW address Mask Device
192.168.3.12 0x1 0x6 00:0c:29:89:c5:cc * eth1
192.168.3.100 0x1 0x6 00:0c:29:89:c5:c8 * eth1
192.168.43.2 0x1 0x2 00:50:56:e1:65:76 * eth0
192.168.3.111 0x1 0x6 00:11:22:33:44:55 * eth1
192.168.43.139 0x1 0x6 00:0c:29:89:c5:cc * eth0
Run Code Online (Sandbox Code Playgroud)
这是我想要做的,但我得到以下错误:
arp -s 192.168.43.138 00:00:22:33:33:33
SIOCSARP: Invalid argument
Run Code Online (Sandbox Code Playgroud)
任何其他IP地址工作正常,只是这一个导致错误任何想法这里有什么问题?(这可能不是mac地址的问题,我尝试了很多其他地址)
c ×7
linux ×3
mac-address ×2
arp ×1
interface ×1
ip ×1
ip-address ×1
malloc ×1
shell ×1
sockets ×1