设置网卡混杂模式


设置网卡混杂模式
 
   有时候为嗅探到网络上的数据,需要将网卡设置到混杂模式。进入该模式将网络上
的数据一并抓获,为此在设置nic的混杂模式的时候有诸多方法?通过shell命令来实现:
ifconfig eth1 promisc  设置混杂模式
ifconfig eth1 -promisc 取消混杂模式
  www.2cto.com  
执行结果如下
[root@localhost tftpboot]# ifconfig
eth6      Link encap:Ethernet  HWaddr 08:00:27:70:1D:79  
          inet6 addr: fe80::a00:27ff:fe70:1d79/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:100124 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8795 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:12986638 (12.3 MiB)  TX bytes:6452270 (6.1 MiB)
 
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:1303 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1303 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:303973 (296.8 KiB)  TX bytes:303973 (296.8 KiB)
 
[root@localhost tftpboot]# ifconfig eth6 promisc
[root@localhost tftpboot]# ifconfig
eth6      Link encap:Ethernet  HWaddr 08:00:27:70:1D:79  
          inet6 addr: fe80::a00:27ff:fe70:1d79/64 Scope:Link
          UP BROADCAST RUNNING PROMISC MULTICAST  MTU:1500  Metric:1
          RX packets:100154 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8795 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:13007885 (12.4 MiB)  TX bytes:6452270 (6.1 MiB)
 
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:1303 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1303 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:303973 (296.8 KiB)  TX bytes:303973 (296.8 KiB)
 
[root@localhost tftpboot]# 
 
也可以通过C语言方式 编程 来实现,
 
#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>
 
#define ETH_NAME    "eth1"
 
int do_promisc(void) {
    int f, s;
    struct ifreq ifr;
    if ( (f=socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)))<0){ 
        return -1;
    }
    strcpy(ifr.ifr_name, ETH_NAME);
    
    if ((s = ioctl(f, SIOCGIFFLAGS, &ifr))<0){
      close(f);
      return-1;
    }
    
    if(ifr.ifr_flags & IFF_RUNNING){
        printf("eth link up\n");
    }else{
        printf("eth link down\n");
    }
  
    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 check_nic(void)
{
    struct ifreq ifr;
    int skfd = socket(AF_INET, SOCK_DGRAM, 0);
 
    strcpy(ifr.ifr_name, ETH_NAME);
    if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0)
    {
        close(skfd);
        return -1;
    }
    if(ifr.ifr_flags & IFF_RUNNING){
        printf("link up\n");
        close(skfd);
        return 0; // 网卡已插上网线
    }else {
        printf("link down\n");
        close(skfd);
        return -1;
    }
}
 
 
 
int main(void) {
    do_promisc();
    return 0;
}
 

转载于:https://my.oschina.net/u/138210/blog/110517

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值