All,
I have this very basic script that's producing some odd output. What I need to do is constantly ping a server, and when/if that server stops responding, I want to write the timestamp and " timed out" to a file. We're trying to lock down a time as to when something is going down. The code is below, and what's happening is that while the server (in this case a host that I'm able to block icmp packets for on a router) is responding, the return code is 0 and it's fine, but once I put an acl on the router, the pings stop responding and now the script alternates between 0 and 1. Below is the code and I'll paste the result below it:
import os, datetime, sys, string
from subprocess import call, Popen
import subprocess
def Help():
print "Must supply a single IP address to ping and filename"
print "\n"
print "This application can be run from multiple"
print "command windows"
if len(sys.argv) < 3 or len(sys.argv) > 3:
Help()
address = sys.argv[1]
filename = sys.argv[2]
while True:
ping = Popen( ['ping','-n','1',address.strip() ] , stdout=subprocess.PIPE, shell=True)
pingResponse = ping.wait()
print "Current response code is " + str(pingResponse)
if pingResponse == 0:
print pingResponse
output = open(filename, 'a')
output.write("["+datetime.datetime.now().strftime("%H:%M:%S.%f")+"]" + " Success\n")
output.close()
if pingResponse == 1:
print pingResponse
output = open(filename, 'a')
output.write("["+datetime.datetime.now().strftime("%H:%M:%S.%f")+"]" + " Timed out\n")
output.close()
The result:
12:48:27.772000] Success
[12:48:28.033000] Success
[12:48:28.300000] Success
[12:48:28.556000] Success
[12:48:28.813000] Success
[12:48:32.714000] Timed out
[12:48:32.965000] Success
[12:48:36.715000] Timed out
[12:48:36.967000] Success
[12:48:40.711000] Timed out
[12:48:40.962000] Success
[12:48:44.716000] Timed out
[12:48:44.979000] Success
[12:48:49.213000] Timed out
[12:48:49.466000] Success
[12:48:53.212000] Timed out
[12:48:53.467000] Success
[12:48:57.216000] Timed out
[12:48:57.495000] Success
[12:49:01.722000] Timed out
[12:49:01.980000] Success
[12:49:06.212000] Timed out
[12:49:06.469000] Success
[12:49:10.214000] Timed out
[12:49:10.476000] Success
[12:49:14.713000] Timed out
The alternating success and time outs was when the device was truly down. I need to somehow clear out the return code, but I'm not sure how to do that.
Thanks!