

# The ping command is the same for Windows and Linux, except for the "number of packets" flag. Python 3.5+, Windows and Linux compatible Optional parameters: packets (int, number of retries), timeout (int, ms to wait for response)ĭoes not show any output, either as popup window or in command line. Required parameter: host_or_ip (str, address of host to ping) ''' Calls system "ping" command, returns True if ping succeeds.

On Windows, returns False if the ping command fails with "Destination Host Unreachable".Works on Python 3.5 and later, on Windows and Linux.Subprocess.call() performs a system call. Platform.system() returns the platform name. The option -n (Windows) or -c (Unix) controls the number of packets which in this example was set to 1.

The command is ping in both Windows and Unix-like systems. Note that, according to on Windows this function will still return True if you get a Destination Host Unreachable error. Param = '-n' if platform.system().lower()='windows' else '-c' # Option for the number of packets as a function of Remember that a host may not respond to a ping (ICMP) request even if the host name is valid. Returns True if host (str) responds to a ping request. Import subprocess # For executing a shell command import platform # For getting the operating system name This avoids shell injection vulnerability in cases where your hostname string might not be validated. This function works in any OS (Unix, Linux, macOS, and Windows)īy os.system was replaced by subprocess.call.
