Generation

generate functionSat, 12 Nov 2022

take column 1 from csv file and define these values in a table as OBJECT_IP. Then prompt for user input to provide a text value to be named OBJECT_DESC. Make this text value the table name. Then take the OBJECT_IP value replace any . with an _ then remove the numbers and _ from the beginning of the value up to the second _ and then add each row of the column to the end of the description with an _ in between the description and row value and retain this new value as OBJECT_NAME to match the row in the table of the OBJECT_IP value used. Then create a new file with output of OBJECT_NAME's on one line with the next line has the word host + the OBJECT_IP on this line. Then take all the OBJECT_NAME values and nest them in a group with the group name being the value of OBJECT_DESC.

import csv def get_input(): prompt1 = raw_input('Enter filename: ') prompt2 = raw_input('Enter OBJECT_DESC: ') return prompt1, prompt2 def read_csv(filename): table = {} with open(filename, 'rb') as csvfile: filereader = csv.reader(csvfile) for row in filereader: table[row[0]] = row[1] return table def my_function(filename, description): table = read_csv(filename) new_table = {} text = 'OBJECT_NAME' for i in table: new_table[i.replace('.', '_')] = table[i] + '_' + text with open('/root/' + description + '_objects', 'w') as f: for i in new_table: f.write(new_table[i] + '\n' + 'host ' + i + '\n')

Questions about programming?Chat with your personal AI assistant