2012-03-20 35 views
4

我正在查找一個讀取日誌並用主機名替換IP地址的bash腳本。有沒有人有任何想法如何做到這一點?用日誌中的主機名替換IP

+5

我嘗試發佈堆棧溢出,我等待答案。 – Kaz 2012-03-20 04:19:24

+0

IPv4或IPv6或兩者兼而有之?你在/ etc/hosts中有主機名嗎? – 2012-03-20 05:46:25

回答

4

以下腳本應該可以工作。您可以使用它像這樣:

保存到ip_to_hostname.sh然後:

./ip_to_hostname.sh your_logfile> resolved_ip

#!/bin/bash 

logFile=$1 

while read line 
do 
     for word in $line 
     do 
       # if word is ip address change to hostname 
       if [[ $word =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] 
       then 
         # check if ip address is correct 
         OIFS=$IFS 
         IFS="." 
         ip=($word) 
         IFS=$OIFS 
         if [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]] 
         then 
           echo -n `host $word | cut -d' ' -f 5` 
           echo -n " " 
         else 
           echo -n "$word" 
           echo -n " " 
         fi 
       # else print word 
       else 
         echo -n $word 
         echo -n " " 
       fi 
     done 
     # new line 
     echo 
done < "$logFile" 
+0

這個腳本工作得很好。事後看來,我應該更好地描述我的日誌格式。 (它來自Netgear路由器)我做了一些微小的修改,這個功能就像一個魅力,謝謝! – zkxs 2012-03-20 20:44:17

1

談到IPv4的:你可以生成你的主機沉渣 - 命令的列表文件:

sed -rn 's/^(([0-9]{1,3}\.){3}([0-9]{1,3}))[ \t]([^ \t]+)[ \t].*/s#\1#\4#/p' /etc/hosts > hosts.sed 

然後把它在你的日誌文件:

sed -f hosts.sed LOGFILE 

當然你hostsfilenames必須列在hostfile中。

另一種反向方法是使用logresolve

從手冊頁:

NAME 
    logresolve - Resolve IP-addresses to hostnames in Apache log files 

SYNOPSIS 
    logresolve [ -s filename ] [ -c ] <access_log> access_log.new 

SUMMARY 
    logresolve is a post-processing program to resolve IP-addresses in Apache's access logfiles. To minimize 
    impact on your nameserver, logresolve has its very own internal hash-table cache. This means that each 
    IP number will only be looked up the first time it is found in the log file. 

    Takes an Apache log file on standard input. The IP addresses must be the first thing on each line and 
    must be separated from the remainder of the line by a space. 

所以,你可以使用正則表達式的提取所有IP地址,把他們2次進入一個新的文件,一旦進了第一列,並與分析(logresolve)轉換。然後用這個表格來生成如上所述的sedfile。

1

的解決是可以做到這樣的:

IP = 72.30.38.140
主機名= nslookup $ip | grep name
主機名= $ {主機名#*名稱=}
主機名= $ {。主機%}

這種方式IP不必在/ etc/hosts中。

腳本本身取決於您的日誌的樣子。你能舉一個例子嗎?

+1

使用sed替代時請注意常見錯誤。想想如果你有IP 72.30.38.140和72.30.38.14會發生什麼。當您替換72.30.38.14時,它將在72.30.38.140中替換,因爲它包含72.30。38.14。您必須更換IP型隔離器。 – 2012-03-20 07:52:49

0

這是wisent的劇本的修改版本我最終使用:

#!/bin/bash 

logFile=$1 

while read line 
do 
     for word in $line 
     do 
       # if word is ip address change to hostname 
       if [[ $word =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\:[0-9]{1,5}$ ]] 
       then 
         port=$(echo "$word" | sed -e "s/.*://") 
         word=$(echo "$word" | sed -e "s/:.*//") 
         OIFS=$IFS 
         IFS="." 
         ip=($word) 
         IFS=$OIFS 
         # check if ip address is correct and not 192.168.* 
         if [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 && ${ip[2]} -le 255 && ${ip[3]} -le 255 && ${ip[0]}${ip[1]} -ne 192168 ]] 
         then 
           host=$(host $word | cut -d' ' -f 5) 
           if [[ $host =~ ^[0-9]{1,3}\(.*\)$ ]] # check for resolver errors 
           then 
             # if the resolver failed 
             echo -n "$word" 
             echo -n ":$port" 
             echo -n " " 
           else 
             # if the resolver worked 
             host=$(echo "$host'" | sed -e "s/\.'//" | sed ':a;N;$!ba;s/.*\n//g') # clean up cut's output 
             echo -n "$host" 
             echo -n ":$port" 
             echo -n " " 
           fi 
         else 
           # if the ip address isn't correct 
           echo -n "$word" 
           echo -n ":$port" 
           echo -n " " 
         fi 
       # else print word 
       else 
         echo -n $word 
         echo -n " " 
       fi 
     done 
     # new line 
     echo 
done < "$logFile" 
0

我將此添加到我的.bashrc前一段時間...

function resolve-hostname-from-ip() 
{ 
    if [ ! $1 ] 
    then 
     echo -e "${red}Please provide an ip address...${no_color}" 
     return 1 
    fi 
    echo "" | traceroute $1|grep " 1 "|cut -d ' ' -f4|cut -d '.' -f1 
} 

我有預定義的終端顏色,所以你可以省略這些,如果你喜歡。 = D

[[email protected] ~ 08:50 AM] $ resolve-hostname-from-ip 111.22.33.444 
someotherhostname 

我已經在RHEL和SUSE上成功測試過了。我還沒有在我的域外測試它,所以我不是100%肯定它會在所有情況下工作...希望這有助於=)