Skip to content

Instantly share code, notes, and snippets.

@sigmapie8
Last active May 6, 2019 20:25
Show Gist options
  • Save sigmapie8/2f536fb39d5d72a2e0382e7f0410a02b to your computer and use it in GitHub Desktop.
Save sigmapie8/2f536fb39d5d72a2e0382e7f0410a02b to your computer and use it in GitHub Desktop.
writing_excel
import os
from openpyxl import Workbook
# we're importing Workbook class from openpyxl module
source_location = "<by now you should know what goes here>"
searchString = "sha512WithECDSAEncryption"
servers = dict()
# A dictionary to keep track of my servers
# as you must remember dictionaries work with key:value pair
# so if my server supports sha512WithECDSAEncryption then
# I'll add server_name:1
# if not then, server_name:0 will be added to it.
row_number = 1
# row_number keeps track of which row I'll be writing in excel
def hasEncryption(file, encryptionString):
with open(file, "r") as server_config_file:
config_file_content = server_config_file.read()
if(encryptionString in config_file_content):
return True
return False
def log_unencrypted(file):
with open("unecrypted_files.txt", "a") as result_file:
result_file.write(file+"\n")
for i in os.walk(source_location):
path, dirs, files = i
for file in files:
if(not hasEncryption(path+"\\"+ file, searchString)):
log_unencrypted(file)
servers[file[:-5]] = 0
# file is supposed to be a string right? A string containing filename
# file[:] would have given us whole string with .yaml extension
# but we don't want some dirty extension in our excel,
# we want pure server name
# file[:-5] gives us the whole string except for the last 5 characters
# nifty way to remove the extension from the servername right?
# you can try different numbers to see how it works
else:
servers[file[:-5]] = 1
wb = Workbook()
# we made an object wb for our class Workbook
# whenevr we would need to do something related to excel sheets
# we'll be doing it through this object
# This means we have created a new excel file in RAM
sheet = wb.create_sheet('servers')
# what do you do after creating an excel?
# you create a sheet
# usually, in microsoft excel, you already get 3 sheets premade
# but here, we create them as we require them
# If you look at any sheet, it always has its columns denoted by alphabets
# and rows denoted by numbers
# so A1 points to a specific cell inside the sheet i.e. the first cell
sheet["A1"] = "Servers"
sheet["B1"] = "Encrypted"
# We created column name for our excel so that we can make sense of the data
# when we open it in microsoft excel
for server in servers:
# we are looping through all the keys inside the servers dictionary
# that means we'll get the server names one by one
sheet["A"+str(row_number+1)] = server
# notice the value of row number is 1 initially
# 1+1 = 2
# 2 then gets converted into a string
# that string gets attached to A and it becomes A2
# means second row and first column
# sheet["A2"] = server
# please note that the value of row_number remains the same
# we haven't changed it yet
sheet["B"+str(row_number+1)] = servers[server]
# sheet["B2"] = dictionary[key]
# that gives us sheet["B2"] = value
row_number += 1
# we update the row_number so that we can write the next row
# in the next iteration
wb.save("servers.xlsx")
# This is the most important part.
# we save our excel, by giving it a path+name
# since we have just given the name, that means the
# file will get created in the same location as our python
# script
# Notice how we used 'wb' object not the 'sheet' variable
# that's because we save the whole workbook and not just
# one sheet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment