티스토리 뷰

Linux 시스템을 운영하다 보면, 현재 시스템 이하 운용중인 Port 및 Port 이하에 연결된 Session Count의 확인이 필요할 때가 있다.

 

이럴때 아래와 같은 for문 명령을 통해 간략히 Session Count를 확인가능하다.

 

 

1. for문을 통한 간략한 session 수 확인

 

[root@Test01 ~]#
[root@Test01 ~]# for STAT in `echo "LISTEN ESTABLISHED TIME_WAIT CLOSE_WAIT"`; do echo "$(date) / ${STAT} - `netstat -na | awk '$6 ~ /^'"$STAT"'$/ {print $0}' | wc -l`"; done


Mon Dec  8 15:26:51 KST 2014 / LISTEN - 19
Mon Dec  8 15:26:51 KST 2014 / ESTABLISHED - 1301
Mon Dec  8 15:26:51 KST 2014 / TIME_WAIT - 35
Mon Dec  8 15:26:51 KST 2014 / CLOSE_WAIT - 1
[root@Test01 ~]#

 

 

만약 FIN_WAIT 와 같이 다른 상태값의 추가가 필요하다면 for 문의 loop 조건절로 사용중인 echo 사이에 추가하여 주면 된다. 

 

[root@Test01 ~]#
[root@Test01 ~]# for STAT in `echo "LISTEN ESTABLISHED TIME_WAIT CLOSE_WAIT FIN_WAIT"`; do echo "$(date) / ${STAT} - `netstat -na | awk '$6 ~ /^'"$STAT"'$/ {print $0}' | wc -l`"; done

 

Mon Dec  8 15:26:51 KST 2014 / LISTEN - 19
Mon Dec  8 15:26:51 KST 2014 / ESTABLISHED - 1301
Mon Dec  8 15:26:51 KST 2014 / TIME_WAIT - 35
Mon Dec  8 15:26:51 KST 2014 / CLOSE_WAIT - 1

Mon Dec  8 15:26:51 KST 2014 / FIN_WAIT - 0
[root@Test01 ~]#

 

 

2. Session 상태를 별도 정의하지 않고 모든 Session Count를 수집하려면 아래와 같이 가능하다.

 

[root@Test01~]#
[root@Test01~]# for STAT in `netstat -na | grep tcp | awk '{print $6}' | sort -u`; do echo "$(date) / ${STAT} - `netstat -na | awk '$6 ~ /^'"$STAT"'$/ {print $0}' | wc -l`"; done


Fri Dec  4 11:46:56 KST 2015 / CLOSE_WAIT - 0
Fri Dec  4 11:46:56 KST 2015 / ESTABLISHED - 11
Fri Dec  4 11:46:56 KST 2015 / FIN_WAIT2 - 0
Fri Dec  4 11:46:56 KST 2015 / LAST_ACK - 10
Fri Dec  4 11:46:56 KST 2015 / LISTEN - 1
Fri Dec  4 11:46:56 KST 2015 / TIME_WAIT - 151
[root@Test01~]#
[root@Test01~]#

 

 

 

3. Server 이하 LISTEN 된 Port를 기반으로 각 Port별 Session Count 정보 확인하기

 

