linux 判断网卡类型 有线 无线
查看所有网络接口ls /sys/class/net/要区分无线网卡则可以在/sys/class/net/${ifname}/下面找 phy80211 或者 wireless 文件夹,无线网卡目录下才存在这些文件夹通过C函数来查看:#include <stdio.h>#include <string.h>#include <unistd.h>#include &l
·
查看所有网络接口
ls /sys/class/net/
要区分无线网卡则可以在
/sys/class/net/${ifname}/
下面找 phy80211 或者 wireless 文件夹,无线网卡目录下才存在这些文件夹
通过C函数来查看:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netpacket/packet.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <err.h>
int device_get_hwinfo(int fd, char* ifname)
{
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0)
{
err(1, "Could not get arptype");
return -1;
}
return ifr.ifr_hwaddr.sa_family; //this value
}
int main(int argc, char* argv[])
{
int fd, if_type = -1;
char *if_name = "eth0";
fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if (fd < 0) {
printf("Could not create packet socket! Please run horst as root!\n");
return -1;
}
if_type = device_get_hwinfo(fd, if_name);
printf("%s type is %d\n", if_name, if_type);
return 0;
}
得到网卡列表:
#include <net/if.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int get_if_name(char ifstr[][32])
{
struct if_nameindex *if_ni, *i;
int num = 0;
if_ni = if_nameindex();
if (if_ni == NULL) {
perror("if_nameindex");
exit(EXIT_FAILURE);
}
for (i = if_ni; ! (i->if_index == 0 && i->if_name == NULL); i++)
{
if(strstr(i->if_name,"eth") != NULL) //比较网卡名得到有线网卡,不可靠,得到全部就注释掉此行
{
strcpy(ifstr[num], i->if_name);
++num;
}
else
printf("%u: %s\n", i->if_index, i->if_name);
}
if_freenameindex(if_ni);
return num;
}
/**
* 通过interface name 调用iocontrol查询网卡是否是无线网卡,执行该程序需要引用wireless.h,
* 如果编译时报错 删除其中的"__user" 或者 直接定义为空,这是内核中用到的用户空间属性
*/
int if_is_wlif(const char * ifname)
{
int skfd, ret = 0;
struct iwreq wrq;
/* Set device name */
strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
skfd = socket(AF_INET, SOCK_DGRAM, 0);
if(skfd < 0)
return ret;
/* Get wireless name */
ret = ioctl(skfd, SIOCGIWNAME, &wrq);
close(skfd);
/* If no wireless name : no wireless extensions */
if(ret < 0)
return 0;
else
return 1;
}
int main(int argc, char *argv[])
{
char ifname[10][32];
int num, i;
num = get_if_name(ifname);
printf("wlan num %d\n", num);
for( i=0; i<num; ++i)
{
printf("wlan if name %s\n", ifname[i]);
}
/* 注释部分 直接打印出网卡列表未存储到变量中
struct if_nameindex *if_ni, *i;
if_ni = if_nameindex();
if (if_ni == NULL) {
perror("if_nameindex");
exit(EXIT_FAILURE);
}
for (i = if_ni; ! (i->if_index == 0 && i->if_name == NULL); i++)
printf("%u: %s\n", i->if_index, i->if_name);
if_freenameindex(if_ni);
*/
exit(EXIT_SUCCESS);
}
查看设备下的网卡可以通过筛选类型值来判断网卡类型
cat /sys/class/net/eth0/type
1
网卡类型的值是 1
无线网卡 Managed
和 Master
模式的值也是 1
,当是 Monitor
模式时值才为 802 或 803
设备类型值列表:
/* ARP protocol HARDWARE identifiers. */
#define ARPHRD_NETROM 0 /* from KA9Q: NET/ROM pseudo */
#define ARPHRD_ETHER 1 /* Ethernet 10Mbps */
#define ARPHRD_EETHER 2 /* Experimental Ethernet */
#define ARPHRD_AX25 3 /* AX.25 Level 2 */
#define ARPHRD_PRONET 4 /* PROnet token ring */
#define ARPHRD_CHAOS 5 /* Chaosnet */
#define ARPHRD_IEEE802 6 /* IEEE 802.2 Ethernet/TR/TB */
#define ARPHRD_ARCNET 7 /* ARCnet */
#define ARPHRD_APPLETLK 8 /* APPLEtalk */
#define ARPHRD_DLCI 15 /* Frame Relay DLCI */
#define ARPHRD_ATM 19 /* ATM */
#define ARPHRD_METRICOM 23 /* Metricom STRIP (new IANA id) */
#define ARPHRD_IEEE1394 24 /* IEEE 1394 IPv4 - RFC 2734 */
#define ARPHRD_EUI64 27 /* EUI-64 */
#define ARPHRD_INFINIBAND 32 /* InfiniBand */
/* Dummy types for non ARP hardware */
#define ARPHRD_SLIP 256
#define ARPHRD_CSLIP 257
#define ARPHRD_SLIP6 258
#define ARPHRD_CSLIP6 259
#define ARPHRD_RSRVD 260 /* Notional KISS type */
#define ARPHRD_ADAPT 264
#define ARPHRD_ROSE 270
#define ARPHRD_X25 271 /* CCITT X.25 */
#define ARPHRD_HWX25 272 /* Boards with X.25 in firmware */
#define ARPHRD_CAN 280 /* Controller Area Network */
#define ARPHRD_PPP 512
#define ARPHRD_CISCO 513 /* Cisco HDLC */
#define ARPHRD_HDLC ARPHRD_CISCO
#define ARPHRD_LAPB 516 /* LAPB */
#define ARPHRD_DDCMP 517 /* Digital's DDCMP protocol */
#define ARPHRD_RAWHDLC 518 /* Raw HDLC */
#define ARPHRD_TUNNEL 768 /* IPIP tunnel */
#define ARPHRD_TUNNEL6 769 /* IP6IP6 tunnel */
#define ARPHRD_FRAD 770 /* Frame Relay Access Device */
#define ARPHRD_SKIP 771 /* SKIP vif */
#define ARPHRD_LOOPBACK 772 /* Loopback device */
#define ARPHRD_LOCALTLK 773 /* Localtalk device */
#define ARPHRD_FDDI 774 /* Fiber Distributed Data Interface */
#define ARPHRD_BIF 775 /* AP1000 BIF */
#define ARPHRD_SIT 776 /* sit0 device - IPv6-in-IPv4 */
#define ARPHRD_IPDDP 777 /* IP over DDP tunneller */
#define ARPHRD_IPGRE 778 /* GRE over IP */
#define ARPHRD_PIMREG 779 /* PIMSM register interface */
#define ARPHRD_HIPPI 780 /* High Performance Parallel Interface */
#define ARPHRD_ASH 781 /* Nexus 64Mbps Ash */
#define ARPHRD_ECONET 782 /* Acorn Econet */
#define ARPHRD_IRDA 783 /* Linux-IrDA */
/* ARP works differently on different FC media .. so */
#define ARPHRD_FCPP 784 /* Point to point fibrechannel */
#define ARPHRD_FCAL 785 /* Fibrechannel arbitrated loop */
#define ARPHRD_FCPL 786 /* Fibrechannel public loop */
#define ARPHRD_FCFABRIC 787 /* Fibrechannel fabric */
/* 787->799 reserved for fibrechannel media types */
#define ARPHRD_IEEE802_TR 800 /* Magic type ident for TR */
#define ARPHRD_IEEE80211 801 /* IEEE 802.11 */
#define ARPHRD_IEEE80211_PRISM 802 /* IEEE 802.11 + Prism2 header */
#define ARPHRD_IEEE80211_RADIOTAP 803 /* IEEE 802.11 + radiotap header */
#define ARPHRD_IEEE802154 804
#define ARPHRD_PHONET 820 /* PhoNet media type */
#define ARPHRD_PHONET_PIPE 821 /* PhoNet pipe header */
#define ARPHRD_CAIF 822 /* CAIF media type */
#define ARPHRD_VOID 0xFFFF /* Void type, nothing is known */
#define ARPHRD_NONE 0xFFFE /* zero header length */
/* ARP protocol opcodes. */
#define ARPOP_REQUEST 1 /* ARP request */
#define ARPOP_REPLY 2 /* ARP reply */
#define ARPOP_RREQUEST 3 /* RARP request */
#define ARPOP_RREPLY 4 /* RARP reply */
#define ARPOP_InREQUEST 8 /* InARP request */
#define ARPOP_InREPLY 9 /* InARP reply */
#define ARPOP_NAK 10 /* (ATM)ARP NAK */
更多推荐
已为社区贡献89条内容
所有评论(0)