Operations 7 min read

Building a Simple Network Monitoring Tool with Python and ping3

This tutorial explains how to develop a lightweight Python network monitoring utility using the ping3 library to send ICMP echo requests, measure latency, handle errors, and extend the script for batch pinging, timeout configuration, result logging, and packet‑loss calculation.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Building a Simple Network Monitoring Tool with Python and ping3

This article demonstrates how to create a simple network monitoring tool in Python using the ping3 library to send ICMP echo requests and measure latency.

First, install the library with pip install ping3 .

A basic script imports ping3, defines a ping(host) function that calls ping3.ping , handles success, failure, and exceptions, and runs from a main block that prompts for a host address.

The tool can be run via python ping_tool.py , which asks for the target host and displays the result.

Additional examples show how to ping multiple hosts, set timeout and retry count, compute average latency, write results to a file, and calculate packet loss, each with full source code:

import ping3

def ping(host):
    try:
        latency = ping3.ping(host)
        if latency is not None:
            print(f"Ping {host} 成功,延迟:{latency}秒")
        else:
            print(f"Ping {host} 失败")
    except Exception as e:
        print(f"Ping {host} 出现错误:{str(e)}")

def main():
    host = input("请输入目标主机地址:")
    ping(host)

if __name__ == "__main__":
    main()

Batch ping example:

import ping3

def ping(hosts):
    for host in hosts:
        try:
            latency = ping3.ping(host)
            if latency is not None:
                print(f"Ping {host} 成功,延迟:{latency}秒")
            else:
                print(f"Ping {host} 失败")
        except Exception as e:
            print(f"Ping {host} 出现错误:{str(e)}")

def main():
    hosts = ['google.com', 'facebook.com', 'youtube.com']
    ping(hosts)

if __name__ == "__main__":
    main()

Timeout and retry count example:

import ping3

def ping(host, timeout=1, count=4):
    try:
        latency = ping3.ping(host, timeout=timeout, count=count)
        if latency is not None:
            print(f"Ping {host} 成功,延迟:{latency}秒")
        else:
            print(f"Ping {host} 失败")
    except Exception as e:
        print(f"Ping {host} 出现错误:{str(e)}")

def main():
    host = 'google.com'
    ping(host, timeout=2, count=3)

if __name__ == "__main__":
    main()

File output example:

import ping3

def ping(hosts, output_file):
    with open(output_file, 'w') as file:
        for host in hosts:
            try:
                latency = ping3.ping(host)
                if latency is not None:
                    result = f"Ping {host} 成功,延迟:{latency}秒"
                else:
                    result = f"Ping {host} 失败"
            except Exception as e:
                result = f"Ping {host} 出现错误:{str(e)}"
            print(result)
            file.write(result + '\n')

def main():
    hosts = ['google.com', 'facebook.com', 'youtube.com']
    output_file = 'ping_results.txt'
    ping(hosts, output_file)

if __name__ == "__main__":
    main()

Packet‑loss calculation example:

import ping3

def ping(host):
    try:
        packet_loss, latency = ping3.ping(host, timeout=2, count=10, size=56)
        if packet_loss is not None and latency is not None:
            print(f"Ping {host} 成功,丢包率:{packet_loss * 100}% ,平均延迟:{latency}秒")
        else:
            print(f"Ping {host} 失败")
    except Exception as e:
        print(f"Ping {host} 出现错误:{str(e)}")

def main():
    host = 'google.com'
    ping(host)

if __name__ == "__main__":
    main()

These examples illustrate practical usage of ping3 for network connectivity checks and monitoring.

Pythonoperationspingscriptingnetwork monitoringping3
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.