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
The command below returns the same output above since we have the IP translation of google.com.
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
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)
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 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('220.127.116.11', size=40, count=5) print(response_list)
With this ping, we sent 5 ICMP packets with a payload of 40 bytes to Google.
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 = ['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
ipaddress modules in the another article in order to keep this short.
GitHub Repo 🚀🚀
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.