linux下获取网卡IP的C/C++代码
2017-11-09 21:37
501 查看
简单:
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <linux/if.h>
#define BUF_SIZE 1024
using namespace std;
static int getIP(string net_name, string &strIP)
{
int sock_fd;
struct ifconf conf;
struct ifreq *ifr;
char buff[BUF_SIZE] = {0};
int num;
int i;
sock_fd = socket(PF_INET, SOCK_DGRAM, 0);
if ( sock_fd < 0 )
return -1;
conf.ifc_len = BUF_SIZE;
conf.ifc_buf = buff;
if ( ioctl(sock_fd, SIOCGIFCONF, &conf) < 0 )
{
close(sock_fd);
return -1;
}
num = conf.ifc_len / sizeof(struct ifreq);
ifr = conf.ifc_req;
for(i = 0; i < num; i++)
{
struct sockaddr_in *sin = (struct sockaddr_in *)(&ifr->ifr_addr);
if ( ioctl(sock_fd, SIOCGIFFLAGS, ifr) < 0 )
{
close(sock_fd);
return -1;
}
if ( (ifr->ifr_flags & IFF_UP) && strcmp(net_name.c_str(),ifr->ifr_name) == 0 )
{
strIP = inet_ntoa(sin->sin_addr);
close(sock_fd);
return 0;
}
ifr++;
}
close(sock_fd);
return -1;
}
int main()
{
string strIP;
getIP("eth1", strIP);
cout << strIP << endl;
return 0;
}
测试OK
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <linux/if.h>
#define BUF_SIZE 1024
using namespace std;
static int getIP(string net_name, string &strIP)
{
int sock_fd;
struct ifconf conf;
struct ifreq *ifr;
char buff[BUF_SIZE] = {0};
int num;
int i;
sock_fd = socket(PF_INET, SOCK_DGRAM, 0);
if ( sock_fd < 0 )
return -1;
conf.ifc_len = BUF_SIZE;
conf.ifc_buf = buff;
if ( ioctl(sock_fd, SIOCGIFCONF, &conf) < 0 )
{
close(sock_fd);
return -1;
}
num = conf.ifc_len / sizeof(struct ifreq);
ifr = conf.ifc_req;
for(i = 0; i < num; i++)
{
struct sockaddr_in *sin = (struct sockaddr_in *)(&ifr->ifr_addr);
if ( ioctl(sock_fd, SIOCGIFFLAGS, ifr) < 0 )
{
close(sock_fd);
return -1;
}
if ( (ifr->ifr_flags & IFF_UP) && strcmp(net_name.c_str(),ifr->ifr_name) == 0 )
{
strIP = inet_ntoa(sin->sin_addr);
close(sock_fd);
return 0;
}
ifr++;
}
close(sock_fd);
return -1;
}
int main()
{
string strIP;
getIP("eth1", strIP);
cout << strIP << endl;
return 0;
}
测试OK
相关文章推荐
- linux c/c++按规则获取网卡ip
- Linux 下获取LAN中指定IP的网卡的MAC(物理地址)
- Linux系统 C++ 获取网络接口,以及主机网关IP
- 【原创】C++实现获取本机机器名及外网IP代码
- C++实现获取本机机器名及外网IP代码
- 树莓派Linux下无线网卡无法获取IP,不识别,等问题
- linux下获取本地IP信息(没看代码)
- linux下的shell命令的编写,以及java怎样调用linux的shell命令(java怎样获取linux上的网卡的ip信息)
- linux 下C实现获取网卡IP和MAC地址
- linux下C编程_获取指定网卡IP信息
- linux下用代码获取系统的IP与MAC
- LINUX下QT与C语言通过网卡名获取网卡IP与MAC
- 获取IP的C++代码
- VS2012 C++ 中通过GetAdaptersInfo获取多网卡,多IP地址信息
- linux下的shell命令的编写,以及java怎样调用linux的shell命令(java怎样获取linux上的网卡的ip信息)
- 批处理获取网卡名称的代码(附修改IP、网关、DNS脚本)
- python linux系统中获取网卡IP
- Linux下靠谱的获取本机IP地址的C代码实现
- Linux 下获取LAN中指定IP的网卡的MAC(物理地址)
- 关于linux 下获取本机网关 ip mac 子网掩码的代码