Ready to Start Your Career?

Ping Using Python Script

Amine Essiraj - 4m1n3 3551r4j's profile image

By: Amine Essiraj - 4m1n3 3551r4j

December 31, 2020

Nowadays, people use scripts for automating almost every day-to-day task. It is essential to automate tasks to increase productivity and reduce the time taken to run the same job repeatedly. People use different scripting languages to automate their functions, including Bash, Python, Javascript and Go. Many simple tasks can be automated using these languages.

As a security professional, the essential task that one encounters is checking whether a website or a system is up or not. This can be done by pinging the IP address of the system or website. One can create a simple script to automate the pinging of a system. This article will cover creating a python script to ping IP addresses.

Ping Basics

According to Wikipedia, “Ping or Packet InterNet Groper is a computer network administration software utility used to test the reachability of a host on an Internet Protocol (IP) network. It is available for virtually all operating systems with networking capability, including most embedded network administration software.”

The ping tool uses the Internet Control Message Protocol (ICMP) Echo function to check whether a system or website is up or not. It sends an Echo request to the server and waits for the reply. If the server sends the answer, then it means that the server is up and reachable. Additionally, ping can also tell how much time it takes to reach the server and resolve the hostname to an IP address.

Python Basics

Before diving into how to create a python script for pinging IP addresses, let’s see what libraries must fulfill this task. ‘ping’ utility comes pre-installed in every Windows and Linux system. Here, the basic idea is creating a script that executes the ‘ping’ utility from a terminal or a command prompt. To do this, libraries like ‘os’ or ‘subprocess’ come into play.

This article will cover using both the ‘os’ and ‘subprocess’ libraries to ping an IP address. First of all, let’s see how both libraries are used to execute a simple terminal or command prompt command.

To execute a simple command ‘ls -l’ using ‘os’ library:

import os

stream = os.popen('ls -l') output = print(output)

To execute a simple command ‘ls -l’ using ‘subprocess’ library:

import subprocess

output = subprocess.check_output(['ls', '-l']) print(output)

Python Ping Script

This article will utilize ping in the following way: ping -c 4 <ip_address>

The -c is used to stop after n counts.

Ping using os command:

import os

ip_addr = input('Enter the IP address:') stream = os.popen('ping -c 4 {}'.format(ip_addr)) output = if '0 received' in output: print('IP unreachable') else: print('IP reachable') print(output)

Ping using subprocess command: import subprocess

ip_addr = input('Enter the IP address:') try: output = subprocess.check_output(['ping', '-c 4', '{}'.format(ip_addr)]) if '0 received' in output: print('IP unreachable') else: print('IP reachable') print(output) except: print('IP unreachable')


This article covers how to create a simple python script to use ping utility. There are many possibilities with scripting languages, and one can make many simple scripts to automate daily tasks.

Schedule Demo