Support root-less ICMP pings on MacOS and Linux
See original GitHub issueProvided the Linux kernel is configured to allow non-raw ICMP for your group ID (on macOS it is available by default, you can use a SOCK_DGRAM
socket to ping without being root.
I can demonstrate this on my macOS system:
>>> import os
>>> os.getuid() # not root
501
>>> os.uname() # on MacOS
posix.uname_result(sysname='Darwin', nodename='Martijns-MacBook-Pro-2.local', release='17.7.0', version='Darwin Kernel Version 17.7.0: Thu Dec 20 21:47:19 PST 2018; root:xnu-4570.71.22~1/RELEASE_X86_64', machine='x86_64')
>>> import socket, threading, ping3
>>> ping3.DEBUG = True
>>> sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_ICMP)
>>> sock.setsockopt(socket.SOL_IP, socket.IP_TTL, 64)
>>> icmp_id = threading.current_thread().ident % 0xFFFF
>>> if True:
... ping3.send_one_ping(sock=sock, dest_addr='192.168.1.160', icmp_id=icmp_id, seq=0, size=56)
... res = ping3.receive_one_ping(sock=sock, icmp_id=icmp_id, seq=0, timeout=4)
...
[DEBUG] IP HEADER: {'version': 69, 'tos': 0, 'len': 14336, 'id': 63036, 'flags': 0, 'ttl': 64, 'protocol': 1, 'checksum': 65435, 'src_addr': 3232235936, 'dest_addr': 3232236008}
[DEBUG] ICMP HEADER: {'type': 0, 'code': 0, 'checksum': 8346, 'id': 61668, 'seq': 0}
>>> res
0.0025510787963867188
Linux enforces a suitable ICMP ID and checksum for you; there is no point in generating your own checksum (it is ignored and re-calculated) and you should not check for the ICMP ID when receiving packets as the kernel routes the packets to your socket based on their internal bookkeeping, instead.
I haven’t found any proof that macOS does this however, so some platform-specific code would be needed to handle this difference. This can be as simple as ignoring the ICMP id when on Linux and not root.
Also see the Linux man 7 icmp
documentation.
Could support for this be implemented in ping3?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:8
- Comments:8 (4 by maintainers)
Top GitHub Comments
Thanks for the information. I will look into this issue and see what I can do with it.
In another hand, I’d love to keep ping3 as an universal solution on all platforms by using python standard libraries. I had to investigate carefully before implementing any platform-specified codes.
@clockzhong please read my post carefully and verify that your kernel and groups have been configured correctly: