📕 SYS 320 - tconklin-champlain/Tech-Journal GitHub Wiki

💡 To open Jupyter Lab open cmd and type jupyter lab and it will auto run the local server

Week 1

💡 Start jupyter labs terminal with Admin privledges or it won't work in root directory.

Tips:

  • Comments are a must in code
  • Start with test files or small files
  • Name your variables properly nothing random or jokes

💡 What is really useful in Jupyter Lab is you can code in small bites

# Create and interface to search through syslog files

# Open a file
with open('../logs/smallSyslog.log') as f:
    
    # read in the file and save it to a variable
    contents = f.readlines()
print(contents)

Most of what we do is just looking for keywords in logs so you could change "failure" with anything to search for anf it will print!

keywords = ['failure','session opened for user','exited abnormally']
# Loop through the list returned. Each element is a line fromt thee smallSyslog file
for line in contents:
    
    # Loops through the keywords
    for eachKeyword in keywords:
        
        # If the 'line' contains the keyword then it will print
        if eachKeyword in line:
            
            print(line)

Using regular expression will help this program a lot

Editing the keywords

keywords = ['sshd\(pam_unix\)\[[0-9]{3,8}\]: authentication failure;','session opened for user.*','exited abnormally']

Editing the loop

# Loop through the list returned. Each element is a line fromt thee smallSyslog file
for line in contents:
    
    # Loops through the keywords
    for eachKeyword in keywords:
        
        # Searches and returns results using a regular expression search
        x = re.findall(r''+eachKeyword+'', line)
        print(x)

Refining the code

  1. Create a new python file
  2. Change our code into a function / module syslogCheck.py
# Create and interface to search through syslog files
import re

def _syslog(filename,listOfKeywords):
    
    # Open a file
    with open(filename) as f:

        # read in the file and save it to a variable
        contents = f.readlines()
    # Lists to store the results
    results = []
    # Loop through the list returned. Each element is a line fromt thee smallSyslog file
    for line in contents:

        # Loops through the keywords
        for eachKeyword in listOfKeywords:

            # If the 'line' contains the keyword then it will print
            #if eachKeyword in line:
            # Searches and returns results using a regular expression search
            x = re.findall(r''+eachKeyword+'', line)
            # print(x)
            
            for found in x:
                
                # Append the returned keywords to the results list
                results.append(x)
                
    return results
  1. Import the modules into new program!

💡 Whenever you have an issue try printing the results so you can easily fix the problem!

Common Error

common

Solution

You need to make sure to import the module!!! commons

What I have learned

  • Create modules using unspecific values to copy and paste it to create others easily
  • You can use Markdown in Jupyter Lab which is amazing

Powershell

💡 Comment multiple lines CMD-K + CMD-C