title: Python sockets: how to enable promiscuous mode in linux
body: <p>According to python documentation we can build simple sniffer like :</p>
  
  <pre><code>import socket
  # the public network interface
  HOST = socket.gethostbyname(socket.gethostname())
  # create a raw socket and bind it to the public interface
  s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)
  s.bind((HOST, 0))
  # Include IP headers
  s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
  # receive all packages
  s.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)
  # receive a package
  print s.recvfrom(65565)
  # disabled promiscuous mode
  s.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)
  </code></pre>
  
  <p>for the windows platform but in linux <code>socket.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)</code> doesn't work.</p>
  
  <p>How that example will looks for linux platform ? How I could set promiscuous mode in Linux?</p>
  
  <p>EDIT</p>
  
  <p>I've got a message :</p>
  
  <pre><code>Traceback (most recent call last):
    File "b.py", line 46, in &lt;module&gt;
      sniffer(count=10,showPort=True,showRawData=True)
    File "b.py", line 12, in sniffer
      s.bind((HOST, 0))
    File "/usr/lib/python2.7/socket.py", line 224, in meth
      return getattr(self._sock,name)(*args)
  socket.error: [Errno 19] No such device
  </code></pre>
  
  <p>When I did a code of @Christian-James-Bell with some changes :</p>
  
  <pre><code>import socket
  
  def sniffer(count, bufferSize=65565, showPort=False, showRawData=False):
      # the public network interface
      HOST = socket.gethostbyname(socket.gethostname())
  
      # create a raw socket and bind it to the public interface
      s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.IPPROTO_IP)
  
      # prevent socket from being left in TIME_WAIT state, enabling reuse
      s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
      s.bind((HOST, 0))
  
      # Include IP headers
      s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
  
      # receive all packages
      s.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)
  
      for i in range(count):
  
          # receive a package
          package = s.recvfrom(bufferSize)
          printPacket(package, showPort, showRawData)
  
      # disabled promiscuous mode
      s.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)
  
  def printPacket(package, showPort, showRawData):
  
      # index values for (data, header) tuple
      dataIndex = 0
      headerIndex = 1
  
      # index values for (ipAddress, port) tuple
      ipAddressIndex = 0
      portIndex = 1
  
      print('IP:', package[headerIndex][ipAddressIndex])
      if(showPort):
          print('Port:', package[headerIndex][portIndex])           
          print ('') #newline
      if(showRawData):
          print ('Data:', package[dataIndex])
  
  sniffer(count=10,showPort=True,showRawData=True)
  </code></pre>
  
  <p>Anybody have any idea whats wrong ?</p>