Skip to content

Instantly share code, notes, and snippets.

@andrewn
Created January 26, 2018 11:35
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Do something when Pi's being being ping'd
#!/usr/bin/python
import socket
# Open a raw socket listening on all ip addresses
sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP)
sock.bind(('', 1))
try :
while True :
# receive data
data = sock.recv(1024)
# ip header is the first 20 bytes
ip_header = data[:20]
# ip source address is 4 bytes and is second last field (dest addr is last)
ips = ip_header[-8:-4]
# convert to dotted decimal format
source = '%i.%i.%i.%i' % (ord(ips[0]), ord(ips[1]), ord(ips[2]), ord(ips[3]))
# Flash LED
# Play a sound
print 'Ping from %s' % source
except KeyboardInterrupt :
print ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment