ping

privex.helpers.net.ping(ip: str, timeout: int = 30)bool[source]

Sends a ping to a given IPv4 / IPv6 address. Tested with IPv4+IPv6 using iputils-ping on Linux, as well as the default IPv4 ping utility on Mac OSX (Mojave, 10.14.6).

Fully supported when using Linux with the iputils-ping package. Only IPv4 support on Mac OSX.

Example Usage:

>>> from privex.helpers import ping
>>> if ping('127.0.0.1', 5) and ping('::1', 10):
...     print('Both 127.0.0.1 and ::1 are up')
... else:
...     print('127.0.0.1 or ::1 failed to respond to a ping within the given timeout.')

Known Incompatibilities:

  • NOT compatible with IPv6 addresses on OSX due to the lack of a timeout argument with ping6

  • NOT compatible with IPv6 addresses when using inetutils-ping on Linux due to separate ping6 command

Parameters
  • ip (str) – An IP address as a string, e.g. 192.168.1.1 or 2a07:e00::1

  • timeout (int) – (Default: 30) Number of seconds to wait for a response from the ping before timing out

Raises

ValueError – When the given IP address ip is invalid or timeout < 1

Return bool

True if ping got a response from the given IP, False if not