Package: asyncping3
AsyncPingAsyncPing is an async-friendly pure python3 version of ICMP ping implementation using raw sockets.(Note that on Linux and Windows, ICMP messages can only be sent from processes running as root.)The Python2 version originally fromhere.The Python3 version originally fromhere.This version maintained atthis github repo.Get StartedIf you met "permission denied", you may need to run this as root.pipinstallasyncping3>>>fromasyncping3importping,verbose_ping>>>anyio.run(ping,'example.com')# Returns delay in seconds.0.215697261510079666>>>anyio.run(verbose_ping,'example.com')# Ping 4 times in a row.ping'example.com'...215msping'example.com'...216msping'example.com'...219msping'example.com'...217ms$ppingexample.com# Verbose ping.ping'example.com'...215ms
ping'example.com'...216ms
ping'example.com'...219ms
ping'example.com'...217msInstallationpipinstallasyncping3# install asyncping3pipinstall--upgradeasyncping3# upgrade asyncping3pipuninstallasyncping3# uninstall asyncping3Functions>>>ping('example.com')# Returns delay in seconds.0.215697261510079666>>>ping('not.exist.com')# if host unknown (cannot resolve), returns FalseFalse>>>ping("224.0.0.0")# If timed out (no reply), returns NoneNone>>>ping('example.com',timeout=10)# Set timeout to 10 seconds. Default timeout=4 for 4 seconds.0.215697261510079666>>>ping('example.com',unit='ms')# Returns delay in milliseconds. Default unit='s' for seconds.215.9627876281738>>>ping('example.com',src_addr='192.168.1.15')# WINDOWS ONLY. Set source ip address for multiple interfaces. Default src_addr=None for no binding.0.215697261510079666>>>ping('example.com',interface='eth0')# LINUX ONLY. Set source interface for multiple network interfaces. Default interface=None for no binding.0.215697261510079666>>>ping('example.com',ttl=5)# Set packet Time-To-Live to 5. The packet is discarded if it does not reach the target host after 5 jumps. Default ttl=64.None>>>ping('example.com',size=56)# Set ICMP packet payload to 56 bytes. The total ICMP packet size is 8 (header) + 56 (payload) = 64 bytes. Default size=56.0.215697261510079666>>>verbose_ping('example.com')# Ping 4 times in a row.ping'example.com'...215msping'example.com'...216msping'example.com'...219msping'example.com'...217ms>>>verbose_ping('example.com',timeout=10)# Set timeout to 10 seconds. Default timeout=4 for 4 seconds.ping'example.com'...215msping'example.com'...216msping'example.com'...219msping'example.com'...217ms>>>verbose_ping('example.com',count=6)# Ping 6 times. Default count=4.ping'example.com'...215msping'example.com'...216msping'example.com'...219msping'example.com'...217msping'example.com'...215msping'example.com'...216ms>>>verbose_ping('example.com',count=0)# Ping endlessly (0 means infinite loops). Using `ctrl + c` to stop manully.ping'example.com'...215ms...>>>verbose_ping('example.com',src_addr='192.168.1.15')# WINDOWS ONLY. Ping from source IP address. Default src_addr=Noneping'example.com'from'192.168.1.15'...215msping'example.com'from'192.168.1.15'...216msping'example.com'from'192.168.1.15'...219msping'example.com'from'192.168.1.15'...217ms>>>verbose_ping('example.com',interface='wifi0')# LINUX ONLY. Ping from network interface 'wifi0'. Default interface=Noneping'example.com'from'192.168.1.15'...215msping'example.com'from'192.168.1.15'...216msping'example.com'from'192.168.1.15'...219msping'example.com'from'192.168.1.15'...217ms>>>verbose_ping('example.com',unit='s')# Displays delay in seconds. Default unit="ms" for milliseconds.ping'example.com'...1sping'example.com'...2sping'example.com'...1sping'example.com'...1s>>>verbose_ping('example.com',ttl=5)# Set TTL to 5. Default is 64.ping'example.com'...Timeoutping'example.com'...Timeoutping'example.com'...Timeoutping'example.com'...Timeout>>>verbose_ping('example.com',interval=5)# Wait 5 seconds between each packet. Default is 0.ping'example.com'...215ms# wait 5 secsping'example.com'...216ms# wait 5 secsping'example.com'...219ms# wait 5 secsping'example.com'...217ms>>>verbose_ping('example.com',size=56)# Set ICMP payload to 56 bytes. Default size=56.ping'example.com'...215msping'example.com'...216msping'example.com'...219msping'example.com'...217msDEBUG modeShow more info for developers.>>>importasyncping3>>>asyncping3.DEBUG=True# Default is False.>>>asyncping3.ping("example.com")# "ping()" prints received IP header and ICMP header.[DEBUG]IPHEADER:{'version':69,'tos':0,'len':14336,'id':8620,'flags':0,'ttl':51,'protocol':1,'checksum':*,'src_addr':*,'dest_addr':*}[DEBUG]ICMPHEADER:{'type':0,'code':0,'checksum':8890,'id':21952,'seq':0}0.215697261510079666>>>asyncping3.ping("example.com",timeout=0.0001)[DEBUG]RequesttimeoutforICMPpacket.(Timeout=0.0001s)None>>>asyncping3.ping("not.exist.com")[DEBUG]Cannotresolve:Unknownhost.(Host=not.exist.com)False>>>asyncping3.ping("example.com",ttl=1)[DEBUG]Timeexceeded:TimeToLiveexpired.NoneEXCEPTIONS modeRaise exceptions when there are errors instead of return None>>>importasyncping3>>>asyncping3.EXCEPTIONS=True# Default is False.>>>asyncping3.ping("example.com",timeout=0.0001)# All Exceptions are subclasses of PingError[...Traceback...]error.Timeout:RequesttimeoutforICMPpacket.(Timeout=0.0001s)>>>asyncping3.ping("not.exist.com")[...Traceback...]error.HostUnknown:Cannotresolve:Unknownhost.(Host=not.exist.com)>>>asyncping3.ping("example.com",ttl=1)[...Traceback...]error.TimeToLiveExpired:Timeexceeded:TimeToLiveexpired.Command Line ExecutionExecuteppingfrom command-line.
Note:ppingneedsrootprivilege to send/receive packets. You may want to usesudo pping.$pping--help# -h/--help. Command-line help message.$pping-v# -v/--version. Show asyncping3 version number.3.0.0

