Sunday, April 20, 2025

How to Ping IP Address - Python

import os

os.system('cls')

print("#" * 60)
ip_to_check = input('IP to Check: ')

print('-' * 60)
os.system('ping {}'.format(ip_to_check))

print('-' * 60)

input('Press any Key to Exit')

This Python code demonstrates how to use the os module to clear the console and execute a ping command to check if an IP address is reachable.

First, the os.system() function is used to run the "cls" command on Windows, which clears the console screen to make it easier to read the output of the program. The # character is printed 60 times to create a visual divider before the user is prompted to input an IP address to check.

Once an IP address is entered, another divider made up of the - character is printed to separate the input from the output. The os.system() function is then used again to execute the ping command followed by the IP address entered by the user using Python's string formatting syntax. This will send ICMP packets to the specified IP address and display the result of the ping command.

Finally, another divider is printed, and the program pauses using the input() function until the user presses any key to exit the program.

Here's an explanation of the script step by step:

  1. import os: This imports the "os" module in Python, which provides a way of using operating system dependent functionality like reading or writing to the file system, starting or stopping processes, etc.

  2. os.system('cls'): This line clears the console screen (in Windows) before running the script, by calling the "system" method of the "os" module with the argument 'cls'. 'cls' is the command to clear the screen in Windows, and this line will only work in a Windows environment.

  3. print("#" * 60): This line prints a separator of 60 "#" symbols to visually separate the output of the different parts of the script.

  4. ip_to_check = input('IP to Check: '): This line prompts the user to input an IP address to check for availability. The input value is assigned to the "ip_to_check" variable.

  5. print('-' * 60): This line prints another separator of 60 "-" symbols to visually separate the previous input prompt from the output of the ping command.

  6. os.system('ping {}'.format(ip_to_check)): This line runs the "ping" command with the IP address entered by the user as the argument. The "ping" command sends an Internet Control Message Protocol (ICMP) echo request to the specified IP address and waits for a response. The output of the command is displayed in the console.

  7. print('-' * 60): This line prints another separator of 60 "-" symbols to visually separate the output of the ping command from the exit prompt.

  8. input('Press any Key to Exit'): This line waits for the user to press any key before exiting the script.

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 .  ...