【ログ解析とか】ワンライナーの早見

bashを想定しています。

思いついたら更新していきます。

ログと睨めっこ系

例としてアクセスログ(HTTP)を扱いますが、考え方は他でも応用可能だと思います。

# アクセス数ランキング トップ10
zcat -f /var/log/httpd/access_log* |awk '{print $1}' |sort |uniq -c |sort -rn |head -10

# アクセス数ランキング トップ10の国などを列挙
zcat -f /var/log/httpd/access_log* |awk '{print $1}' |sort |uniq -c |sort -rn |head -10 |awk '{print $2}' |while read ip ;do echo "ip = $ip" ;whois $ip ;done |egrep "ip \=|descr|country|Country"

# 時間毎のアクセス数
zcat -f /var/log/httpd/access_log |awk -F ':' '{print $2 ":" $3}' |uniq -c

# アクセスの多い時間ランキング
zcat -f /var/log/httpd/access_log* |awk -F ':' '{print $2 "時"}' |sort |uniq -c |sort -nr

# アクセスの多いページランキング トップ10
zcat -f /var/log/httpd/access_log* |awk '{print $7}' |sort |uniq -c |sort -nr |head -10

# 現在の同時接続数(ポート番号は任意)
netstat -na |grep ":80" |wc -l

# 現在の同時接続IPランキング(ポート番号は任意)
netstat -na |grep ":80" |awk '{print $5}' |awk -F ':' '{print $1}' |sort |uniq -c |sort -nr

# 現在の同時接続数が多いポートランキング
netstat -na |egrep ":[0-9][0-9][0-9][0-9][0-9]" |awk -F ':' '{print $2}' |awk '{print $1}' |sort |uniq -c |sort -nr

# 容量を食っているディレクトリ トップ10
du -chs /*/* 2>/dev/null |sort -hr |head -10

ブレイクタイム

毎日がエブリデイなコマンド集。

# 天気を見る
curl wttr.in/Tokyo &&curl wttr.in/moon

# 気温を見る
echo -e "Tokyo\n" |nc graph.no 79

# パンダ
curl http://artscene.textfiles.com/asciiart/panda

# StarWarsを見る(Telnetの抜け方は "Ctrl + C" の後にquit )
telnet towel.blinkenlights.nl 23

疎通確認とか

基本はTelnetで良いと思いますので、Telnetでは出来ないSSL/TLS通信とか。

# SSL/TLS通信の確認(ポートは任意)
openssl s_client -connect www.google.com:443 </dev/null

# 証明書の期限確認(ポートは任意)
openssl s_client -connect www.google.com:443 |openssl x509 -text |grep "Not"

# ダウンロード速度(bytes/sec)
# 詳細はコチラ:https://dev.classmethod.jp/articles/curl-benchmark/
seq 100 |while read ;do curl -w "code:%{http_code},speed:%{speed_download}\n" -o /dev/null -s https://www.google.com ;sleep 0.1 ;done

# SYNスキャン(自身の環境でのみ使用してください)
# ポート範囲とかhpingのオプションなどは適宜変更(bashとzshで動作確認済み)
# IP指定です
seq 0 100 |while read port ;do (sudo hping -S -p $port -c 1 192.168.0.1 2>/dev/null )&done |grep sport

# /24のホストスキャン(自身の環境でのみ使用してください)
seq 254 |while read host ;do (sudo hping -C 8 -c 1  192.168.0.${host} 2>/dev/null )&done |grep "ip=" |grep "rtt="

# /16のホストスキャン(自身の環境でのみ使用してください)
# 結構な負荷がかかるため、sleepの値で調整してください。(EC2のt2.microではsleep 5くらいが良さそうでした)
seq 254 |while read octet3 ;do (seq 254 |while read octet4 ;do (sudo hping -C 8 -c 1  172.20.${octet3}.${octet4} 2>/dev/null )&done );sleep 1 ;done |grep "ip=" |grep "rtt="