ImportSocketImportThreadingImportTimeactivedegree=dict () flag=1defMain ():GlobalActivedegreeGlobalGlag#get the native IP addresshost=Socket.gethostbyname (Socket.gethostbyname ())#create raw sockets for Windows platforms #for other systems, the socket is to be put. Ipproto_ip replaced by socket.ipproto_icmps=Socket.socket (Socket.af_inet,socket. Sock_raw,socket. IPPROTO_IP) S.bind ((host,0) )#set the IP header to be included in the capture packetS.setsockopt (socket. Ipproto_ip,socket. ip_hdrincl,1) #enable promiscuous mode to capture all packetsS.ioctl (socket. Sio_rcvall,socket. RCVALL_ON)#Start capturing packets whileFlag:c=s.recvfrom (65535) Host=c[1][0] Activedegree[host]=activedegree.get (host,0) +1#Suppose the native IP address is 10.2.1.8 ifC[1][0]! ='10.2.1.8': Print(c)#turn off promiscuous modeS.ioctl (socket. Tio_rcvall,socket. Rcvall_off) S.close () T=threading. Thread (target=main) T.start () Time.sleep (60) Flag=0t.join () forIteminchActivedegree.items ():Print(item)
Python_sniffer (Network Sniffer)