[root@WEB1-1 ~]#
[root@WEB1-1 ~]# ./session_check.sh 80
Fri Jan 23 01:26:36 KST 2015 / LISTEN / Port 80 - 1
Fri Jan 23 01:26:36 KST 2015 / ESTABLISHED / Port 80 - 0
Fri Jan 23 01:26:36 KST 2015 / TIME_WAIT / Port 80 - 157
Fri Jan 23 01:26:36 KST 2015 / CLOSE_WAIT / Port 80 - 0
[root@WEB1-1 ~]#
[root@WEB1-1 ~]#
[root@WEB1-1 ~]#
[root@WEB1-1 ~]# ./session_check.sh
Fri Jan 23 01:26:40 KST 2015 / LISTEN / Port 2113 - 1
Fri Jan 23 01:26:40 KST 2015 / LISTEN / Port 936 - 1
Fri Jan 23 01:26:40 KST 2015 / LISTEN / Port 111 - 1
Fri Jan 23 01:26:40 KST 2015 / LISTEN / Port 2738 - 1
Fri Jan 23 01:26:40 KST 2015 / LISTEN / Port 21 - 1
Fri Jan 23 01:26:40 KST 2015 / LISTEN / Port 23 - 1
Fri Jan 23 01:26:40 KST 2015 / LISTEN / Port 8282 - 1
Fri Jan 23 01:26:40 KST 2015 / LISTEN / Port 2171 - 1
Fri Jan 23 01:26:40 KST 2015 / LISTEN / Port 8283 - 1
Fri Jan 23 01:26:40 KST 2015 / LISTEN / Port 8284 - 1
Fri Jan 23 01:26:40 KST 2015 / LISTEN / Port 8285 - 1
Fri Jan 23 01:26:40 KST 2015 / LISTEN / Port 8286 - 1
Fri Jan 23 01:26:40 KST 2015 / LISTEN / Port 8287 - 1
Fri Jan 23 01:26:40 KST 2015 / LISTEN / Port 448 - 1
Fri Jan 23 01:26:40 KST 2015 / LISTEN / Port 449 - 1
Fri Jan 23 01:26:41 KST 2015 / LISTEN / Port 5666 - 1
Fri Jan 23 01:26:41 KST 2015 / LISTEN / Port 80 - 1
Fri Jan 23 01:26:41 KST 2015 / LISTEN / Port 5555 - 1
Fri Jan 23 01:26:41 KST 2015 / LISTEN / Port 22 - 1
Fri Jan 23 01:26:41 KST 2015 / LISTEN / Port 442 - 1
Fri Jan 23 01:26:41 KST 2015 / LISTEN / Port 8443 - 1
Fri Jan 23 01:26:41 KST 2015 / LISTEN / Port 443 - 1
Fri Jan 23 01:26:41 KST 2015 / LISTEN / Port 444 - 1
Fri Jan 23 01:26:41 KST 2015 / LISTEN / Port 446 - 1
Fri Jan 23 01:26:41 KST 2015 / LISTEN / Port 447 - 1
Fri Jan 23 01:26:41 KST 2015 / ESTABLISHED / Port 2113 - 0
Fri Jan 23 01:26:41 KST 2015 / ESTABLISHED / Port 936 - 0
Fri Jan 23 01:26:41 KST 2015 / ESTABLISHED / Port 111 - 0
Fri Jan 23 01:26:41 KST 2015 / ESTABLISHED / Port 2738 - 0
Fri Jan 23 01:26:41 KST 2015 / ESTABLISHED / Port 21 - 0
Fri Jan 23 01:26:41 KST 2015 / ESTABLISHED / Port 23 - 0
Fri Jan 23 01:26:41 KST 2015 / ESTABLISHED / Port 8282 - 1
Fri Jan 23 01:26:41 KST 2015 / ESTABLISHED / Port 2171 - 1
Fri Jan 23 01:26:41 KST 2015 / ESTABLISHED / Port 8283 - 1
Fri Jan 23 01:26:41 KST 2015 / ESTABLISHED / Port 8284 - 1
Fri Jan 23 01:26:41 KST 2015 / ESTABLISHED / Port 8285 - 1
Fri Jan 23 01:26:41 KST 2015 / ESTABLISHED / Port 8286 - 1
Fri Jan 23 01:26:41 KST 2015 / ESTABLISHED / Port 8287 - 0
Fri Jan 23 01:26:41 KST 2015 / ESTABLISHED / Port 448 - 0
Fri Jan 23 01:26:41 KST 2015 / ESTABLISHED / Port 449 - 0
Fri Jan 23 01:26:41 KST 2015 / ESTABLISHED / Port 5666 - 0
Fri Jan 23 01:26:41 KST 2015 / ESTABLISHED / Port 80 - 0
Fri Jan 23 01:26:41 KST 2015 / ESTABLISHED / Port 5555 - 0
Fri Jan 23 01:26:41 KST 2015 / ESTABLISHED / Port 22 - 1
Fri Jan 23 01:26:41 KST 2015 / ESTABLISHED / Port 442 - 0
Fri Jan 23 01:26:41 KST 2015 / ESTABLISHED / Port 8443 - 0
Fri Jan 23 01:26:41 KST 2015 / ESTABLISHED / Port 443 - 0
Fri Jan 23 01:26:41 KST 2015 / ESTABLISHED / Port 444 - 0
Fri Jan 23 01:26:41 KST 2015 / ESTABLISHED / Port 446 - 0
Fri Jan 23 01:26:42 KST 2015 / ESTABLISHED / Port 447 - 0
Fri Jan 23 01:26:42 KST 2015 / TIME_WAIT / Port 2113 - 0
Fri Jan 23 01:26:42 KST 2015 / TIME_WAIT / Port 936 - 0
Fri Jan 23 01:26:42 KST 2015 / TIME_WAIT / Port 111 - 0
Fri Jan 23 01:26:42 KST 2015 / TIME_WAIT / Port 2738 - 0
Fri Jan 23 01:26:42 KST 2015 / TIME_WAIT / Port 21 - 0
Fri Jan 23 01:26:42 KST 2015 / TIME_WAIT / Port 23 - 0
Fri Jan 23 01:26:42 KST 2015 / TIME_WAIT / Port 8282 - 0
Fri Jan 23 01:26:42 KST 2015 / TIME_WAIT / Port 2171 - 0
Fri Jan 23 01:26:42 KST 2015 / TIME_WAIT / Port 8283 - 0
Fri Jan 23 01:26:42 KST 2015 / TIME_WAIT / Port 8284 - 0
Fri Jan 23 01:26:42 KST 2015 / TIME_WAIT / Port 8285 - 0
Fri Jan 23 01:26:42 KST 2015 / TIME_WAIT / Port 8286 - 0
Fri Jan 23 01:26:42 KST 2015 / TIME_WAIT / Port 8287 - 0
Fri Jan 23 01:26:42 KST 2015 / TIME_WAIT / Port 448 - 0
Fri Jan 23 01:26:42 KST 2015 / TIME_WAIT / Port 449 - 0
Fri Jan 23 01:26:42 KST 2015 / TIME_WAIT / Port 5666 - 0
Fri Jan 23 01:26:42 KST 2015 / TIME_WAIT / Port 80 - 180
Fri Jan 23 01:26:42 KST 2015 / TIME_WAIT / Port 5555 - 0
Fri Jan 23 01:26:42 KST 2015 / TIME_WAIT / Port 22 - 0
Fri Jan 23 01:26:42 KST 2015 / TIME_WAIT / Port 442 - 0
Fri Jan 23 01:26:42 KST 2015 / TIME_WAIT / Port 8443 - 0
Fri Jan 23 01:26:42 KST 2015 / TIME_WAIT / Port 443 - 0
Fri Jan 23 01:26:42 KST 2015 / TIME_WAIT / Port 444 - 0
Fri Jan 23 01:26:42 KST 2015 / TIME_WAIT / Port 446 - 0
Fri Jan 23 01:26:42 KST 2015 / TIME_WAIT / Port 447 - 0
Fri Jan 23 01:26:42 KST 2015 / CLOSE_WAIT / Port 2113 - 0
Fri Jan 23 01:26:42 KST 2015 / CLOSE_WAIT / Port 936 - 0
Fri Jan 23 01:26:42 KST 2015 / CLOSE_WAIT / Port 111 - 0
Fri Jan 23 01:26:42 KST 2015 / CLOSE_WAIT / Port 2738 - 0
Fri Jan 23 01:26:42 KST 2015 / CLOSE_WAIT / Port 21 - 0
Fri Jan 23 01:26:42 KST 2015 / CLOSE_WAIT / Port 23 - 0
Fri Jan 23 01:26:42 KST 2015 / CLOSE_WAIT / Port 8282 - 1
Fri Jan 23 01:26:42 KST 2015 / CLOSE_WAIT / Port 2171 - 0
Fri Jan 23 01:26:43 KST 2015 / CLOSE_WAIT / Port 8283 - 0
Fri Jan 23 01:26:43 KST 2015 / CLOSE_WAIT / Port 8284 - 0
Fri Jan 23 01:26:43 KST 2015 / CLOSE_WAIT / Port 8285 - 0
Fri Jan 23 01:26:43 KST 2015 / CLOSE_WAIT / Port 8286 - 0
Fri Jan 23 01:26:43 KST 2015 / CLOSE_WAIT / Port 8287 - 0
Fri Jan 23 01:26:43 KST 2015 / CLOSE_WAIT / Port 448 - 0
Fri Jan 23 01:26:43 KST 2015 / CLOSE_WAIT / Port 449 - 0
Fri Jan 23 01:26:43 KST 2015 / CLOSE_WAIT / Port 5666 - 0
Fri Jan 23 01:26:43 KST 2015 / CLOSE_WAIT / Port 80 - 0
Fri Jan 23 01:26:43 KST 2015 / CLOSE_WAIT / Port 5555 - 0
Fri Jan 23 01:26:43 KST 2015 / CLOSE_WAIT / Port 22 - 0
Fri Jan 23 01:26:43 KST 2015 / CLOSE_WAIT / Port 442 - 0
Fri Jan 23 01:26:43 KST 2015 / CLOSE_WAIT / Port 8443 - 0
Fri Jan 23 01:26:43 KST 2015 / CLOSE_WAIT / Port 443 - 0
Fri Jan 23 01:26:43 KST 2015 / CLOSE_WAIT / Port 444 - 0
Fri Jan 23 01:26:43 KST 2015 / CLOSE_WAIT / Port 446 - 0
Fri Jan 23 01:26:43 KST 2015 / CLOSE_WAIT / Port 447 - 0
[root@WEB1-1 ~]#
[root@WEB1-1 ~]#
[root@WEB1-1 ~]#

 

 

