Ronnie Atuhaire
Ronnie Atuhaire's Blog 🤓

Ronnie Atuhaire's Blog 🤓

Ping With Python

Ping With Python

Ronnie Atuhaire's photo
Ronnie Atuhaire
·Apr 18, 2022·

4 min read

Subscribe to my newsletter and never miss my upcoming articles

Table of contents

  • 🌟 What is Ping?
  • 🌟 Windows Ping
  • 🌟 os module
  • 🌟 pythonping
  • 🌟 Conclusion

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.

image.png

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

ping google.com

Output image.png

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

ping 216.58.223.78

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 google.com')
for line in response.readlines():
    print(line)

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('216.58.223.78', size=40, count=5)
print(response_list)

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.

print(response_list.rtt_avg_ms)

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

target_hosts = ['nytimes.com',
            'github.com',
            'google.com',
            'reddit.com',
            'hashnode.com',
            'producthunt.com']

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

ping_all(target_hosts)

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! 🙂
 
Share this