6
6
import csv
7
7
import io
8
8
import os
9
+ import argparse
9
10
import subprocess
10
11
from subprocess import Popen , PIPE
11
12
from header import printHeader
25
26
if not os .path .exists (folder_name ):
26
27
os .makedirs (folder_name )
27
28
29
+ pingcount = 4
30
+ telnetretry = 4
31
+ filename = ""
32
+
33
+
34
+ def read_cmd_args ():
35
+ # Read command line arguments with python2
36
+ parser = argparse .ArgumentParser ()
37
+ parser .add_argument ("-f" , "--file" , help = "File name to read from" )
38
+ parser .add_argument ("-pc" , "--packet_counts" , help = "Number of packets" )
39
+ parser .add_argument ("-tr" , "--telnet_retries" , help = "Telnet retries" )
40
+
41
+ args = parser .parse_args ()
42
+ return args
43
+
28
44
29
45
def isOpen (ip , port ):
30
46
s = socket .socket (socket .AF_INET6 , socket .SOCK_STREAM )
@@ -33,21 +49,17 @@ def isOpen(ip, port):
33
49
s .connect ((ip , int (port ), 0 , 0 ))
34
50
s .shutdown (socket .SHUT_RDWR )
35
51
return True
36
- except :
52
+ except BaseException :
37
53
return False
38
54
finally :
39
55
s .close ()
40
56
41
57
42
- pingcount = raw_input ("ENTER PACKET COUNTS: " )
43
- telnetretry = int (raw_input ("ENTER TELNET RETRIES: " ))
44
-
45
-
46
58
def pingStatistics (ip ):
47
59
print " > GETTING STATISTICS FOR [ " , ip , " ]"
48
60
49
61
try :
50
- command = "ping6 -W 1 -c " + pingcount + " " + ip
62
+ command = "ping6 -W 1 -c " + pingcount + " " + ip
51
63
process = Popen (command , stdout = PIPE , stderr = None , shell = True )
52
64
output = process .communicate ()[0 ]
53
65
@@ -68,14 +80,14 @@ def pingStatistics(ip):
68
80
else :
69
81
return stats
70
82
71
- except :
83
+ except BaseException :
72
84
print (' > STATISTCS_FAILURE' )
73
85
74
86
75
87
def pingSuccess (ip ):
76
88
hostname = ip
77
89
# -i for duration, -c for packet count
78
- response = os .system ("ping6 -W 1 -c " + pingcount + " " + hostname )
90
+ response = os .system ("ping6 -W 1 -c " + pingcount + " " + hostname )
79
91
if response == 0 :
80
92
return 0
81
93
else :
@@ -91,11 +103,11 @@ def checkHost(ip, port):
91
103
for i in range (retry ):
92
104
print ('=> ping success' )
93
105
94
- for x in range (1 , telnetretry + 1 ):
106
+ for x in range (1 , telnetretry + 1 ):
95
107
telnetStatus = isOpen (ip , port )
96
108
if x != 1 :
97
109
print "[ ! WARN ! Retrying telnet (" , x , ")... ]"
98
- if telnetStatus == True :
110
+ if telnetStatus :
99
111
ipup = True
100
112
break
101
113
else :
@@ -108,26 +120,27 @@ def checkHost(ip, port):
108
120
else :
109
121
ping = ipup = False
110
122
111
- if ping == True :
123
+ if ping :
112
124
lst .append ("PING SUCCESS" )
113
125
else :
114
126
lst .append ("PING FAIL" )
115
- if ipup == True :
127
+ if ipup :
116
128
lst .append ("PORT OPEN" )
117
129
else :
118
130
lst .append ("PORT CLOSED" )
119
131
120
- if ping == True :
132
+ if ping :
121
133
# Collect ping statistics only when the host is up
122
134
lst .append (pingStatistics (ip ))
123
- else : lst .append (['--' , '--' , '-' , '--' , '100%' ])
135
+ else :
136
+ lst .append (['--' , '--' , '-' , '--' , '100%' ])
124
137
""" lst.append(ping)
125
138
lst.append(ipup) """
126
139
return lst
127
140
128
141
129
142
def readFromCSV (filename ):
130
- with io .open (filename + '.csv' , newline = '' ) as f :
143
+ with io .open (filename + '.csv' , newline = '' ) as f :
131
144
reader = csv .reader (f )
132
145
data .append (list (reader ))
133
146
f .close ()
@@ -140,14 +153,14 @@ def preprocess(s):
140
153
141
154
142
155
def getFileData ():
143
- with io .open (os .path .join (folder_name , "Results_" + tailingFilename + ".txt" ), 'r' , newline = '' ) as flhndl :
156
+ with io .open (os .path .join (folder_name , "Results_" + tailingFilename + ".txt" ), 'r' , newline = '' ) as flhndl :
144
157
return flhndl .readlines ()
145
158
146
159
147
160
def extractToCSV (listData ):
148
161
header = ['HOST IP' , 'PING STATUS' , 'TELNET STATUS' ,
149
162
'MIN' , 'MAX' , 'AVG' , 'LATENCY' , 'LOSS %' ]
150
- with io .open (os .path .join (folder_name , "Output_ResultsCSV_" + tailingFilename + ".csv" ), 'wb' ) as myfile :
163
+ with io .open (os .path .join (folder_name , "Output_ResultsCSV_" + tailingFilename + ".csv" ), 'wb' ) as myfile :
151
164
wr = csv .writer (myfile , quoting = csv .QUOTE_ALL )
152
165
wr .writerow (header )
153
166
for lines in listData :
@@ -158,31 +171,42 @@ def extractToCSV(listData):
158
171
wr .writerow (first )
159
172
160
173
161
- filename = raw_input (
162
- "ENTER THE FILE NAME WITHOUT THE EXTENSION (DEFAULT FORMAT CSV): " )
163
- print (filename )
174
+ args = read_cmd_args ()
175
+ # Parse the cmd args and store them in the variables
176
+ pingcount = args .packet_counts
177
+ telnetretry = args .telnet_retries
178
+ filename = args .file
179
+
180
+ if not args .packet_counts :
181
+ pingcount = raw_input ("ENTER PACKET COUNTS: " )
182
+ if not args .telnet_retries :
183
+ telnetretry = int (raw_input ("ENTER TELNET RETRIES: " ))
184
+ if not args .file :
185
+ filename = raw_input (
186
+ "ENTER THE FILE NAME WITHOUT THE EXTENSION (DEFAULT FORMAT CSV): " )
187
+ print (filename )
188
+
164
189
readFromCSV (filename )
165
- with io .open (os .path .join (folder_name , "Results_" + tailingFilename + ".txt" ), 'w' , newline = '' ) as file :
190
+ with io .open (os .path .join (folder_name , "Results_" + tailingFilename + ".txt" ), 'w' , newline = '' ) as file :
166
191
for ips in data :
167
192
for index , ips_get in enumerate (ips ):
168
193
print "[ ```````````````````````````````````````````` ]"
169
- print ("[ RUN {} ]" .format (index + 1 ))
194
+ print ("[ RUN {} ]" .format (index + 1 ))
170
195
get_lst = list ()
171
196
get_lst = checkHost (ips_get [0 ], port )
172
197
file .write (
173
- unicode (ips_get [0 ]+ "\t " +
174
- str (get_lst [0 ])+ "\t " +
175
- str (get_lst [1 ])+ "\t " +
176
- str (get_lst [2 ][0 ])+ "\t " +
177
- str (get_lst [2 ][1 ])+ "\t " +
178
- str (get_lst [2 ][2 ])+ "\t " +
179
- str (get_lst [2 ][3 ])+ "\t " +
180
- str (get_lst [2 ][4 ].strip ())+ "\n " ))
198
+ unicode (ips_get [0 ] + "\t " +
199
+ str (get_lst [0 ]) + "\t " +
200
+ str (get_lst [1 ]) + "\t " +
201
+ str (get_lst [2 ][0 ]) + "\t " +
202
+ str (get_lst [2 ][1 ]) + "\t " +
203
+ str (get_lst [2 ][2 ]) + "\t " +
204
+ str (get_lst [2 ][3 ]) + "\t " +
205
+ str (get_lst [2 ][4 ].strip ()) + "\n " ))
181
206
182
207
print "[ ```````````````````````````````````````````` ]\n \n "
183
208
184
209
185
210
printHeader ()
186
211
data = getFileData ()
187
212
extractToCSV (data )
188
-
0 commit comments