linux C语言获取网卡对应IP地址
linux-dash
A beautiful web dashboard for Linux
项目地址:https://gitcode.com/gh_mirrors/li/linux-dash
免费下载资源
·
int get_gw_ip(char *eth, char *ipaddr)
{
int sock_fd;
struct sockaddr_in my_addr;
struct ifreq ifr;
/**//* Get socket file descriptor */
if ((sock_fd = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
{
perror("socket");
return RET_FAILED;
}
/**//* Get IP Address */
strncpy(ifr.ifr_name, eth, IF_NAMESIZE);
ifr.ifr_name[IFNAMSIZ-1]='/0';
if (ioctl(sock_fd, SIOCGIFADDR, &ifr) < 0)
{
DBGPRN(":No Such Device %s/n",eth);
return RET_FAILED;
}
memcpy(&my_addr, &ifr.ifr_addr, sizeof(my_addr));
strcpy(ipaddr, inet_ntoa(my_addr.sin_addr));
close(sock_fd);
return RET_SUCCESS;
}
其中 eth是网卡名字 如“eth0” “eth1”等,ipaddr是输出参数
GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:2 个月前 )
186a802e
added ecosystem file for PM2 4 年前
5def40a3
Add host customization support for the NodeJS version 4 年前
更多推荐
已为社区贡献7条内容
所有评论(0)