Show contents
Chapter 6 of 7 · 25 min
Project: a server audit script
By the end of this chapter, you'll be able to
- Combine find, awk and Bash functions in a structured script
- Write a strict-mode script that cleans up after itself with trap
- Produce a readable report and a usable exit status
- Test a script on a prepared environment
Preparing a test server
You never test an audit script directly in production: first you prepare a folder that imitates the server, with known problems.
mkdir -p srv/www/school-a srv/www/school-b srv/log
touch srv/www/school-a/index.html srv/www/school-b/index.html srv/www/school-b/config.php
chmod 644 srv/www/school-a/index.html srv/www/school-b/index.html
chmod 666 srv/www/school-b/config.php
head -c 6000000 /dev/zero > srv/log/old.log
cat > srv/log/access.log <<'EOF'
10.0.0.5 GET /school-a 200 120
10.0.0.8 GET /school-b/enrolment 500 2300
10.0.0.5 GET /school-a/calendar 200 90
10.0.0.9 POST /school-b/enrolment 500 2100
10.0.0.7 GET /school-b 200 150
EOF
find srv -type f | sort
srv/log/access.log
srv/log/old.log
srv/www/school-a/index.html
srv/www/school-b/config.php
srv/www/school-b/index.html
Three problems are hidden: a world-writable file, a 6 MB log, and 500 errors on the enrolment page.
The audit script
Each check is a function that prints its findings and counts the problems in a shared variable. The script ends with status 0 if all is well, 1 if it found problems: a monitoring tool or a scheduled task can then raise an alert.
cat > audit.sh <<'EOF'
#!/usr/bin/env bash
set -euo pipefail
root=${1:?usage: audit.sh FOLDER}
problems=0
tmp=$(mktemp)
trap 'rm -f "$tmp"' EXIT
report() {
echo " [!] $1"
problems=$((problems + 1))
}
check_permissions() {
echo "== Permissions"
find "$root" -perm -o+w > "$tmp"
while read -r path; do
report "writable by everyone: $path"
done < "$tmp"
}
check_logs() {
echo "== Logs over 5 MB"
find "$root" -name '*.log' -size +5M > "$tmp"
while read -r path; do
report "log too heavy: $path"
done < "$tmp"
}
check_errors() {
echo "== 500 errors"
awk '$4 >= 500 { nb[$3]++ } END { for (p in nb) print nb[p], p }' "$root/log/access.log" | sort -k1,1nr -k2,2 > "$tmp"
while read -r count page; do
report "$count 500 error(s) on $page"
done < "$tmp"
}
check_permissions
check_logs
check_errors
echo "Summary: $problems problem(s)"
[ "$problems" -eq 0 ]
EOF
chmod +x audit.sh
code=0
./audit.sh srv || code=$?
echo "exit status: $code"
== Permissions
[!] writable by everyone: srv/www/school-b/config.php
== Logs over 5 MB
[!] log too heavy: srv/log/old.log
== 500 errors
[!] 2 500 error(s) on /school-b/enrolment
Summary: 3 problem(s)
exit status: 1
A few of the script's choices:
${1:?usage: …}stops the script with a clear message if you forget the argument;- the results of
findandawkgo through a temporary file, then read bywhile read: if you wrotefind … | while read …, the loop would run in a subshell, and theproblemscounter would be lost when the loop ends; trap 'rm -f "$tmp"' EXITdeletes the temporary file, even if the script stops on an error;- the last line,
[ "$problems" -eq 0 ], sets the exit status.
Exit status 1 lets a scheduled task send an alert.
Text description of the animation
- The script checks it was given a folder, otherwise it stops with a usage message.
- First finding: a configuration file writable by any account.
- Second finding: a 6 MB log that should be archived.
- Third finding: the enrolment page crashes. This is often the most urgent.
- Exit status 1 lets a scheduled task send an alert.
Your turn
Worked exercise
Worked exercise: one more check
Add a check_scripts function to the script that reports every .sh file in the folder that does not contain the line set -euo pipefail. Test it on a tools folder holding one correct script and one script without strict mode.
Hint 1
grep -L "pattern" files… lists the files that do not contain the pattern. Combine it with find "$root" -name '*.sh' -exec grep -L … {} +.Hint 2
|| true after grep -L: grep returns a non-zero status when no file matches, which would stop the script in strict mode.Show the solution and common mistakes
mkdir -p tools
printf '#!/usr/bin/env bash\nset -euo pipefail\necho ok\n' > tools/backup.sh
printf '#!/usr/bin/env bash\necho ok\n' > tools/cleanup.sh
cat > check.sh <<'EOF'
#!/usr/bin/env bash
set -euo pipefail
root=$1
problems=0
report() { echo " [!] $1"; problems=$((problems + 1)); }
check_scripts() {
echo "== Scripts without strict mode"
local list
list=$(find "$root" -name '*.sh' -exec grep -L 'set -euo pipefail' {} + || true)
for path in $list; do
report "strict mode missing: $path"
done
}
check_scripts
echo "Summary: $problems problem(s)"
EOF
bash check.sh tools
== Scripts without strict mode
[!] strict mode missing: tools/cleanup.sh
Summary: 1 problem(s)
Common mistakes:
- Using
grep -l(lower case): it lists the files that do contain the pattern, the opposite of what you want. - Forgetting
|| true: when every script is correct,grep -Lfinds nothing, returns a non-zero status, and strict mode stops the audit without printing the summary. - Looping with
for path in $listwhen file names contain spaces: they would be cut into pieces. For a production audit, prefer the temporary file andwhile read -r.
Summary
- 1An audit script groups its checks in functions that share a problem counter.
- 2Strict mode, ${1:?usage} and trap EXIT make the script safe and tidy.
- 3A loop placed after a pipe runs in a subshell: its variables are lost when it ends.
- 4The exit status (0 or 1) lets a monitoring tool raise an alert.
- 5An audit finds and reports; fixing remains a separate step, decided by a human.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.