6
6
import csv
7
7
import io
8
8
import os
9
+ import argparse
9
10
import subprocess
10
11
import sys
11
12
from subprocess import Popen , PIPE
22
23
tailingFilename = str (uuid .uuid4 ())
23
24
24
25
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
+
26
31
print ("DIR: " , dir_path )
27
32
28
33
# Create the folder, skip if exists
29
34
if not os .path .exists (folder_name ):
30
35
os .makedirs (folder_name )
31
36
37
+ pingcount = 4
38
+ telnetretry = 4
39
+ filename = ""
40
+
32
41
33
42
def isOpen (ip , port ):
34
43
s = socket .socket (socket .AF_INET6 , socket .SOCK_STREAM )
@@ -43,12 +52,8 @@ def isOpen(ip, port):
43
52
s .close ()
44
53
45
54
46
- pingcount = 3
47
- telnetretry = 3
48
-
49
-
50
55
def pingStatistics (ip ):
51
- ip = str (ip )
56
+ ip = str (ip )
52
57
print (ip )
53
58
print " > GETTING STATISTICS FOR [ " , ip , " ]"
54
59
@@ -81,7 +86,7 @@ def pingStatistics(ip):
81
86
def pingSuccess (ip ):
82
87
hostname = ip
83
88
# -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 ))
85
90
if response == 0 :
86
91
return 0
87
92
else :
@@ -126,20 +131,23 @@ def checkHost(ip, port):
126
131
if ping == True :
127
132
# Collect ping statistics only when the host is up
128
133
lst .append (pingStatistics (ip ))
129
- else : lst .append (['--' , '--' , '-' , '--' , '100%' ])
134
+ else :
135
+ lst .append (['--' , '--' , '-' , '--' , '100%' ])
130
136
""" lst.append(ping)
131
137
lst.append(ipup) """
132
138
return lst
133
139
140
+
134
141
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.
137
144
print "Reading data from file: " + csv_input
138
145
return csv_input
139
146
140
147
141
148
filename = read_cmd_args ()
142
149
150
+
143
151
def readFromCSV (filename ):
144
152
with io .open (filename + '.csv' , newline = '' ) as f :
145
153
reader = csv .reader (f )
@@ -154,14 +162,14 @@ def preprocess(s):
154
162
155
163
156
164
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 :
158
166
return flhndl .readlines ()
159
167
160
168
161
169
def extractToCSV (listData ):
162
170
header = ['HOST IP' , 'PING STATUS' , 'TELNET STATUS' ,
163
171
'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 :
165
173
wr = csv .writer (myfile , quoting = csv .QUOTE_ALL )
166
174
wr .writerow (header )
167
175
for lines in listData :
@@ -171,24 +179,41 @@ def extractToCSV(listData):
171
179
172
180
wr .writerow (first )
173
181
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 ()
180
182
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 )
183
208
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 :
185
210
for ips in data :
186
211
for index , ips_get in enumerate (ips ):
187
212
print "[ ```````````````````````````````````````````` ]"
188
213
print ("[ RUN {} ]" .format (index + 1 ))
189
214
get_lst = list ()
190
215
get_lst = checkHost (ips_get [0 ], port )
191
- print ("FLAG: " ,get_lst )
216
+ print ("FLAG: " , get_lst )
192
217
file .write (
193
218
unicode (ips_get [0 ]+ "\t " +
194
219
str (get_lst [0 ])+ "\t " +
@@ -202,7 +227,6 @@ def read_cmd_args():
202
227
print "[ ```````````````````````````````````````````` ]\n \n "
203
228
204
229
205
-
206
230
printHeader ()
207
231
data = getFileData ()
208
- extractToCSV (data )
232
+ extractToCSV (data )
0 commit comments