Question: Write a python script that collects ping latency. Script Command: python pinger.py.... having trouble with datetime Command Description: To launch the script file (pinger.py), we
Write a python script that collects ping latency. Script Command: python pinger.py.... having trouble with datetime
Command Description: To launch the script file (pinger.py), we give the script the 2 arguments, the address and the number of pings to collect: address: A string of the address to ping. pings_to_collect: An integer of how many pings we want the script to collect data for. Each ping is separated by one second. Ex. Input: To ping google 120 times: python pinger.py google.com 120 Script Output: Format: , Clock_Time: The system's clock time. The format should be hour:minute:second Ping_in_ms: The result of the ping, only in milliseconds (as an integer). If the ping fails, report the value as -1. Ex. Output: 13:01:40,54 13:01:41,60 13:01:42,-1 13:01:43,61 Notes: The ping call will fail sometimes. Do not assume a ping response will returned within a second. So in your ping loop, wait until a response is returned (either with a ping or timeout) before waiting a second to start the next loop iteration.
import os import sys import datetime <--- is this correct? try: host = sys.argv[1] number_of_times = int(sys.argv[2]) # Taking the args from command line for i in range(number_of_times): # Iterating the given number of times cmd = 'timeout 1 ping -c 1 ' + host # forming command data = os.popen(cmd).read() # reading data return by popen # Displaying results if 'time=' in data and 'ms' in data: ms = data.split('time=')[1].split(' ms')[0] print datetime.datetime.now().strftime("%H:%M:%S"), ms <--- error else: print datetime.datetime.now().strftime("%H:%M:%S"), -1 except Exception as e: # printing exception print "Please pass correct number of arguments", e Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
