Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python icmp socket server (not tcp\udp)

Tags:

python

sockets

I'm trying to write a socket server in Python that can receive ICMP packets.

Here's my code:

s = socket.socket(socket.AF_INET,socket.SOCK_RAW, socket.IPPROTO_ICMP)
host = socket.gethostbyname(socket.gethostname())
s.bind((host,0))
s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
s.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)

However, I dont know how to receive the packets - I tried using s.listen but it failed. What am I missing or am I completly in the wrong direction?

Thanks!

like image 403
user1062253 Avatar asked Feb 22 '23 08:02

user1062253


1 Answers

I've done this before in twisted and I set the socket up like this:

import socket

def listen():
  s = socket.socket(socket.AF_INET,socket.SOCK_RAW,socket.IPPROTO_ICMP)
  s.setsockopt(socket.SOL_IP, socket.IP_HDRINCL, 1)
  while 1:
    data, addr = s.recvfrom(1508)
    print "Packet from %r: %r" % (addr,data)
like image 140
MattH Avatar answered Mar 03 '23 03:03

MattH