leonteale

NessusParser.py

Sep 8th, 2015
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.76 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import csv
  4. import sys
  5.  
  6. # Name of file
  7. finput = sys.argv[1]
  8. foutput = "newfile.csv"
  9.  
  10. # The items to get
  11. items = ['Risk', 'Name']
  12. itemindex = []
  13. finalList = []
  14.  
  15. prio = { 'None':0, 'Low':1, 'Medium':2, 'High':4, 'Critical':8 }
  16.  
  17.  
  18. # Get the lines in the file
  19. def map_function(line):
  20.     # Extract only needed items
  21.     tmp = [line[i].rstrip() for i in itemindex]
  22.     return [1, tmp]
  23.  
  24. # Combine same lines
  25. def red_function(tp, finalList):
  26.     for ind, i in enumerate(finalList):
  27.         if i[1] == tp[1]:
  28.             finalList[ind][0] += tp[0]
  29.             return
  30.     else:
  31.         finalList.append(tp)
  32.  
  33. # Open the file
  34. with open(finput, 'r') as csvfile:
  35.     # Get file iterator
  36.     rows = csv.reader(csvfile)
  37.    
  38.     # Get the title
  39.     title = rows.next()
  40.     itemindex = []
  41.     for i in items:
  42.         if i in title:
  43.             itemindex.append(title.index(i))
  44.         else:
  45.             print i, "is not a column!"
  46.    
  47.     # Get input statistics
  48.     map_result = map(map_function, rows)
  49.     # Reduce same lines
  50.     for i in map_result:
  51.         red_function(i, finalList)
  52.  
  53. # Open output file
  54. with open(foutput, 'w') as csvfile:
  55.     # Get file to be written
  56.     rows = csv.writer(csvfile)
  57.     rows.writerow(['#'] + items)
  58.     # Sort by risk
  59.     if 'Risk' in items:
  60.         n = items.index('Risk')
  61.         for i in sorted(finalList, key=lambda x: (prio[x[1][n]], x[0]), reverse=True):
  62.             # Change all None to Info
  63.             if (i[1][n] == 'None'):
  64.                 i[1][n] = 'Info'
  65.             rows.writerow(i[0:1] + i[1])
  66.     else:
  67.         # Sort by frequency if risk is not present
  68.         for i in sorted(finalList, key=lambda x: x[0], reverse=True):
  69.             rows.writerow(i[0:1] + i[1])
Advertisement
Add Comment
Please, Sign In to add comment