Bashでのユーティリティスクリプト(不定期更新)


1、lnoi.sh:現在のシステムにおける各IP(IPv 4)の接続数をリストします.
# cat lnoi.sh
#!/bin/bash

# Use for list connect numbers and IP address.

netstat -n | awk '/^tcp/{print $5}' | awk -F: '!/^::/{print $1}' | sort | uniq -c | sort -rn | awk 'BEGIN{printf "%-10s%s
","ConNum","IP"}{printf "%-10s%s
",$1,$2}'

2、lu.sh:UID>=500かつ!=65534のアカウント:
# cat lu.sh
#!/bin/bash

# Use for list conut for UID > 500 and not eq 65534.

awk -F: 'BEGIN{printf "%-15s%-7s%s
%-s
", "Username","UID","GID","=========================="} $3 >= 500 && $3 != 65534 {printf "%-15s%-7s%s
", $1,$3,$4} END{printf "%s
%s
", "==========================","List Over"}' /etc/passwd

3、lla.sh:nginxまたはapacheログで最もアクセスが高い上位10個のIPをリストします.
#!/bin/bash

# List top 10 IP on the http's access_log.
PATH=${PATH}

#access_file="/var/log/httpd/access_log-20131214"
access_file="/var/log/nginx/access.log"

cat $access_file | awk '{print $1}' | sort |uniq -c | sort -rn | head -n 10

4、イントラネットの下でアクティブなホストを検出する2つのスクリプト:
  (1)、# cat pinghost.sh
#!/bin/bash

netid="10.1.2."
n=0
for i in $(seq 1 254)
do
    ping -c1 -W1 -n $netid$i &> /dev/null
    if [ $? == 0 ]
    then
        echo -e "$netid$i is UP!"
        let n+=1
    fi
done
echo -e "====================
Totle: $n
"

  (2)、# cat scanhost.sh
#!/bin/bash

PATH=${PATH}
network="10.1.2.0/24"

#if [ $UID -ne 0 ]; then
#    echo "Run this script need as root identity."
#    exit 1
#fi
which nmap &> /dev/null
if [ $? -ne '0' ]; then
    echo "This script relies on "nmap" program, please install it first."
    exit 2
fi

nmap -sn $network | awk 'BEGIN{i=0}{i++}/report/{print $NF," is UP!"}END{printf "%-s
%-s%s
","==================","Totla: ",i}' | sed 's/(//' |sed 's/)//' exit 0

この2つの小さなscriptと比較すると、実際の効果は「scanhost.sh」のほうがよく、速度が速く、探査も多い.scanhostはnmapというプローブツールを使用してプローブするため、pingは遅延が長すぎる(1秒以内にパケットを返すことができず、ICMPパケットがクライアントファイアウォールにブロックされる可能性がある)ため、結果が不正確に検出できない場合があります.