- Shell Script Souce Code

 

[root@WEB1-1 ~]#
[root@WEB1-1 ~]# cat ./session_check.sh
#!/bin/bash

export LANG=C
export LC_ALL=C

if [ $# = 0 ]
then
 for STAT in `echo "LISTEN ESTABLISHED TIME_WAIT CLOSE_WAIT"`;
 do
  for LIST in `netstat -na | awk '$6 ~ /^LISTEN$/ {print $4}' | cut -d ":" -f2- | sed 's#:##g'`
  do
   echo "$(date) / ${STAT} / Port ${LIST} - `netstat -na | awk '$1 ~ /^tcp$/ && $4 ~ /:'"$LIST"'$/ && $6 ~ /^'"$STAT"'$/ {print $0}' | wc -l`";
  done
 done
else
 for STAT in `echo "LISTEN ESTABLISHED TIME_WAIT CLOSE_WAIT"`;
 do
  echo "$(date) / ${STAT} / Port $1 - `netstat -na | awk '$1 ~ /^tcp$/ && $4 ~ /:'"$1"'$/ && $6 ~ /^'"$STAT"'$/ {print $0}' | wc -l`";
 done
fi
[root@WEB1-1 ~]#

[root@WEB1-1 ~]#

 

 

 

 

 

 

반응형
댓글
댓글쓰기 폼