0

at the moment I am able to create one CSV file with all the content I get at once.

Now I would like to create a list where I have different names in it.

How can I produce for every functioncall a different CSV file name? I thought about looping a list but I just want a +1 iteration at each call. I thought about saving my state somehow and use it in next functioncall. Everytime I initialize my variable with 0 and so I don't get 1. I think I could do it with Python Function Parameter calls but I have no idea how to use it. Can someone give me a little tip or example? If there are better ideas (maybe my idea is totally bullshit), how to solve this, just help please.

The comments in the code shall represent my imagination.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from tenable.sc import SecurityCenter as SC
import os.path
import sys
import getpass
import csv

SC_HOST = '...'

def parse_entry(entry): 
  split_after_path = ''
  ip = entry.get('ip', None)
  pluginText = entry.get('pluginText', None)

  if 'Path : ' in pluginText:
      for line in pluginText.splitlines(0):      
          if 'Path : ' in line:
              split_after_path_in_plugintext = line.split("Path : ",1)[1]

               # place = ['place1', 'place2', 'place3', 'place4', 'place5']
               # i = 0
               # i = i+1

               file_exists = os.path.isfile('testfile_path.csv')
               # file_exists = os.path.isfile('testfile_path_'+place[i]+'.csv')

               data = open('testfile_path.csv', 'a')
               # data = open('testfile_path_'+place[i]+'.csv', 'a')

               with data as csvfile:
                    header = ['IP Address', 'Path']
                    writer = csv.DictWriter(csvfile, lineterminator='\n', quoting=csv.QUOTE_NONNUMERIC, fieldnames=header)

                    if not file_exists:
                       writer.writeheader()

                    writer.writerow({'IP Address': ip, 'Path': split_after_path})
               data.close()


def main():
  sc_user = input('[<<] username: ')
  sc_pass = getpass.getpass('[<<] password: ')
  sc = SC(SC_HOST)
  sc.login(sc_user, sc_pass)

  # Query API for data

  # asset = [12,13,14,25,29]
  # i = 0
  # assetid = asset[i]
  # vuln = sc.analysis.vulns(('pluginID', '=', '25072')('asset','=','assetid'))
  # i = i+1

  vuln = sc.analysis.vulns(('pluginID', '=', '25072'),('asset','=','11'))

  for entry in vuln:
      parse_entry(entry)
  sc.logout()
  return 0

if __name__ == '__main__':
  sys.exit(main())
jalazbe
  • 1,801
  • 3
  • 19
  • 40
erDi
  • 65
  • 1
  • 7

2 Answers2

0

You can use a function attribute to keep track of the number of times the function has been called.

def parse_entry(entry):
  parse_entry.i += 1

# outside the function you have to initialize the attribute
parse_entry.i = 0

Or you can look at other ways to initialize the function attribute in this post.

Alternatively, you can use glob to get the current number of files.

from glob import glob
i = len(glob('testfile_path_*.csv'))
emilyfy
  • 183
  • 1
  • 8
0

The simplest and most obvious solution is to pass the full file path to your parse_entry function, ie:

def parse_entry(entry, filepath): 

  # ...

  if 'Path : ' in pluginText:
      for line in pluginText.splitlines(0):      
          if 'Path : ' in line:

               # ...    

               file_exists = os.path.isfile(filepath)    
               with open(filepath, 'a') as csvfile:
                  # ...

Then in main() use enumerate() to build sequential filenames:

def main():

  # ...

  for i, entry in enumerate(vuln):
      path = "'testfile_path{}.csv".format(i)
      parse_entry(entry, path)
bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118