online posts 16
linux and the command line 101

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.

you edit, in the labanswers/3.2.yml

open a file in VS Code: Ctrl+P (Cmd+P on a Mac), type its name, Enter

given
LessonPipes, redirection and grep
The logfiles/logs/r1.log, as "$LOG", like task 3.1
Your answersanswers/3.2.yml: one command per line
new to this? step by stepbeginner
  1. LOG=files/logs/r1.log, then grep LOGIN_FAILED "$LOG". Which field is the user name?
  2. Count them, count them per name with cut, sort, uniq -c and sort -rn, and list the names with sort -u.
  3. Put each command in answers/3.2.yml, then make check T=3.2.

more in meet the lab

1 explain

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

ToolDoes
sort -usorts and keeps each line once (unique): the same as sort | uniq
head -n 3after a sort, keeps the top 3
wc -lcounts lines; grep -c does the same for matching lines, in one step
2 show

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.

3 follow along

Find the field

  1. Set LOG again if this is a new terminal, and look at the failures:

    run in the terminal
    LOG=files/logs/r1.log
    grep LOGIN_FAILED "$LOG"
    
  2. 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 terminal
    grep LOGIN_FAILED "$LOG" | cut -d' ' -f8
    

    If that printed failed or from, count again: *Nov is field 1.

in one sentence Find the field once, by counting, then let cut pull it out of every line.

4 challenge

How many, and who

Three commands in answers/3.2.yml, each reading "$LOG":

  1. failed_total: how many failed logins there are, as one number.
  2. failed_by_user: failed logins per user name, biggest count first, like 3 netops.
  3. 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.2

when 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