linux 查看网卡工作模式,linux中从文件获取当前网卡的工作模式

转自:http://bbs.chinaunix.net/thread-1940813-2-1.html

//copy by duanjigang from source code of ethtool 6

struct ethtool_cmd

{

__u32   cmd;

__u32   supported;  /* Features this interface supports */

__u32   advertising;    /* Features this interface advertises */

__u16   speed;      /* The forced speed, 10Mb, 100Mb, gigabit */

__u8    duplex;     /* Duplex, half or full */

__u8    port;       /* Which connector port */

__u8    phy_address;

__u8    transceiver;    /* Which transceiver to use */

__u8    autoneg;    /* Enable or disable autonegotiation */

__u32   maxtxpkt;   /* Tx pkts before generating tx int */

__u32   maxrxpkt;   /* Rx pkts before generating rx int */

__u32   reserved[4];

};

int get_settings(const char* devname)

{

struct ifreq ifr;

int fd;

struct ethtool_cmd ecmd;

int allfail = 1;

/* Setup our control structures. */

memset(&ifr, 0, sizeof(ifr));

strcpy(ifr.ifr_name, devname);

fd = socket(AF_INET, SOCK_DGRAM, 0);

if (fd < 0)

{

return -1;

}

ecmd.cmd = 1;

ifr.ifr_data = (caddr_t)&ecmd;

if(ioctl(fd, SIOCETHTOOL, &ifr))

{

close(fd);

return -1;

}

//duplex 0 half 1 full

fprintf(stdout, "Settings for %s: speed:%d, duplex:%s\n", devname, ecmd.speed, ecmd.duplex ? "full" : "half");

close(fd);

return 1;

}

http://hi.baidu.com/deep_pro/blog/item/8473bed9df3bce2010df9bfc.html

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

typedef unsigned short u16;

typedef unsigned int u32;

typedef unsigned char u8;

#include

#include

int get_settings(const char* devname)

{

struct ifreq ifr;

int fd;

struct ethtool_cmd ecmd;

int allfail = 1;

/* Setup our control structures. */

memset(&ifr, 0, sizeof(ifr));

strcpy(ifr.ifr_name, devname);

fd = socket(AF_INET, SOCK_DGRAM, 0);

if (fd < 0)

{

return -1;

}

ecmd.cmd = 1;

ifr.ifr_data = (caddr_t)&ecmd;

if(ioctl(fd, SIOCETHTOOL, &ifr))

{

close(fd);

perror("SIOCETHTOOL");

return -1;

}

//duplex 0 half 1 full

fprintf(stdout, "Settings for %s: speed:%d, duplex:%s\n", devname, ecmd.speed,

ecmd.duplex ? "full" : "half");

close(fd);

return 1;

}

int main(int argc, char *argv[])

{

int ret =get_settings(argv[1]);

printf("%d\n",ret);

return EXIT_SUCCESS;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值