Monday, April 21, 2025

Crafting Custom Ping in Python

import os

ip_dest = input("Enter IP addr to Check: ")
icmp_num = input("ICMP Echo Number: ")

os.system("ping -n {} {}".format(icmp_num, ip_dest))

This Python code demonstrates how to use the os module to execute a ping command and check the response from a specific IP address. Below is a detailed explanation of how the code works:

  1. First, the code prompts the user to enter the IP address to check and the number of ICMP echo requests to send to the target host.
  2. The os.system() function is then used to execute a ping command to the specified IP address with the number of ICMP echo requests specified by the user.
  3. The ping command sends ICMP echo requests to the specified IP address and waits for a response. If the target host responds, it sends an ICMP echo reply message back to the sender.
  4. The response from the ping command is then printed to the console. If the target host responds to the ICMP echo requests, the output will show the number of packets sent, received, lost, and the response time.
  5. If the target host does not respond to the ICMP echo requests, the output will show that all packets were lost.

In summary, this code demonstrates a simple way to check if a specific IP address is reachable on the network using ICMP echo requests.

Ping multiple IPs from list:

import os

ip_list = ['127.0.0.1', '192.168.0.103']

icmp_num = input("ICMP Echo Number: ")

for x in ip_list:
    os.system("ping -n {} {}".format(icmp_num, x))

This Python code is a simple script that allows the user to enter a list of IP addresses and an ICMP echo number, and then pings each IP address in the list using the specified number of ICMP echoes. Here's a step-by-step explanation of what the code does:

  1. The code imports the "os" module, which provides a way to interact with the operating system.

  2. The code creates a list called "ip_list" containing two IP addresses - '127.0.0.1' and '192.168.0.103'.

  3. The code prompts the user to enter an ICMP echo number using the "input()" function and stores the result in the variable "icmp_num".

  4. The code uses a "for" loop to iterate over each IP address in the "ip_list".

  5. Inside the loop, the code calls the "os.system()" function to execute the "ping" command with the specified number of ICMP echoes and IP address using string formatting.

  6. The output of each "ping" command is printed to the console.

  7. The loop repeats for each IP address in the "ip_list".

In summary, this code allows the user to quickly ping multiple IP addresses with a specified number of ICMP echoes, which can be useful for testing network connectivity or troubleshooting network issues.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...