[웹서버] apache log 로 부터 IP 접속 기관명 구하기

1. Apache의 access log file에 적당한 condition을 두어 필요한 log만 채취합니다.

   이 파일을 access_log 라고 하겠습니다.

2. access_log로 부터 IP 부분만 추려옵니다.

   아래 첨부한 getip.sh 스크립트를 사용합니다.

   # ./getip.sh < access_log > access_ip

3. access_ip 로부터 기관명을 찾습니다.

  아래 첨부한 iptoname.c 를 컴파일하여 iptoname 프로그램을 만들어 사용합니다.

   # ./iptoname < access_ip > access_name

  iptoname.c 를 보면알 수 있듯이, krnic 에 한글로 등록된 도메인 기관명만을 찾아줍니다.

끝~

아래는 첨부 파일(?)

———————————–

getip.sh 스크립트 파일

===================================

#!/bin/sh

# getip.sh – get ip list from Apache access log # Clunix, glory@clunix.com, yjlee@clunix.com, 2000.6.4,2002.4.27

awk ‘{print $1}’ | sort -r | uniq

===================================

iptoname 프로그램의 소스파일

“gcc -o iptoname iptoname.c” 와 같이 compile하여 사용

===================================

/*

* iptoname.c – Get whois name for ip

* Clunix, glory@clunix.com, yjlee@clunix.com, 2000.6.4,2002.4.28.

* Modify glory’s original work to pipe style program by yjlee at 2002.4.28.

*/

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <errno.h>

#define WHOIS_SERVER  “whois.krnic.net”

int

main(int argc, char **argv)

{

  FILE *fp;

  char ip[256], cmd[256], line[256], *name;

  while (fgets(ip, 256, stdin)) {

    ip[strlen(ip)-1] = ‘\\0’;

    if (!strncmp(ip, “211.218.206”, 11) ||

        !strncmp(ip, “211.236.73”, 10))

      continue;

    sprintf(cmd, “fwhois -h %s %s”, WHOIS_SERVER, ip);

    fp = popen(cmd, “r”);

    name = ” unknown”;

    while (fgets(line, 256, fp)) {

      if (strstr(line, “기관명”)) {

        if (name = strchr(line, ‘:’))

          name++;

        else

          name = line;  /* this might not happen */

        name[strlen(name) – 1] = ‘\\0’;

        break;

      }

    }

    printf(“%s:%s\\n”, ip, name);

    pclose(fp);

  }

}

===================================

서진우

슈퍼컴퓨팅 전문 기업 클루닉스/ 상무(기술이사)/ 정보시스템감리사/ 시스존 블로그 운영자

You may also like...

페이스북/트위트/구글 계정으로 댓글 가능합니다.