#!/usr/bin/env python import os import csv from datetime import datetime # Local path and filenames csv_path = "/home/foo/path/to/network_status/" csv_filename = "network_status_data.csv" log_path = "/home/foo/Logs/" log_filename = "network_status_updated" # Remove the log file if it exists if os.path.exists(log_path+log_filename): os.remove(log_path+log_filename) else: print("The file does not exist") # ping Google and note the response hostname = "google.com" response = os.system("ping -c 1 " + hostname) # Generate what to write to the .csv file dt = datetime.now() full_date = dt.strftime('%Y-%m-%d') full_time = dt.strftime('%H:%M:%S') # Write to the .csv file if response == 0: csvWrite = full_date, full_time, 'Network is up' csvFile = open(csv_path+csv_filename, 'a') with csvFile: writer = csv.writer(csvFile) writer.writerow(csvWrite) else: csvWrite = full_date, full_time, 'Network is down' csvFile = open(csv_path+csv_filename, 'a') with csvFile: writer = csv.writer(csvFile) writer.writerow(csvWrite) # Create a log file to show the latest status was captured f = open(log_path+log_filename, "x")