Skip to content

Instantly share code, notes, and snippets.

@andrewn
Created January 26, 2018 11:35
Show Gist options
  • Select an option

  • Save andrewn/b5069ed83a0be201ee732beb2fd06f53 to your computer and use it in GitHub Desktop.

Select an option

Save andrewn/b5069ed83a0be201ee732beb2fd06f53 to your computer and use it in GitHub Desktop.

Revisions

  1. andrewn created this gist Jan 26, 2018.
    2 changes: 2 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@

    http://forums.whirlpool.net.au/archive/1473574
    27 changes: 27 additions & 0 deletions receive_ping.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    #!/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 ''