Created
January 26, 2018 11:35
-
-
Save andrewn/b5069ed83a0be201ee732beb2fd06f53 to your computer and use it in GitHub Desktop.
Do something when Pi's being being ping'd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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