사이트 내 전체검색
검색목록
웹서버 과다접속 IP 찾아내기
로빈아빠
https://cmd.kr/server/522 URL이 복사되었습니다.

본문

웹로그를 이용하면 어떤 IP가 많이 접속하는 지를 알 수 있다.
과다접속 IP를 적발한 다음, Apache httpd.conf에서 접속을 거부하거나 방화벽 레벨에서 접속거부 처리를 할 수 있다.
다음은 웹로그의 끝부분을 읽어서 IP별로 접속 카운트를 조사하고 리포트하는 파이썬프로그램이다.
접속을 많이 하는 IP를 찾아내서 그 IP의 로그를 일부 뿌려주고, nslookup 결과도 보여준다.
로그 중에서 browser 표시 부분(라인 끝 부근)을 보면 유명검색엔진의 로봇이 접속한 것인지 알 수 있다.
Googlebot은 구글 웹로봇이다. 로봇은 전체사이트를 퍼가기 때문에 일반인의 접속보다 로드를 훨씬 많이 주지만 검색엔진결과에 나타날지 안 나타날지를 좌우하기 때문에 마음대로 막기는 곤란하다. 검색엔진을 통한 방문자를 줄이는 결과가 되기 때문이다. 무명의 검색엔진이나 로봇표시가 전혀 없는 IP 들을 막는 것을 고려하는 것은 부작용이 적을 것이다.

check_log_ip.py
===================================================
#!/usr/local/bin/python
# -*- coding: ISO-8859-1 -*-
#
# Apache access_log의 마지막 5000라인을 읽어서
# 페이지뷰가 많은 IP주소 10개를 표시하고
# 페이지뷰가 150건 이상인 경우 log의 앞 3라인을 보여주고
# nslookup 결과도 보여준다. (내부 IP제외)
#
#
import glob, os
from urllib import *
from time import *

# dictionary를 값을 기준으로 소트한 후 출력하는 함수
def print_dic(dicx):
entries = []
for word, cnt in dicx.items():
if cnt > 2:
entries.append((cnt, word))
entries.sort()
entries.reverse()
for cnt, word in entries[:10]: # Top n만 출력
print '\n', word, ':', cnt
# 203.233.으로 시작하면 내부 IP로 보고 제외
if word[:8] != '203.233.' and cnt > 150:
os.system('grep "%s" accesstail5000 | head -10' %(word))
os.system('nslookup %s' % (word))

def process_file(file):
global total_cnt, dic
f = open(file, 'r')
for line in f:
total_cnt += 1
if total_cnt == 1:
continue
pos = line.find(' - ')
ip = line[:pos]
if len(ip) == 0:
continue
if dic.has_key(ip):
dic[ip] += 1
else:
dic[ip] = 1
print '\nIP : Pageviews\n'
print_dic(dic)
f.close()
a = time()
total_cnt = 0
dic = {}

print '\nDate:', strftime('%Y/%m/%d %H:%M:%S', localtime(time()))
suff = strftime('%Y%m%d', localtime(time()))
os.system('tail -5000 access_log.%s > accesstail5000' % ( suff ) )

process_file('accesstail5000')

print 'total count = %d' % (total_cnt)
b = time()
print 'total execution time : %f seconds' % (b-a)
========================================================
이 프로그램을 access 로그파일이 있는 디렉토리에 두고 실행하면 된다.
로그파일 네이밍에 따라서 로그파일명을 맞춰주어야 한다.
위의 예는 access_log.20060921 과 같이 로그를 쌓는 경우이다.

댓글목록

등록된 댓글이 없습니다.

47 (1/1P)

Search

Copyright © Cmd 명령어 3.149.213.209