根据网卡名获取IP地址,以及掩码地址
2012-07-11 13:45
573 查看
nterface name is something like “eth0″ and the ip address of the interface can be retrieved using the ioctl function.
Here is a simple piece of code that demonstrates how :
The socket used can be a TCP socket (SOCK_STREAM) as well.
If you also need the netmask then use the SIOCGIFNETMASK value in ioctl like this :
Output :
view source
print?
Here is a simple piece of code that demonstrates how :
Code
1 | #include <stdio.h> |
2 | #include <string.h> |
3 | #include <sys/types.h> |
4 | #include <sys/socket.h> |
5 | #include <sys/ioctl.h> |
6 | #include <netinet/in.h> |
7 | #include <net/if.h> |
8 | #include <unistd.h> |
9 | #include <arpa/inet.h> |
10 |
11 | int main() |
12 | { |
13 | int fd; |
14 | struct ifreq ifr; |
15 |
16 | char iface[] = "eth0" ; |
17 |
18 | fd = socket(AF_INET, SOCK_DGRAM, 0); |
19 |
20 | //Type of address to retrieve - IPv4 IP address |
21 | ifr.ifr_addr.sa_family = AF_INET; |
22 |
23 | //Copy the interface name in the ifreq structure |
24 | strncpy (ifr.ifr_name , iface , IFNAMSIZ-1); |
25 |
26 | ioctl(fd, SIOCGIFADDR, &ifr); |
27 |
28 | close(fd); |
29 |
30 | //display result |
31 | printf ( "%s - %s\n" , iface , inet_ntoa(( ( struct sockaddr_in *)&ifr.ifr_addr )->sin_addr) ); |
32 |
33 | return 0; |
34 | } |
Output
1 | $ gcc ioctl.c && ./a.out |
2 | eth0 - 192.168.0.6 |
If you also need the netmask then use the SIOCGIFNETMASK value in ioctl like this :
1 | #include <stdio.h> |
2 | #include <string.h> |
3 | #include <sys/socket.h> |
4 | #include <sys/ioctl.h> |
5 | #include <net/if.h> |
6 | #include <unistd.h> |
7 | #include <arpa/inet.h> |
8 |
9 | int main() |
10 | { |
11 | int fd; |
12 | struct ifreq ifr; |
13 |
14 | char iface[] = "eth0" ; |
15 |
16 | fd = socket(AF_INET, SOCK_DGRAM, 0); |
17 |
18 | //Type of address to retrieve - IPv4 IP address |
19 | ifr.ifr_addr.sa_family = AF_INET; |
20 |
21 | //Copy the interface name in the ifreq structure |
22 | strncpy (ifr.ifr_name , iface , IFNAMSIZ-1); |
23 |
24 | //get the ip address |
25 | ioctl(fd, SIOCGIFADDR, &ifr); |
26 |
27 | //display ip |
28 | printf ( "IP address of %s - %s\n" , iface , inet_ntoa(( ( struct sockaddr_in *)&ifr.ifr_addr )->sin_addr) ); |
29 |
30 | //get the netmask ip |
31 | ioctl(fd, SIOCGIFNETMASK, &ifr); |
32 |
33 | //display netmask |
34 | printf ( "Netmask of %s - %s\n" , iface , inet_ntoa(( ( struct sockaddr_in *)&ifr.ifr_addr )->sin_addr) ); |
35 |
36 | close(fd); |
37 |
38 | return 0; |
39 | } |
view source
print?
1 | $ gcc ioctl.c && ./a.out |
2 | IP address of eth0 - 192.168.0.6 |
3 | Netmask of eth0 - 255.255.255.0 |
相关文章推荐
- 根据ip地址和子网掩码获取广播地址的java实现
- 利用新浪js接口根据ip地址获取实际地址
- 根据ip地址获取所在省市城市地区的真实地址/php函数
- shell 根据用户的要求修改ip获取方式 并修改ip以及掩码
- C#获取cpu序列号 硬盘ID 网卡硬地址以及操作注册表
- 使用php根据起始ip地址和掩码 计算广播地址(网段结束地址)
- 获取远程服务器的ip地址以及地区地址
- linux下 获取本机ip地址和掩码地址
- delphi技术专题---获取网卡物理地址之NB30.pas单元TNCB详解以及源代码分享
- shell 获取网卡IP地址 子网掩码 默认网关 广播地址 MAC地址
- ip地址转换(通过ip地址以及掩码获取广播地址)
- C# 获取本地IP地址以及MAC地址
- 获取电脑物理地址以及通过IP地址获取当前地理位置(省份)的接口
- [C#]获取IP地址以及获取地址
- 根据ip地址和子网掩码计算网段地址和广播地址(原创)
- 获取所有连接本机的IP地址、网卡物理地址、数据库名、程序名等
- ASP.NET获取IP地址以及MAC地址
- C#根据IP地址和子网掩码计算广播地址
- 根据ip地址和掩码求网络地址,主机号,广播号和主机最大数。。
- 获取远程服务器的ip地址以及地区地址