免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1726 | 回复: 0
打印 上一主题 下一主题

linux 设置网卡模式为混杂模式 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-01-16 22:19 |只看该作者 |倒序浏览
[root@mip-123456 ioctl]# cat promisc.c
#include sys/types.h>
#include stdio.h>
#include unistd.h>
#include sys/socket.h>
#include netinet/in.h>
#include arpa/inet.h>
#include errno.h>
#include linux/if_ether.h>
#include net/if.h>
#include sys/ioctl.h>
#include string.h>
int do_promisc()
{
  int f, s;
  struct ifreq ifr;
  
  if ((f = socket(AF_INET, SOCK_PACKET, htons(ETH_P_IP)))  0)
    return -1;
  strcpy(ifr.ifr_name, "eth0");
  
  if ((s = ioctl(f, SIOCGIFFLAGS, &ifr))  0)
    {
      close(f);
      return-1;
    }
  
ifr.ifr_flags |= IFF_PROMISC;
if ((s = ioctl(f, SIOCSIFFLAGS, &ifr))  0)
    {
      return -1;
    }
  printf("Setting interface ::: %s ::: to promisc\n\n", ifr.ifr_name);
  
  return 0;
}

int main()
{
  do_promisc();
  return 0;
}
[root@mip-123456 ioctl]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:22:68:3C:9C:F0  
          inet addr:172.24.149.212  Bcast:172.24.149.255  Mask:255.255.255.0
          inet6 addr: fe80::222:68ff:fe3c:9cf0/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3643 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6629 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:6253468 (5.9 MiB)  TX bytes:2379136 (2.2 MiB)
          Interrupt:233 Base address:0xa000
[root@mip-123456 ioctl]# ./promisc
Setting interface ::: eth0 ::: to promisc
[root@mip-123456 ioctl]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:22:68:3C:9C:F0  
          inet addr:172.24.149.212  Bcast:172.24.149.255  Mask:255.255.255.0
          inet6 addr: fe80::222:68ff:fe3c:9cf0/64 Scope:Link
          UP BROADCAST RUNNING PROMISC MULTICAST  MTU:1500  Metric:1
          RX packets:3653 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6630 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:6255432 (5.9 MiB)  TX bytes:2379200 (2.2 MiB)
          Interrupt:233 Base address:0xa000
其实可以直接ifconfig eth0 promisc 设置混杂
      ifconfig eth0 -promisc 取消混杂  


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/104357/showart_2151020.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP