Monday, April 21, 2025

Continous Ping with User Input - Python

import os

while True:
    os.system('cls')
    print('-' * 79)
    print('Custom Constant Pinger v0.1')

    target = input('Please, enter IP to ping: ')

    if target == 'quit':
        print('Script terminad')
        break
    else:
        os.system('ping ' + target)

This Python code allows a user to enter an IP address and then uses the command prompt to continuously ping that IP address until the user enters "quit" to end the program. Here's a step-by-step explanation of the code:

  1. The code imports the 'os' module, which provides a way of using operating system dependent functionality, such as executing command prompt commands.

  2. The code uses a while loop with the condition 'True' which means the loop will continue to run until a 'break' statement is encountered.

  3. Inside the while loop, the code first clears the command prompt screen using the 'os.system' method to execute the 'cls' command, which clears the console screen on Windows systems.

  4. Then, it prints a separator line of 79 characters and the name of the program 'Custom Constant Pinger v0.1'.

  5. The code prompts the user to enter an IP address to ping by using the 'input' function and storing the user input in the 'target' variable.

  6. The code then checks if the user entered the string 'quit'. If so, it prints 'Script terminated' and breaks out of the loop.

  7. If the user did not enter 'quit', the code uses the 'os.system' method to execute the 'ping' command followed by the IP address entered by the user. This will ping the target IP address continuously until the user manually stops the program.

Overall, this code provides a simple way to ping a specified IP address continuously in the command prompt. However, it is important to note that continuous pinging can potentially cause network congestion and other issues, so it should be used with caution.

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