0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【SadServers学習メモ #2】使ったコマンドまとめ(awk, sort, uniq, head)

Posted at

初めに

Linuxの学習目的でSadServersを始めました。
備忘のために使用したコマンドをアウトプットします。

SadServersとは

Linuxの学習ができるサイト。
架空のサーバを題材とし与えられた依頼をこなすことで学習ができる。

今回のお題

タイトル

"Saskatoon": counting IPs.

詳細

  • Webサーバのアクセスログを見て、どのIPからのアクセスが一番多いかを特定する
アクセスログ
$ head -n 3 /home/admin/access.log 
83.149.9.216 - - [17/May/2015:10:05:03 +0000] "GET /presentations/logstash-monitorama-2013/images/kibana-search.png HTTP/1.1" 200 203023 "http://semicomplete.com/presentations/logstash-monitorama-2013/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.77 Safari/537.36"
83.149.9.216 - - [17/May/2015:10:05:43 +0000] "GET /presentations/logstash-monitorama-2013/images/kibana-dashboard3.png HTTP/1.1" 200 171717 "http://semicomplete.com/presentations/logstash-monitorama-2013/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.77 Safari/537.36"
83.149.9.216 - - [17/May/2015:10:05:47 +0000] "GET /presentations/logstash-monitorama-2013/plugin/highlight/highlight.js HTTP/1.1" 200 26185 "http://semicomplete.com/presentations/logstash-monitorama-2013/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.77 Safari/537.36"

作業の流れ

  • アクセスログからIPアドレスを抽出する
    • awkコマンド
  • IPアドレスごとの数を数える
    • uniqコマンド
  • 一番数が多いIPアドレスを特定する
    • sortコマンド
    • headコマンド

使ったコマンド

awkコマンド

ファイルから文字列を抽出するコマンド。

$ awk 'パターン { アクション }'
  • パターンには抽出する行の条件を記載する。
  • アクションには抽出した行に対してどんな処理を行うかを記載する。

例)IPアドレスを抽出したい

  • パターン
    • 全行抽出するのでパターン指定はなし
  • アクション
    • IPアドレスのみを抽出したい
      • printコマンドを用いて出力する
      • IPアドレスは行頭にあるため、$1で指定可能
IPアドレスを抽出するコマンド
$ awk '{print $1}' /home/admin/access.log
63.140.98.80
63.140.98.80
66.249.73.135
180.76.6.56
46.105.14.53

$1$2は空白やタブで区切られたn個目の文字を抽出するコマンド。
-Fで区切り文字を変更できる。
$0で行全体を指定可能。

sortコマンド

順番を並び替えるコマンド
-r:降順(デフォルトは昇順)
-n:対象を数値として扱う(デフォルトは文字列として扱う)

$ sort -r -n ファイル名

uniqコマンド

重複をなくした一覧を出力するコマンド。
今回は-cをつけて重複をカウントした。

$ uniq -c ファイル名

uniqコマンドは隣り合う行のみ重複とカウントする。
ファイル全体の重複をカウントしたい場合はsortしてからuniq -cを行う必要がある。

headコマンド

先頭からn行抽出する。
-n:先頭からn行抽出する。

$ head -n 5 ファイル名

回答

※出力結果はダミーです。

$ awk '{print $1}' /home/admin/access.log | sort | uniq -c | sort -n -r | head -n 1 | awk '{print $2}'
1.1.1.1
  • awk '{print $1}' /home/admin/access.log:IPアドレスの抽出
  • sort:並び替え(★uniqで全てのIPアドレスの重複をなくすために必須!)
  • uniq -c:IPアドレスごとのリクエスト数を数える
  • sort -n -r:リクエスト数で並び替え
  • head -n 1:一番多いリクエストを抽出
  • awk '{print $2}':抽出されたリクエスト数 IPアドレスのフォーマットからIPアドレスのみを抽出

終わりに

sortが隣り合う行しか重複を排除しないのを初めて知りました。ChatGPTありがとう。

ここまでご覧いただきありがとうございました!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?