OpenCyber Academy
Technical · Chapter 4 · English
Course code: OCA-057Chapter 04: OCA-057-C004

Chapter 4 — Scripting and data formats

Open, free learning for anyone who wants to protect people and technology. No account is required to read this text or its worked answers.

Lesson map

Lesson 04 published. Map, example, EX013.

flowchart TD
  N0["Learning outcomes"]
  N1["4.1 Why scripts help"]
  N2["4.2 CSV and JSON"]
  N3["4.3 Processing logs safely"]
  N4["4.4 Making output useful"]
  N5["Synthesis"]
  N0 --> N1 --> N2 --> N3 --> N4 --> N5
  N5 --> EX["Worked example"]
  EX --> Q["Exercise EX013"]

Logical drawing

flowchart LR
  N0["Learning outcomes"]
  N1["4.1 Why scripts help"]
  N2["4.2 CSV and JSON"]
  N3["4.3 Processing logs safely"]
  N4["4.4 Making output useful"]
  N5["Synthesis"]
  N0 --> N1 --> N2 --> N3 --> N4 --> N5 --> Q["EX013"]
[Learning outcomes]
    |
    v
[4.1 Why scripts help]
    |
    v
[4.2 CSV and JSON]
    |
    v
[4.3 Processing logs safely]
    |
    v
[4.4 Making output useful]
    |
    v
[Synthesis]
    |
    v
[Worked example] --> [Exercise EX013]

Learning outcomes

You will (1) write simple Python scripts; (2) read CSV and JSON; (3) process logs safely.

4.1 Why scripts help

Technicians repeat checks: which certificates expire soon, which accounts failed to log in, which configurations changed. Short Python scripts using only the standard library make these checks fast and repeatable. Amina writes scripts that read a file, process each record and print a short summary. She keeps them under fifty lines where possible and comments why each step exists. All course data are synthetic. Scripts are tools for careful work; she reads every line and tests each script on small examples before trusting its output.

4.2 CSV and JSON

Data often arrive as CSV, a table with one record per line and commas between fields, or JSON, a structured format with keys and values. Python's csv and json modules read both. Amina uses CSV for lists such as user accounts and certificate inventories, which Rosa can open in a spreadsheet, and JSON for structured exports from devices. She documents the expected fields and checks each record for missing values, reporting how many were skipped rather than ignoring them silently.

4.3 Processing logs safely

Logs are text written by devices and sometimes include text supplied by users, such as usernames. Amina never runs log text as a command and never inserts it into a shell. She treats every field as data, checks it matches the expected format and counts records that do not. She also avoids copying logs to personal devices, keeping them on the institute's lab storage. These habits protect both the analysis and the organisation's data, and they are easy to build from the first script.

4.4 Making output useful

A good script answers a question in a few lines. Amina's scripts print aligned columns with headings, a summary line and any warnings, such as how many records were malformed. She uses clear labels such as "renew now" rather than codes. Rosa will use some scripts after Amina's course, so the output must make sense to someone who did not write the script. Useful output also makes errors visible, because readers can check that the summary matches the detail. She asks Rosa to try each script once.

Worked example

Amina's certificate script reads a CSV of six certificates, skips one malformed row and prints a sorted table with statuses.

Synthetic case. Data are synthetic.

Errors and limits

Silently skipping malformed records can hide important data.

Exercises

Synthesis

Simple, safe scripts reading standard formats make routine checks reliable.

Continue studying — chapter 04 of 18

← Chapter 03 (draft) · Course chapter index · Chapter 05 (draft) →

Worked answers

EX013. For simple tables, such as account lists, that Rosa can open in a spreadsheet.

EX014. Counted and reported in the output.

EX015. It may contain user-supplied text that could be executed.

EX016. So people who did not write the script understand the output.