ifconfig eth1 promisc 设置混杂模式
ifconfig eth1 -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 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;
- }