0% found this document useful (0 votes)
5 views2 pages

Python Automation Essentials Guide

The document outlines essential Python components for automation, including variables, conditional statements, iterative statements, functions, and techniques for working with strings and lists. It provides an example of counting logins for a flagged user using a for loop, conditional statements, and a counter variable, emphasizing the importance of functions for code reuse. Additionally, it discusses working with files, specifically .txt and .csv formats, which are commonly used for security logs.

Uploaded by

scrapirontri
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Python Automation Essentials Guide

The document outlines essential Python components for automation, including variables, conditional statements, iterative statements, functions, and techniques for working with strings and lists. It provides an example of counting logins for a flagged user using a for loop, conditional statements, and a counter variable, emphasizing the importance of functions for code reuse. Additionally, it discusses working with files, specifically .txt and .csv formats, which are commonly used for security logs.

Uploaded by

scrapirontri
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd

Essential Python components for automation

Variables
A variable is a container that stores data. Variables are essential for automation. Without them, you
would have to individually rewrite values for each action you took in Python.

Conditional statements
A conditional statement is a statement that evaluates code to determine if it meets a specified set of
conditions. Conditional statements allow you to check for conditions before performing actions. This is
much more efficient than manually evaluating whether to apply an action to each separate piece of data.

Iterative statements
An iterative statement is code that repeatedly executes a set of instructions. You explored two kinds of
iterative statements: for loops and while loops. In both cases, they allow you to perform the same
actions a certain number of times without the need to retype the same code each time. Using a for loop
allows you to automate repetition of that code based on a sequence, and using a while loop allows you
to automate the repetition based on a condition.

Functions
A function is a section of code that can be reused in a program. Functions help you automate your
tasks by reducing the need to incorporate the same code multiple places in a program. Instead, you can
define the function once and call it wherever you need it.
You can develop your own functions based on your particular needs. You can also incorporate the built-
in functions that exist directly in Python without needing to manually code them.

Techniques for working with strings


String data is one of the most common data types that you'll encounter when automating cybersecurity
tasks through Python, and there are a lot of techniques that make working with strings efficient. You
can use bracket notation to access characters in a string through their indices. You can also use a variety
of functions and methods when working with strings, including str(), len(), and .index().

Techniques for working with lists


List data is another common data type. Like with strings, you can use bracket notation to access a list
element through its index. Several methods also help you with automation when working with lists.
These include .insert(), .remove(), .append(), and .index().
Example: Counting logins made by a flagged user
As one example, you may find that you need to investigate the logins of a specific user who has been
flagged for unusual activity. Specifically, you are responsible for counting how many times this user
has logged in for the day. If you are given a list identifying the username associated with each login
attempt made that day, you can automate this investigation in Python.
To automate the investigation, you'll need to incorporate the following Python components:
• A for loop will allow you to iterate through all the usernames in the list.
• Within the for loop, you should incorporate a conditional statement to examine whether each
username in the list matches the username of the flagged user.
• When the condition evaluates to True, you also need to increment a counter variable that keeps
track of the number of times the flagged user appears in the list.
Additionally, if you want to reuse this code multiple times, you can incorporate it into a function. The
function can include parameters that accept the username of the flagged user and the list to iterate
through. (The list would contain the usernames associated with all login attempts made that day.) The
function can use the counter variable to return the number of logins for that flagged user.

Working with files in Python


One additional component of automating cybersecurity-related tasks in Python is understanding how to
work with files. Security-related data will often be initially found in log files. A log is a record of
events that occur within an organization's systems. In logs, lines are often appended to the record as
time progresses.
Two common file formats for security logs are .txt files and .csv files. Both .txt and .csv files are types
of text files, meaning they contain only plain text. They do not contain images and do not specify
graphical properties of the text, including font, color, or spacing. In a .csv file, or a "comma-separated
values" file, the values are separated by commas. In a .txt file, there is not a specific format for
separating values, and they may be separated in a variety of ways, including spaces.

You might also like