네트워크 트래픽을 체크하는 쉘스크립트이다.
현재 접속자수도 체크를 해준다.
아래 스크립트 코드를 적절한 이름으로 저장하자.
vi traffic.sh
#!/bin/sh
if [ "$1" == "" ] ; then
echo "사용법 : $0 장치명 [delay]"
echo "예) $0 eth0 3 "
exit 1
fi
if [ "$2" == "" ] ; then delay=3 ; else delay=$2 ; fi
echo "시간 : 수신(Kbps) / 송신(Kbps) Connection"
while ( true ) ; do
rx1=`cat /proc/net/dev | grep $1 | awk -F: '{print $2}' | awk '{print $1}'`
tx1=`cat /proc/net/dev | grep $1 | awk -F: '{print $2}' | awk '{print $9}'`
sleep $delay
rx2=`cat /proc/net/dev | grep $1 | awk -F: '{print $2}' | awk '{print $1}'`
tx2=`cat /proc/net/dev | grep $1 | awk -F: '{print $2}' | awk '{print $9}'`
conn=`netstat -n | grep EST | wc -l`
# 1024/8 == 128
rx3=$((($rx2-$rx1)*8/1024/$delay))
tx3=$((($tx2-$tx1)*8/1024/$delay))
echo "`date '+%k:%M:%S'` : $rx3 / $tx3 $conn"
done
if [ "$1" == "" ] ; then
echo "사용법 : $0 장치명 [delay]"
echo "예) $0 eth0 3 "
exit 1
fi
if [ "$2" == "" ] ; then delay=3 ; else delay=$2 ; fi
echo "시간 : 수신(Kbps) / 송신(Kbps) Connection"
while ( true ) ; do
rx1=`cat /proc/net/dev | grep $1 | awk -F: '{print $2}' | awk '{print $1}'`
tx1=`cat /proc/net/dev | grep $1 | awk -F: '{print $2}' | awk '{print $9}'`
sleep $delay
rx2=`cat /proc/net/dev | grep $1 | awk -F: '{print $2}' | awk '{print $1}'`
tx2=`cat /proc/net/dev | grep $1 | awk -F: '{print $2}' | awk '{print $9}'`
conn=`netstat -n | grep EST | wc -l`
# 1024/8 == 128
rx3=$((($rx2-$rx1)*8/1024/$delay))
tx3=$((($tx2-$tx1)*8/1024/$delay))
echo "`date '+%k:%M:%S'` : $rx3 / $tx3 $conn"
done
파일을 저장한 다음에 퍼미션을 700으로 변경해주자.
chmod 700 traffic.sh
사용방법은
# ./traffic.sh eth0
시간 : 수신(Kbps) / 송신(Kbps) Connection
10:28:38 : 24 / 1016 18
10:28:41 : 38 / 1119 18
10:28:44 : 38 / 1259 16
10:28:47 : 58 / 1910 16
10:28:50 : 78 / 2033 16
10:28:53 : 45 / 1611 16
10:28:56 : 53 / 1649 16
10:28:59 : 49 / 1477 16
10:29:02 : 43 / 1525 16
10:29:05 : 44 / 1598 19
10:29:08 : 60 / 1774 16
10:29:12 : 150 / 2493 15
10:29:15 : 394 / 5967 18
10:28:38 : 24 / 1016 18
10:28:41 : 38 / 1119 18
10:28:44 : 38 / 1259 16
10:28:47 : 58 / 1910 16
10:28:50 : 78 / 2033 16
10:28:53 : 45 / 1611 16
10:28:56 : 53 / 1649 16
10:28:59 : 49 / 1477 16
10:29:02 : 43 / 1525 16
10:29:05 : 44 / 1598 19
10:29:08 : 60 / 1774 16
10:29:12 : 150 / 2493 15
10:29:15 : 394 / 5967 18
위 처럼 3초마다 체크해서 출력해준다.
간격을 조절할 수도 있다.
# ./traffic.sh eth0 5
시간 : 수신(Kbps) / 송신(Kbps) Connection
10:30:54 : 74 / 2666 20
10:30:59 : 40 / 2265 21
10:31:04 : 82 / 2898 21
10:31:09 : 90 / 2957 22
10:31:14 : 88 / 2658 22
10:31:19 : 72 / 2123 22
10:31:24 : 69 / 2425 22
10:31:29 : 52 / 1672 21
10:31:34 : 69 / 1999 21
10:31:39 : 48 / 1518 22
10:31:44 : 71 / 2065 21
10:30:54 : 74 / 2666 20
10:30:59 : 40 / 2265 21
10:31:04 : 82 / 2898 21
10:31:09 : 90 / 2957 22
10:31:14 : 88 / 2658 22
10:31:19 : 72 / 2123 22
10:31:24 : 69 / 2425 22
10:31:29 : 52 / 1672 21
10:31:34 : 69 / 1999 21
10:31:39 : 48 / 1518 22
10:31:44 : 71 / 2065 21
5초 마다 체크한 결과물이다.