Skip to content

Commit b2530c2

Browse files
committed
chore: add cmd line arguments to skip inputs while program run
1 parent 7722c8d commit b2530c2

File tree

3 files changed

+170
-32
lines changed

3 files changed

+170
-32
lines changed

.gitignore

+112
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,115 @@
77
/.settings
88
*.nja
99
output/
10+
conda/
11+
# Editor temporary/working/backup files #
12+
#########################################
13+
.#*
14+
[#]*#
15+
*~
16+
*$
17+
*.bak
18+
*.diff
19+
*.org
20+
.project
21+
*.rej
22+
.settings/
23+
.*.sw[nop]
24+
.sw[nop]
25+
*.tmp
26+
*.coverage.*
27+
.idea/
28+
29+
# Compiled source #
30+
###################
31+
*.a
32+
*.com
33+
*.class
34+
*.dll
35+
*.exe
36+
*.o
37+
*.py[ocd]
38+
*.so
39+
40+
# Packages #
41+
############
42+
# it's better to unpack these files and commit the raw source
43+
# git has its own built in compression methods
44+
*.7z
45+
*.bz2
46+
*.bzip2
47+
*.dmg
48+
*.gz
49+
*.iso
50+
*.jar
51+
*.rar
52+
*.tar
53+
*.tbz2
54+
*.tgz
55+
*.zip
56+
57+
# Python files #
58+
################
59+
# setup.py working directory
60+
/build
61+
# sphinx build directory
62+
/_build
63+
# setup.py dist directory
64+
/dist
65+
/doc/build
66+
/doc/cdoc/build
67+
/MANIFEST
68+
# Egg metadata
69+
*.egg-info
70+
/.eggs
71+
# The shelf plugin uses this dir
72+
./.shelf
73+
# coverage reports
74+
.coverage
75+
# ipython files
76+
**/.ipynb_checkpoints
77+
__pycache__
78+
79+
# Logs and databases #
80+
######################
81+
*.log
82+
*.sqlite
83+
84+
# Patches #
85+
###########
86+
*.patch
87+
*.diff
88+
89+
# OS generated files #
90+
######################
91+
.DS_Store*
92+
.VolumeIcon.icns
93+
.fseventsd
94+
Icon?
95+
.gdb_history
96+
ehthumbs.db
97+
Thumbs.db
98+
99+
# pytest-cov #
100+
##############
101+
/.cache
102+
*,cover
103+
htmlcov
104+
105+
# Things specific to this project #
106+
###################################
107+
/.anaconda
108+
/.envs
109+
/record.txt
110+
/docs/build
111+
/examples/movies/movies.db
112+
*.orig
113+
CHANGELOG.temp
114+
115+
examples/*/envs
116+
117+
## testing outputs
118+
cov.xml
119+
coverage.xml
120+
junit.xml
121+
/fake_project

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():

python2port.py

+55-31
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
from subprocess import Popen, PIPE
1112
from header import printHeader
@@ -25,6 +26,21 @@
2526
if not os.path.exists(folder_name):
2627
os.makedirs(folder_name)
2728

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+
2844

2945
def isOpen(ip, port):
3046
s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
@@ -33,21 +49,17 @@ def isOpen(ip, port):
3349
s.connect((ip, int(port), 0, 0))
3450
s.shutdown(socket.SHUT_RDWR)
3551
return True
36-
except:
52+
except BaseException:
3753
return False
3854
finally:
3955
s.close()
4056

4157

42-
pingcount = raw_input("ENTER PACKET COUNTS: ")
43-
telnetretry = int(raw_input("ENTER TELNET RETRIES: "))
44-
45-
4658
def pingStatistics(ip):
4759
print " > GETTING STATISTICS FOR [ ", ip, " ]"
4860

4961
try:
50-
command = "ping6 -W 1 -c "+pingcount+" "+ip
62+
command = "ping6 -W 1 -c " + pingcount + " " + ip
5163
process = Popen(command, stdout=PIPE, stderr=None, shell=True)
5264
output = process.communicate()[0]
5365

@@ -68,14 +80,14 @@ def pingStatistics(ip):
6880
else:
6981
return stats
7082

71-
except:
83+
except BaseException:
7284
print(' > STATISTCS_FAILURE')
7385

7486

7587
def pingSuccess(ip):
7688
hostname = ip
7789
# -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)
7991
if response == 0:
8092
return 0
8193
else:
@@ -91,11 +103,11 @@ def checkHost(ip, port):
91103
for i in range(retry):
92104
print('=> ping success')
93105

94-
for x in range(1, telnetretry+1):
106+
for x in range(1, telnetretry + 1):
95107
telnetStatus = isOpen(ip, port)
96108
if x != 1:
97109
print "[ ! WARN ! Retrying telnet (", x, ")... ]"
98-
if telnetStatus == True:
110+
if telnetStatus:
99111
ipup = True
100112
break
101113
else:
@@ -108,26 +120,27 @@ def checkHost(ip, port):
108120
else:
109121
ping = ipup = False
110122

111-
if ping == True:
123+
if ping:
112124
lst.append("PING SUCCESS")
113125
else:
114126
lst.append("PING FAIL")
115-
if ipup == True:
127+
if ipup:
116128
lst.append("PORT OPEN")
117129
else:
118130
lst.append("PORT CLOSED")
119131

120-
if ping == True:
132+
if ping:
121133
# Collect ping statistics only when the host is up
122134
lst.append(pingStatistics(ip))
123-
else: lst.append(['--', '--', '-', '--', '100%'])
135+
else:
136+
lst.append(['--', '--', '-', '--', '100%'])
124137
""" lst.append(ping)
125138
lst.append(ipup) """
126139
return lst
127140

128141

129142
def readFromCSV(filename):
130-
with io.open(filename+'.csv', newline='') as f:
143+
with io.open(filename + '.csv', newline='') as f:
131144
reader = csv.reader(f)
132145
data.append(list(reader))
133146
f.close()
@@ -140,14 +153,14 @@ def preprocess(s):
140153

141154

142155
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:
144157
return flhndl.readlines()
145158

146159

147160
def extractToCSV(listData):
148161
header = ['HOST IP', 'PING STATUS', 'TELNET STATUS',
149162
'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:
151164
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
152165
wr.writerow(header)
153166
for lines in listData:
@@ -158,31 +171,42 @@ def extractToCSV(listData):
158171
wr.writerow(first)
159172

160173

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+
164189
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:
166191
for ips in data:
167192
for index, ips_get in enumerate(ips):
168193
print "[ ```````````````````````````````````````````` ]"
169-
print("[ RUN {} ]".format(index+1))
194+
print("[ RUN {} ]".format(index + 1))
170195
get_lst = list()
171196
get_lst = checkHost(ips_get[0], port)
172197
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"))
181206

182207
print "[ ```````````````````````````````````````````` ]\n\n"
183208

184209

185210
printHeader()
186211
data = getFileData()
187212
extractToCSV(data)
188-

0 commit comments

Comments
 (0)