#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <unistd.h>
int main() {
int fd;
struct ifreq ifr;
struct sockaddr_in *addr;
fd = socket(AF_INET, SOCK_DGRAM, 0);
strcpy(ifr.ifr_name, "eth0");
ioctl(fd, SIOCGIFADDR, &ifr);
close(fd);
addr = (struct sockaddr_in *)&ifr.ifr_addr;
printf("\t\t\teth0 IP address: %s\n", inet_ntoa(addr->sin_addr));
return 0;
}
如何获取eth网卡的ip地址
于 2024-01-26 20:01:49 首次发布