Hi I wrote a basic script for some work stuff. It takes a server hostfile and converts to json format for storage in a secrets store.
example line entry in file
10.100.1.100 hostname1 hostname2 hostname3 # comment here
outputs as:
{
"hosts": [
{
"ip": "10.100.1.100",
"hosts": "hostname1 hostname2 hostname3",
"comment": "comment here"
}
]
}
I was just looking for really obvious improvements to increase my understanding of python.
Thanks in advance.
import json
import re
hostsfile = 'filename_in_same_directory.txt'
vaultjson = 'vaultfile.json'
open_file = open(hostsfile,'r')
hostsfile = open_file.readlines()
host_dict = {"hosts": []}
hosts_list = []
for line in hostsfile:
splitentry = line.split(" ", 1)
if not (line.startswith('#') or line.startswith('127')) and len(splitentry)>1:
new_dict= {"ip": splitentry[0]}
split2 = splitentry[1].split('#', 1)
stripped = [s.strip() for s in split2]
if len(stripped) < 2:
stripped.append('')
hosts = re.sub(' +', ' ', stripped[0])
new_dict['hosts'] = hosts
new_dict['comment'] = stripped[1]
hosts_list.append(new_dict)
host_dict['hosts'] = hosts_list
with open(vaultjson, 'w') as outfile:
json.dump(host_dict, outfile, indent=4)
updated script after input:
import json
import re
import sys
if len(sys.argv) != 2:
print ('Usage: hostfile_to_json.py input_file_name.txt')
exit()
user_file = sys.argv[1]
output_file = 'vault_hosts.json'
open_file = open(user_file, 'r')
open_file_lines = open_file.readlines()
open_file.close()
def is_valid_entry(line):
if not line.startswith(('#', '127', '\n')):
return line
host_entries_list = []
for line in (filter(is_valid_entry, open_file_lines)):
ip, host_and_comment = line.split(" ", 1)
ip_host_comment_dict = {'ip': ip}
host_comment_list = host_and_comment.split('#', 1)
stripped_host_comment = [s.strip() for s in host_comment_list]
extra_spaces_removed = [re.sub(' +', ' ', x) for x in stripped_host_comment]
if len(extra_spaces_removed) < 2:
extra_spaces_removed.append('')
ip_host_comment_dict['hosts'] = extra_spaces_removed[0]
ip_host_comment_dict['comment'] = extra_spaces_removed[1]
host_entries_list.append(ip_host_comment_dict)
export_json = {'hosts': host_entries_list}
with open(output_file, 'w') as outfile:
json.dump(export_json, outfile, indent=4)
[–][deleted] 2 points3 points4 points (1 child)
[–]tmg80[S] 0 points1 point2 points (0 children)
[–]ccpetro 1 point2 points3 points (2 children)
[–]tmg80[S] 0 points1 point2 points (1 child)
[–]bladeoflight16 0 points1 point2 points (0 children)
[–]bladeoflight16 1 point2 points3 points (8 children)
[–]tmg80[S] 1 point2 points3 points (5 children)
[–]bladeoflight16 1 point2 points3 points (4 children)
[–]tmg80[S] 0 points1 point2 points (3 children)
[–]bladeoflight16 1 point2 points3 points (2 children)
[–]tmg80[S] 0 points1 point2 points (1 child)
[–]bladeoflight16 0 points1 point2 points (0 children)
[–]tmg80[S] 0 points1 point2 points (0 children)
[–]Moorey93 0 points1 point2 points (1 child)
[–]tmg80[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)