To perform a multi-ping operation on network devices and provide a list of details for devices that are down and devices that are up. Here’s an example program that you can use:
import subprocess
from colorama import Fore, Style
def multi_ping(devices):
results = []
for device in devices:
result = {}
result[“device”] = device
try:
# Execute the ping command
output = subprocess.check_output([‘ping’, ‘-c’, ‘1’, device], universal_newlines=True)
if “1 packets transmitted, 1 received” in output:
result[“status”] = Fore.GREEN + “UP” + Style.RESET_ALL
else:
result[“status”] = Fore.RED + “DOWN” + Style.RESET_ALL
except subprocess.CalledProcessError:
result[“status”] = Fore.RED + “DOWN” + Style.RESET_ALL
results.append(result)
return results
# Example usage
devices = [“192.168.1.1”, “192.168.1.2”, “192.168.1.3”, “192.168.1.4”]
ping_results = multi_ping(devices)
print(“Ping Results:”)
for result in ping_results:
print(f”Device: {result[‘device’]}, Status: {result[‘status’]}”)
In this updated version, we have imported the Fore
and Style
modules from the colorama
library. The Fore
module provides color constants for foreground (text) colors, and the Style
module allows resetting the color settings.
Within the multi_ping
function, when a device is determined to be “UP,” we prepend the string with Fore.GREEN
to set the text color to green. Similarly, when a device is determined to be “DOWN,” we prepend the string with Fore.RED
to set the text color to red. Additionally, we append Style.RESET_ALL
to reset the color settings and ensure subsequent text is displayed normally.
When running the program, the “UP” devices will be displayed in green, and the “DOWN” devices will be displayed in red.
Make sure you have the colorama
library installed. If not, you can install it by running pip install colorama
in your command prompt or terminal.