task 3.2 · chapter 3 of 8: pipes, redirection and grep · 20 min
Who's knocking?
objective
From r1’s log, count the failed logins, list them per user name, most first, and list every name that failed, once each. One pipeline each, that works on logs it hasn’t seen.
answers/3.2.ymlopen a file in VS Code: Ctrl+P (Cmd+P on a Mac), type its name, Enter
| Lesson | Pipes, redirection and grep |
|---|---|
| The log | files/logs/r1.log, as "$LOG", like task 3.1 |
| Your answers | answers/3.2.yml: one command per line |
new to this? step by stepbeginner
LOG=files/logs/r1.log, thengrep LOGIN_FAILED "$LOG". Which field is the user name?- Count them, count them per name with
cut,sort,uniq -candsort -rn, and list the names withsort -u. - Put each command in answers/3.2.yml, then
make check T=3.2.
more in meet the lab
A log is evidence
Why failed logins matter
Every router and server on the internet sees logins it didn’t expect: someone, or more often a script, trying
common user names and passwords. One failure is a typo. Thirty for admin in a minute is a scan. A pipeline
turns a page of log lines into the two questions that matter: how many, and who.
in one sentence Counting failures per user name tells a typo from a scan in one line.
Three tools you haven’t used yet
| Tool | Does |
|---|---|
sort -u | sorts and keeps each line once (unique): the same as sort | uniq |
head -n 3 | after a sort, keeps the top 3 |
wc -l | counts lines; grep -c does the same for matching lines, in one step |
The lines to work from
$ grep LOGIN_FAILED "$LOG" | head -n 3
*Nov 11 05:42:38.832: %CAPEOS-4-LOGIN_FAILED: Login failed for guest from 172.80.102.1 port 41694
*Nov 11 05:42:47.185: %CAPEOS-4-LOGIN_FAILED: Login failed for netops from 172.80.102.1 port 41698
*Nov 11 05:43:01.375: %CAPEOS-4-LOGIN_FAILED: Login failed for netops from 172.80.102.1 port 33882
These are real: the lab tried to log in to r1 with names r1 doesn’t know. Every attempt came from
172.80.102.1, the codespace’s address on the management network.
Find the field
Set
LOGagain if this is a new terminal, and look at the failures:run in the terminalLOG=files/logs/r1.log grep LOGIN_FAILED "$LOG"Count the words in one of those lines, the way
cut -d' 'will. Which field is the user name? Try your guess:run in the terminalgrep LOGIN_FAILED "$LOG" | cut -d' ' -f8If that printed
failedorfrom, count again:*Novis field 1.
in one sentence Find the field once, by counting, then let cut pull it out of every line.
How many, and who
Three commands in answers/3.2.yml, each reading "$LOG":
failed_total: how many failed logins there are, as one number.failed_by_user: failed logins per user name, biggest count first, like3 netops.users_once: every user name that failed, once each, from A to Z, one per line.
Then make check T=3.2. The checker’s other logs have names r1.log doesn’t, so nothing can be typed in by hand.
hint
The first is one grep with an option you used in task 3.1.
hint
The second is task 3.1’s downs_per_interface again, on different lines and a different field.
hint
The third needs no counting at all: cut out the names, then sort -u.
show the solution
failed_total: grep -c LOGIN_FAILED "$LOG"
failed_by_user: grep LOGIN_FAILED "$LOG" | cut -d' ' -f8 | sort | uniq -c | sort -rn
users_once: grep LOGIN_FAILED "$LOG" | cut -d' ' -f8 | sort -u
On r1.log:
3 netops
2 support
2 guest
1 operator
netops exists on ws1 and srv1, but not on r1, so to r1 it’s a stranger. support and guest have the same
count, so either can come first.
5 check: run it, then prove it
$ make check T=3.2when it's right, you'll see
╭─ task 3.2 · who's knocking?
│ ✓ how many failed logins (challenge): right on r1.log and on three logs it hadn't seen
│ ✓ failed logins per user, most first (challenge): right on r1.log and on three logs it hadn't seen
│ ✓ every user who failed, once each, A to Z (challenge): right on r1.log and on three logs it hadn't seen
╰─ 3/3 checks passed verified