-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtestsocket-md.py
More file actions
executable file
·134 lines (120 loc) · 3.34 KB
/
testsocket-md.py
File metadata and controls
executable file
·134 lines (120 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/python
import socket
import sys
import urllib2
import argparse
import os.path
import ssl
class MyParser(argparse.ArgumentParser):
def error(self, message):
sys.stderr.write('Error: %s\n' % message)
self.print_help()
sys.exit(2)
parse = MyParser()
parse.add_argument('-i', '--inputfile', help='Input file in the format IPADDRESS IPPROTOCOL PORT')
if len(sys.argv)==1:
print "Error: No arguments supplied"
parse.print_help()
sys.exit(1)
args=parse.parse_args()
if os.path.isfile(args.inputfile):
openfile = open(args.inputfile, 'r')
else:
print "Error: Input File provided does not exist"
print "\nInput file must be in the following format: -"
print "\n IPADDRESS IPPROTOCOL PORT"
print "\neg \n\n 8.8.8.8 udp 53\n 192.168.100.1 tcp ssh"
sys.exit(1)
socket.setdefaulttimeout(1)
def socket_test(sockaddr, sockport, s):
try:
code = s.connect_ex((sockaddr,sockport))
if code == 0:
test_res = "Passed"
return test_res
else:
test_res = "Failed"
return test_res
s.close()
except Exception as e:
test_res = "Failed"
return test_res
finally:
s.close()
def http_test(httpaddr, httpport):
try:
response = urllib2.urlopen('http://' + httpaddr)
html = response.read()
test_res = "Passed"
return test_res
except ssl.CertificateError, e:
print e
test_res = "Passed"
return test_res
except urllib2.URLError, e:
if "timed out" in str(e):
test_res = "Failed"
else:
test_res = "Passed"
return test_res
except:
test_res = "Failed"
def https_test(httpsaddr, httpsport):
try:
response = urllib2.urlopen('https://' + httpsaddr)
html = response.read()
test_res = "Passed"
return test_res
except ssl.CertificateError, e:
print e
test_res = "Passed"
return test_res
except urllib2.URLError, e:
if "timed out" in str(e):
test_res = "Failed"
else:
test_res = "Passed"
return test_res
except:
test_res = "Failed"
def port_connect(address, port, proto):
if port == 443:
test_res = https_test(address, port)
return test_res
elif port == 80:
test_res = http_test(address, port)
return test_res
elif proto == "udp":
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
test_res = socket_test(address, port, s)
return test_res
elif proto == "tcp":
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
test_res = socket_test(address, port, s)
return test_res
passed = {}
failed = {}
for each in openfile:
address = each.split(" ")[0]
proto = each.split(" ")[1]
port = int(each.split(" ")[2])
testresult = port_connect(address, port, proto)
if testresult == "Passed":
passed[address] = (proto, port)
elif testresult == "Failed":
failed[address] = (proto, port)
print ""
print "|Address|Protocol|Port|"
print "|-------|--------|----|"
for each in passed:
print "|%s|%s|%s|" % (each, passed[each][0], passed[each][1])
print " "
print "**Total Passed: %s**" % len(passed)
print "\n---------------------------------------------------------"
print "| Address | Protocol | Port |"
print "---------------------------------------------------------"
for each in failed:
print "| %s | %s | %s |" % (each, failed[each][0], failed[each][1])
print "---------------------------------------------------------"
print "| Total Failed: %s |" % len(failed)
print "---------------------------------------------------------\n"