Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4826626
  • 博文数量: 930
  • 博客积分: 12070
  • 博客等级: 上将
  • 技术积分: 11448
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-15 16:57
文章分类

全部博文(930)

文章存档

2011年(60)

2010年(220)

2009年(371)

2008年(279)

分类: LINUX

2009-05-07 15:44:25

[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 取消混杂  

阅读(4713) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~