Skip to content

Commit 6d98d5a

Browse files
committed
fix: mitigate cron_job issues
1 parent 56e1555 commit 6d98d5a

File tree

2 files changed

+51
-25
lines changed

2 files changed

+51
-25
lines changed

cron_job/header.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
def clear():
77
# check and make call for specific operating system
8-
_ = call('clear' if os.name == 'posix' else 'cls')
8+
# !WARN: Below line of code is deprecated due to incompatibility with Windows
9+
# _ = call('clear' if os.name == 'posix' else 'cls')
10+
os.system('cls' if os.name == 'nt' else 'clear')
911

1012

1113
def printHeader():

cron_job/python2port.py

+48-24
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import csv
77
import io
88
import os
9+
import argparse
910
import subprocess
1011
import sys
1112
from subprocess import Popen, PIPE
@@ -22,13 +23,21 @@
2223
tailingFilename = str(uuid.uuid4())
2324

2425
folder_name = 'cron_output'
25-
dir_path = os.path.abspath(os.getcwd())+"/OPT/pingTester/Test/Temp/cron_job"
26+
# dir_path = os.path.abspath(os.getcwd())+"/OPT/pingTester/Test/Temp/cron_job"
27+
28+
# Get executing python file path
29+
dir_path = os.path.dirname(os.path.realpath(__file__))
30+
2631
print("DIR: ", dir_path)
2732

2833
# Create the folder, skip if exists
2934
if not os.path.exists(folder_name):
3035
os.makedirs(folder_name)
3136

37+
pingcount = 4
38+
telnetretry = 4
39+
filename = ""
40+
3241

3342
def isOpen(ip, port):
3443
s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
@@ -43,12 +52,8 @@ def isOpen(ip, port):
4352
s.close()
4453

4554

46-
pingcount = 3
47-
telnetretry = 3
48-
49-
5055
def pingStatistics(ip):
51-
ip=str(ip)
56+
ip = str(ip)
5257
print(ip)
5358
print " > GETTING STATISTICS FOR [ ", ip, " ]"
5459

@@ -81,7 +86,7 @@ def pingStatistics(ip):
8186
def pingSuccess(ip):
8287
hostname = ip
8388
# -i for duration, -c for packet count
84-
response = os.system("ping6 -W 1 -c " +str( pingcount)+" " + str(hostname))
89+
response = os.system("ping6 -W 1 -c " + str(pingcount)+" " + str(hostname))
8590
if response == 0:
8691
return 0
8792
else:
@@ -126,20 +131,23 @@ def checkHost(ip, port):
126131
if ping == True:
127132
# Collect ping statistics only when the host is up
128133
lst.append(pingStatistics(ip))
129-
else: lst.append(['--', '--', '-', '--', '100%'])
134+
else:
135+
lst.append(['--', '--', '-', '--', '100%'])
130136
""" lst.append(ping)
131137
lst.append(ipup) """
132138
return lst
133139

140+
134141
def read_cmd_args():
135-
filename=sys.argv[0] # This will have the filename being executed
136-
csv_input=sys.argv[1] # This shall contain the csv data input filename.
142+
filename = sys.argv[0] # This will have the filename being executed
143+
csv_input = sys.argv[1] # This shall contain the csv data input filename.
137144
print "Reading data from file: "+csv_input
138145
return csv_input
139146

140147

141148
filename = read_cmd_args()
142149

150+
143151
def readFromCSV(filename):
144152
with io.open(filename+'.csv', newline='') as f:
145153
reader = csv.reader(f)
@@ -154,14 +162,14 @@ def preprocess(s):
154162

155163

156164
def getFileData():
157-
with io.open(os.path.join(dir_path,folder_name, "Results_"+tailingFilename+".txt"), 'r', newline='') as flhndl:
165+
with io.open(os.path.join(dir_path, folder_name, "Results_"+tailingFilename+".txt"), 'r', newline='') as flhndl:
158166
return flhndl.readlines()
159167

160168

161169
def extractToCSV(listData):
162170
header = ['HOST IP', 'PING STATUS', 'TELNET STATUS',
163171
'MIN', 'MAX', 'AVG', 'LATENCY', 'LOSS %']
164-
with io.open(os.path.join(dir_path,folder_name, "Output_ResultsCSV_"+filename.split("/")[-1]+"_"+tailingFilename+".csv"), 'wb') as myfile:
172+
with io.open(os.path.join(dir_path, folder_name, "Output_ResultsCSV_"+filename.split("/")[-1]+"_"+tailingFilename+".csv"), 'wb') as myfile:
165173
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
166174
wr.writerow(header)
167175
for lines in listData:
@@ -171,24 +179,41 @@ def extractToCSV(listData):
171179

172180
wr.writerow(first)
173181

174-
def read_cmd_args():
175-
filename=sys.argv[0] # This will have the filename being executed
176-
csv_input=sys.argv[1] # This shall contain the csv data input filename.
177-
print "Reading data from file: "+csv_input
178-
return csv_input
179-
filename = read_cmd_args()
180182

181-
182-
print(filename)
183+
def read_cmd_args():
184+
# Read command line arguments with python2
185+
parser = argparse.ArgumentParser()
186+
parser.add_argument("-f", "--file", help="File name to read from")
187+
parser.add_argument("-pc", "--packet_counts", help="Number of packets")
188+
parser.add_argument("-tr", "--telnet_retries", help="Telnet retries")
189+
190+
args = parser.parse_args()
191+
return args
192+
193+
194+
args = read_cmd_args()
195+
# Parse the cmd args and store them in the variables
196+
pingcount = args.packet_counts
197+
telnetretry = args.telnet_retries
198+
filename = args.file
199+
200+
if not args.packet_counts:
201+
pingcount = raw_input("ENTER PACKET COUNTS: ")
202+
if not args.telnet_retries:
203+
telnetretry = int(raw_input("ENTER TELNET RETRIES: "))
204+
if not args.file:
205+
filename = raw_input(
206+
"ENTER THE FILE NAME WITHOUT THE EXTENSION (DEFAULT FORMAT CSV): ")
207+
print(filename)
183208
readFromCSV(filename)
184-
with io.open(os.path.join(dir_path,folder_name, "Results_"+tailingFilename+".txt"), 'w', newline='') as file:
209+
with io.open(os.path.join(dir_path, folder_name, "Results_"+tailingFilename+".txt"), 'w', newline='') as file:
185210
for ips in data:
186211
for index, ips_get in enumerate(ips):
187212
print "[ ```````````````````````````````````````````` ]"
188213
print("[ RUN {} ]".format(index+1))
189214
get_lst = list()
190215
get_lst = checkHost(ips_get[0], port)
191-
print("FLAG: ",get_lst)
216+
print("FLAG: ", get_lst)
192217
file.write(
193218
unicode(ips_get[0]+"\t" +
194219
str(get_lst[0])+"\t" +
@@ -202,7 +227,6 @@ def read_cmd_args():
202227
print "[ ```````````````````````````````````````````` ]\n\n"
203228

204229

205-
206230
printHeader()
207231
data = getFileData()
208-
extractToCSV(data)
232+
extractToCSV(data)

0 commit comments

Comments
 (0)