当前位置: 技术问答>linux和unix
在linux下怎么样获取网络接口信息?
来源: 互联网 发布时间:2016-05-09
本文导语: 在Linux下的GCC编程, 我想根据网络接口名(如eth0,ppp_0_8_81_1等), 获取这个接口的IP地址, 子网掩码, MTU等信息, 不知道该怎么实现? 最好能给出代码实现. 谢谢了.(听说用ioctl能获取,但不知如何操作. 使用ifconfig输出信息到一个...
在Linux下的GCC编程, 我想根据网络接口名(如eth0,ppp_0_8_81_1等), 获取这个接口的IP地址, 子网掩码, MTU等信息, 不知道该怎么实现? 最好能给出代码实现. 谢谢了.(听说用ioctl能获取,但不知如何操作. 使用ifconfig输出信息到一个文件的做法暂时不做考虑.)
|
getip.c
#include
#include
#include
int main(void)
{
struct ifaddrs *ifc, *ifc1;
char ip[64];
char nm[64];
if(0!=getifaddrs(&ifc)) return(-1);
ifc1 = ifc;
printf( "IfacetIP addresstNetmaskn ");
for(; NULL != ifc; ifc = (*ifc).ifa_next)
{
printf( "%s ", (*ifc).ifa_name);
if (NULL != (*ifc).ifa_addr)
{
inet_ntop(AF_INET, &(((struct sockaddr_in*)((*ifc).ifa_addr))-> sin_addr), ip, 64);
printf( "t%s ", ip);
}
else
{
printf( "tt ");
}
if (NULL != (*ifc).ifa_netmask)
{
inet_ntop(AF_INET, &(((struct sockaddr_in*)((*ifc).ifa_netmask))-> sin_addr), nm, 64);
printf( "t%s ", nm);
}
else
{
printf( "tt ");
}
printf( "n ");
}
freeifaddrs(ifc1);
return(0);
}
mtu从/proc/net/rt_cahe内读也行。ifconfig也是从/proc/net里边的来的。
#include
#include
#include
int main(void)
{
struct ifaddrs *ifc, *ifc1;
char ip[64];
char nm[64];
if(0!=getifaddrs(&ifc)) return(-1);
ifc1 = ifc;
printf( "IfacetIP addresstNetmaskn ");
for(; NULL != ifc; ifc = (*ifc).ifa_next)
{
printf( "%s ", (*ifc).ifa_name);
if (NULL != (*ifc).ifa_addr)
{
inet_ntop(AF_INET, &(((struct sockaddr_in*)((*ifc).ifa_addr))-> sin_addr), ip, 64);
printf( "t%s ", ip);
}
else
{
printf( "tt ");
}
if (NULL != (*ifc).ifa_netmask)
{
inet_ntop(AF_INET, &(((struct sockaddr_in*)((*ifc).ifa_netmask))-> sin_addr), nm, 64);
printf( "t%s ", nm);
}
else
{
printf( "tt ");
}
printf( "n ");
}
freeifaddrs(ifc1);
return(0);
}
mtu从/proc/net/rt_cahe内读也行。ifconfig也是从/proc/net里边的来的。
|
int get_host_ip(int tcp_socket)
{
char ip_addr[16];
char net_mask[16];
char gate_way[16];
enum {MAXINTERFACES = 16};
struct ifreq ifreqs[MAXINTERFACES];
struct ifconf ifconf;
int interfaces,i;
memset(ifreqs, 0, sizeof(ifreqs));
ifconf.ifc_len = sizeof(ifreqs);
ifconf.ifc_req = ifreqs;
if(ioctl(tcp_socket, SIOCGIFCONF, (char *)&ifconf) == -1) {
perror("ioctl");
return -1;
}
interfaces = ifconf.ifc_len / sizeof(struct ifreq);
if(interfaces = 2)
--interfaces;
for(i=1;isin_addr), 15);
if(ioctl(tcp_socket, SIOCGIFNETMASK, (char *)&ifreqs[interfaces]) == -1) {
perror("ioctl SIOCGIFNETMASK");
return -1;
}
strncpy(net_mask,
inet_ntoa(((struct sockaddr_in *)&ifreqs[interfaces].ifr_netmask)->sin_addr), 15);
if(ioctl(tcp_socket, SIOCGIFBRDADDR, (char *)&ifreqs[interfaces]) == -1) {
perror("ioctl SIOCGIFBRDADDR");
return -1;
}
strncpy(gate_way,
inet_ntoa(((struct sockaddr_in *)&ifreqs[interfaces].ifr_broadaddr)->sin_addr), 15);
// printf("%sn%sn%snn", ip_addr,net_mask,gate_way);//ip地址,子网掩码,网关
}
return 0;
} /* get_ip */
{
char ip_addr[16];
char net_mask[16];
char gate_way[16];
enum {MAXINTERFACES = 16};
struct ifreq ifreqs[MAXINTERFACES];
struct ifconf ifconf;
int interfaces,i;
memset(ifreqs, 0, sizeof(ifreqs));
ifconf.ifc_len = sizeof(ifreqs);
ifconf.ifc_req = ifreqs;
if(ioctl(tcp_socket, SIOCGIFCONF, (char *)&ifconf) == -1) {
perror("ioctl");
return -1;
}
interfaces = ifconf.ifc_len / sizeof(struct ifreq);
if(interfaces = 2)
--interfaces;
for(i=1;isin_addr), 15);
if(ioctl(tcp_socket, SIOCGIFNETMASK, (char *)&ifreqs[interfaces]) == -1) {
perror("ioctl SIOCGIFNETMASK");
return -1;
}
strncpy(net_mask,
inet_ntoa(((struct sockaddr_in *)&ifreqs[interfaces].ifr_netmask)->sin_addr), 15);
if(ioctl(tcp_socket, SIOCGIFBRDADDR, (char *)&ifreqs[interfaces]) == -1) {
perror("ioctl SIOCGIFBRDADDR");
return -1;
}
strncpy(gate_way,
inet_ntoa(((struct sockaddr_in *)&ifreqs[interfaces].ifr_broadaddr)->sin_addr), 15);
// printf("%sn%sn%snn", ip_addr,net_mask,gate_way);//ip地址,子网掩码,网关
}
return 0;
} /* get_ip */
|
/proc/net
从这下边的文件内找就是了
从这下边的文件内找就是了
|
struct ifreq s_ifreq;
strncpy ( s_ifreq.ifr_name, file, IFNAMSIZ );
struct nc_data *mii = (struct nc_data *)&s_ifreq.ifr_data;
mii->reg_num = id;
if ( ioctl ( skfd, 0x89f1, &s_ifreq ) sin_addr.s_addr;
return 0;
}
strncpy ( s_ifreq.ifr_name, file, IFNAMSIZ );
struct nc_data *mii = (struct nc_data *)&s_ifreq.ifr_data;
mii->reg_num = id;
if ( ioctl ( skfd, 0x89f1, &s_ifreq ) sin_addr.s_addr;
return 0;
}
|
为什么要舍易求难呢。。。我觉得从ifconfig读取蛮好的
|
要代码,参考ifconfig的代码就可以了。
ifconfig位于net-tools 包里面,去网上下一个,研究下!
ifconfig位于net-tools 包里面,去网上下一个,研究下!
|
帮顶!不错
|
恩
学习了
学习了