转自:
www.cublog.cn/u3/104357/showart_2151020.html#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 取消混杂