Ping With Python

Ping With Python


4 min read

At one point in life, you will ping if you are a programmer, techy, network engineer or in the IT World. This simple command allows you to check if a remote device is alive, and it offers some other amenities.

๐ŸŒŸ What is Ping?

According to Tech Target, a ping (Packet Internet or Inter-Network Groper) is a basic Internet program that allows a user to test and verify if a particular destination IP address exists and can accept requests in computer network administration.


Ping measures the round-trip time for messages sent from the originating host to a destination computer that are echoed back to the source.

๐ŸŒŸ Windows Ping

In windows, you could simply ping in your terminal. Let's ping google


Output image.png

The command below returns the same output above since we have the IP translation of


What if you have a bunch of hosts or remote computers that you really want to check if they are online and running? You need to use some tools or you could write your own ping sweep programmatically.

This can also come in handy for pen-testers and network engineers who want to check for all subnets under a particular IP.

We could achieve the same output above by using the os module

๐ŸŒŸ os module

The OS module in Python provides functions for interacting with the operating system. OS comes under Python's standard utility modules.

import os
response = os.popen('ping -n 1')
for line in response.readlines():

Output; image.png The popen() function executes the command specified by the string command. It creates a pipe between the calling program and the executed command

Before I proceed, let me explain these major keywords that you will continuously come across.

TTL ๐Ÿ’จ Time to live (TTL) refers to the amount of time or โ€œhopsโ€ that a packet is set to exist inside a network before being discarded by a router.
byte ๐Ÿ’จ The payload of the packet.
timeout ๐Ÿ’จ How long before considering a non-received response lost, in seconds?
count ๐Ÿ’จ The number of ICMP packets to send in sequence.
ICMP ๐Ÿ’จ ICMP (Internet Control Message Protocol) is an error-reporting protocol that network devices such as routers use to generate error messages to the source IP address when network problems prevent the delivery of IP packets.
RTT ๐Ÿ’จ (Round-trip Time) Measure (in milliseconds) of the latency of a network โ€” that is, the time between initiating a network request and receiving a response.

๐ŸŒŸ pythonping

pythonping is a simple way to ping in Python. With it, you can send ICMP Probes to remote devices like you would do from the terminal.

You can simply grab it by pip install pythonping

Create a new file and we start writing some code.

from pythonping import ping
response_list = ping('', size=40, count=5)

With this ping, we sent 5 ICMP packets with a payload of 40 bytes to Google.

image.png To see our average RTT, we can print the rtt_avg_ms from the response_list object.


We could also ping a bunch of sites using a for loop;

target_hosts = ['',

def ping_all(targets):
    for target in targets:
        print(ping(target, size=40, count=2))


I will be writing how to identify hosts on a network and ping them using python subprocess and ipaddress modules in the another article in order to keep this short.

GitHub Repo ๐Ÿš€๐Ÿš€

๐ŸŒŸ Conclusion

Once again, hope you learned something today from my little closet.

Please consider subscribing or following me for related content, especially about Tech, Python & General Programming.

You can show extra love by buying me a coffee to support this free content and I am also open to partnerships, technical writing roles, collaborations and Python-related training or roles.

Buy Ronnie A Coffee ๐Ÿ“ข You can also follow me on Twitter : โ™ฅ โ™ฅ Waiting for you! ๐Ÿ™‚