$ppingexample.com# Verbose ping.ping'example.com'...215ms
ping'example.com'...216ms
ping'example.com'...219ms
ping'example.com'...217ms

$ppingexample.com8.8.8.8# Verbose ping all the addresses in parallel.ping'example.com'...215ms
ping'example.com'...217ms
ping'8.8.8.8'...5ms
ping'example.com'...216ms
ping'8.8.8.8'...2ms
ping'8.8.8.8'...6ms
ping'example.com'...219ms
ping'8.8.8.8'...5ms

$pping-c1example.com# -c/--count. How many pings should be sent.  Default is 4.ping'example.com'...215ms

$pping-c0example.com# Ping endlessly (0 means infinite loops). Using `ctrl + c` to stop manully.ping'example.com'...215ms
...

$pping-w10example.com# -w/--wait. Set timeout to 10 seconds. Default is 4.ping'example.com'...215ms
ping'example.com'...216ms
ping'example.com'...219ms
ping'example.com'...217ms

$pping-t5example.com# -t/--ttl. # Set TTL to 5. Default is 64.ping'example.com'...Timeout
ping'example.com'...Timeout
ping'example.com'...Timeout
ping'example.com'...Timeout

$pping-l56example.com# -l/--load. Set ICMP packet payload to 56 bytes. Default is 56.ping'example.com'...215ms
ping'example.com'...216ms
ping'example.com'...219ms
ping'example.com'...217ms

$pping-i5example.com# -i/--interval. Wait 5 seconds between each packet. Default is 0.ping'example.com'...215ms# wait 5 secsping'example.com'...216ms# wait 5 secsping'example.com'...219ms# wait 5 secsping'example.com'...217ms

$pping--exceptions--wait0.001example.com# EXCPETIONS mode is on when --exceptions shows up.[...Traceback...]error.Timeout:RequesttimeoutforICMPpacket.(Timeout=0.0001s)$pping--debug--wait0.001example.com# DEBUG mode is on when --debug shows up.[DEBUG]RequesttimeoutforICMPpacket.(Timeout=0.001s)ping'example.com'...Timeout>0.001s[DEBUG]RequesttimeoutforICMPpacket.(Timeout=0.001s)ping'example.com'...Timeout>0.001s[DEBUG]RequesttimeoutforICMPpacket.(Timeout=0.001s)ping'example.com'...Timeout>0.001s[DEBUG]RequesttimeoutforICMPpacket.(Timeout=0.001s)ping'example.com'...Timeout>0.001s