Security wants an alert if a server listens on any port not in an approved list. Write it.
IntermediateScenarioScenarios: health checks, monitoring and alerts
Answer
Situation read → Compare the current listening sockets against an allow-list.
What I'd do →
allowed=(22 80 443 9100)
mapfile -t listening < <(ss -Htlnu | awk '{print $5}' | sed -E 's/.*:([0-9]+)$/\1/' | sort -un)
unexpected=$(comm -13 <(printf '%s\n' "${allowed[@]}" | sort -u) <(printf '%s\n' "${listening[@]}"))
if [[ -n $unexpected ]]; then
echo "CRIT unexpected listening ports: $(paste -sd, <<<"$unexpected")"
ss -Htlnup | grep -E ":($(paste -sd'|' <<<"$unexpected"))\b"
exit 2
fi
ss -Hdrops the header,-p(as root) shows the owning process for the report.comm -13prints lines only in the second (current) list.
Why → An unexpected listener is either drift or compromise; both deserve attention.
Interview tip → Mention that comm needs both inputs sorted the same way.
More on Scenarios: health checks, monitoring and alerts
You are asked for a host health-check script covering disk, memory, systemd services and an HTTP endpoint. How do you design it?IntermediateYour disk alert fires and clears every few minutes around 80%, spamming Slack. How do you fix the alerting?AdvancedA server reports `No space left on device` but `df -h` shows 40% free. What would you add to the health checks?BeginnerAlert when any certificate under `/etc/letsencrypt/live` expires within 21 days.IntermediateA developer wrote a Bash loop that restarts the app whenever its process disappears. What do you suggest instead?IntermediateYou suspect a memory leak in a service. How do you collect evidence with a script before involving developers?Advanced