Python Script to Ping IPs from CSV and Export Results to Text File #NetworkAutomation

preview_player
Показать описание
Learn how to create a Python script that reads a list of IP addresses from a CSV file, pings each IP to check if it is up, and then exports the results to a text file. This video is perfect for network engineers looking to enhance their automation skills using Python.

To learn more, NetworkJourney has introduced a 3-month hands-on training course for network engineers! Enroll now to boost your network automation and scripting skills.

𝐏𝐲𝐭𝐡𝐨𝐧 𝐒𝐜𝐫𝐢𝐩𝐭 𝐭𝐨 𝐏𝐢𝐧𝐠 𝐈𝐏𝐬 𝐟𝐫𝐨𝐦 𝐂𝐒𝐕 𝐚𝐧𝐝 𝐄𝐱𝐩𝐨𝐫𝐭 𝐑𝐞𝐬𝐮𝐥𝐭𝐬 𝐭𝐨 𝐓𝐞𝐱𝐭 𝐅𝐢𝐥𝐞 #𝐍𝐞𝐭𝐰𝐨𝐫𝐤𝐀𝐮𝐭𝐨𝐦𝐚𝐭𝐢𝐨𝐧

ip

172.16.24.74

172.16.24.60

8.8.8.8

1.1.1.1

8.8.8.1

Step 2# (replace system path as per your need)

import csv

import subprocess

import os

def ping_ip(ip):

# Ping command based on the OS

try:

# Run the ping command

return True

except subprocess.CalledProcessError:

return False

def ping_from_csv(input_csv, output_txt):

# Read the IPs from the CSV file

with open(input_csv, mode='r') as file:

reader = csv.DictReader(file)

ips = [row['ip'] for row in reader]

# Ping each IP and collect the results

results = []

for ip in ips:

status = "Up" if ping_ip(ip) else "Down"

# Write the results to the output text file

with open(output_txt, mode='w') as file:

for line in results:

print(f"Results have been saved to {output_txt}")

# Set the input CSV and output text file paths

# Run the function

ping_from_csv(input_csv, output_txt)

#PythonScript, #PingIPs, #CSVFile, #NetworkAutomation, #PythonNetworking, #NetworkJourney, #HandsOnTraining, #NetworkEngineers, #LearnPython, #AutomationSkills

#PythonScript, #PingIPs, #CSVFile, #NetworkAutomation, #PythonNetworking, #NetworkJourney, #HandsOnTraining, #NetworkEngineers, #LearnPython, #AutomationSkills
Рекомендации по теме
Комментарии
Автор

𝐏𝐲𝐭𝐡𝐨𝐧 𝐒𝐜𝐫𝐢𝐩𝐭 𝐭𝐨 𝐏𝐢𝐧𝐠 𝐈𝐏𝐬 𝐟𝐫𝐨𝐦 𝐂𝐒𝐕 𝐚𝐧𝐝 𝐄𝐱𝐩𝐨𝐫𝐭 𝐑𝐞𝐬𝐮𝐥𝐭𝐬 𝐭𝐨 𝐓𝐞𝐱𝐭 𝐅𝐢𝐥𝐞 #𝐍𝐞𝐭𝐰𝐨𝐫𝐤𝐀𝐮𝐭𝐨𝐦𝐚𝐭𝐢𝐨𝐧

Learn how to create a Python script that reads a list of IP addresses from a CSV file, pings each IP to check if it is up, and then exports the results to a text file. This video is perfect for network engineers looking to enhance their automation skills using Python.

To learn more, NetworkJourney has introduced a 3-month hands-on training course for network engineers! Enroll now to boost your network automation and scripting skills.

Step 1# Create ips.csv (replace with your Device IP's)

ip

172.16.24.74

172.16.24.60

8.8.8.8

1.1.1.1

8.8.8.1



Step 2# (replace system path as per your need)

import csv

import subprocess

import os

def ping_ip(ip):

# Ping command based on the OS

command = ['ping', '-c', '5', ip] if os.name != 'nt' else ['ping', '-n', '5', ip]

try:

# Run the ping command

subprocess.run(command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True)

return True

except

return False

def ping_from_csv(input_csv, output_txt):

# Read the IPs from the CSV file

with open(input_csv, mode='r') as file:

reader = csv.DictReader(file)

ips = [row['ip'] for row in reader]

# Ping each IP and collect the results

results = []

for ip in ips:

status = "Up" if ping_ip(ip) else "Down"

results.append(f"{ip} is {status}")

# Write the results to the output text file

with open(output_txt, mode='w') as file:

for line in results:

file.write(line + '\n')

print(f"Results have been saved to {output_txt}")

# Set the input CSV and output text file paths

input_csv =

output_txt =

# Run the function

ping_from_csv(input_csv, output_txt)



#PythonScript, #PingIPs, #CSVFile, #NetworkAutomation, #PythonNetworking, #NetworkJourney, #HandsOnTraining, #NetworkEngineers, #LearnPython, #AutomationSkills

NetworkJourney