Monday, 9 March 2009

Firewall port testing #2

Well another week and some more tweaks to my firewall port testing python script. I have added a new column to the input and results, so you can have an info field and also added a header row. Also the results filename will now have a date stamp and if running under windows it will have the host name in the file to help ordering and what now.

PortTester.py
#! /usr/bin/python

import telnetlib
import thread
import os
import datetime


class PortTester(object):

thread_count = 0

def __init__(self, input, log):

for row in input:
thread.start_new(self.testPort, (row['info'], row['host'], row['port'], log))
self.thread_count = self.thread_count + 1
while self.thread_count > 0:
pass


def testPort(self, info, host, port, log):
print 'Testing %s. host %s on port %s' % (info, host, port)
try:
connection = telnetlib.Telnet(host, port)
log.write('%s,%s,%s,pass\n' % (info,host,port))
log.flush()
except:
log.write('%s,%s,%s,fail\n' % (info,host,port))
log.flush()
self.thread_count = self.thread_count - 1

def main():
test_list = readCSV('input.txt', ',', 1)
output = open('results_%s_%s.txt'%(os.getenv('COMPUTERNAME'),datetime.datetime.now().strftime('%Y%m%d-%H%M%S')), 'w')
output.write('info,host,port,result\n')
output.flush()
PortTester(test_list, output)

def readCSV(path, delimiter, header_row):
text = open(path, 'r').read()
text = text.replace('%s%s' % (delimiter, delimiter), '%s %s' % (delimiter, delimiter))
lines = text.split('\n')
rows = []
if header_row:
headers = []
if lines[0] != '':
headers = lines[0].split(delimiter)
tmp = []
for head in headers:
tmp.append(head.strip().lower())
headers = tmp
del lines[0]
for line in lines:
if line != '':
values = line.split(delimiter)
row = {}
for i in range(0, len(headers)):
row[headers[i]] = values[i].strip()
rows.append(row)
else:
for line in lines:
if line != '':
values = line.split(delimiter)
rows.append(values)
return rows

if __name__ == '__main__':
main()


input.txt
  Info      , Host    ,  Port   
Google 81, google.com,81
Google Work,google.com,80
Google 82,google.com,82
Google IP,74.125.67.100,80
Google IP 81 , 74.125.67.100 , 81
Blog Work,blog.karit.geek.nz,80
Blog Fail,blog.karit.geek.nz,81


results.txt
info,host,port,result
Google IP,74.125.67.100,80,pass
Google Work,google.com,80,pass
Blog Work,blog.karit.geek.nz,80,pass
Google IP 81,74.125.67.100,81,fail
Blog Fail,blog.karit.geek.nz,81,fail
Google 81,google.com,81,fail
Google 82,google.com,82,fail

0 comments: