确定网络接口是无线还是有线 [英] Determine if a network interface is wireless or wired

查看:67
本文介绍了确定网络接口是无线还是有线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两个单独部分的程序:当网络接口是无线LAN时应执行其中的一个,而在有线LAN连接时应执行另一个.我怎么能在程序中知道这一点?我应该使用什么功能来获取该信息?

I have a program that has two separate sections: one of them should be executed when the network interface is wireless LAN and the other one when it's a wired LAN connection. How can I know that inside of my program? What function should I use to get that information?

推荐答案

您可以从命令行使用iwconfig命令:

You can use the iwconfig command from the command line:

$ iwconfig
lo        no wireless extensions.
eth0      no wireless extensions.

如果您需要从C使用它,如上面@opaque的链接所述,请获取源代码或使用strace来查看需要使用的ioctls():

If you need to use it from C, as @opaque's link above explains, get the sources or use strace to see which ioctls() you need to use:

ioctl(3, SIOCGIWNAME, 0x7fff82c0d040)   = -1 EOPNOTSUPP (Operation not supported)
ioctl(3, SIOCGIFFLAGS, {ifr_name="lo", ifr_flags=IFF_UP|IFF_LOOPBACK|IFF_RUNNING}) = 0
write(2, "lo        no wireless extensions"..., 35lo        no wireless extensions.

) = 35
ioctl(3, SIOCGIWNAME, 0x7fff82c0d040)   = -1 EOPNOTSUPP (Operation not supported)
ioctl(3, SIOCGIFFLAGS, {ifr_name="eth0", ifr_flags=IFF_UP|IFF_BROADCAST|IFF_RUNNING|IFF_MULTICAST}) = 0
write(2, "eth0      no wireless extensions"..., 35eth0      no wireless extensions.

) = 35

请参见 SIOCGIWNAME用法:

#define SIOCGIWNAME 0x8B01 /* get name == wireless protocol */
/* SIOCGIWNAME is used to verify the presence of Wireless Extensions.
* Common values : "IEEE 802.11-DS", "IEEE 802.11-FH", "IEEE 802.11b"...

这篇关于确定网络接口是无线还是有线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