我正在查找一個讀取日誌並用主機名替換IP地址的bash腳本。有沒有人有任何想法如何做到這一點?用日誌中的主機名替換IP
回答
以下腳本應該可以工作。您可以使用它像這樣:
保存到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"
這個腳本工作得很好。事後看來,我應該更好地描述我的日誌格式。 (它來自Netgear路由器)我做了一些微小的修改,這個功能就像一個魅力,謝謝! – zkxs 2012-03-20 20:44:17
談到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。
的解決是可以做到這樣的:
IP = 72.30.38.140
主機名= nslookup $ip | grep name
主機名= $ {主機名#*名稱=}
主機名= $ {。主機%}
這種方式IP不必在/ etc/hosts中。
腳本本身取決於您的日誌的樣子。你能舉一個例子嗎?
使用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
這是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"
我將此添加到我的.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%肯定它會在所有情況下工作...希望這有助於=)
- 1. 用IP替換我的本地主機
- 2. sed:替換主機文件中的ip,使用主機名作爲模式
- 3. Perl:爲每個設備分別記錄日誌並將日誌文件名中的IP轉換爲主機名
- 4. 將IP轉換爲主機名
- 5. python腳本的替代主機/ IP
- 6. 的monit:使用主機名在日誌文件路徑
- 7. Ip日誌file_get_content()
- 8. 將IP從一個文件轉換爲主機名用Perl
- 9. fail2ban白名單動態ip更新主機名的關聯ip
- 10. 如何用網絡驅動器路徑中的IP地址替換主機名使用批量命令
- 11. Logstash如何找出日誌來自的主機的名稱?
- 12. 用於主機名的Python套接字替換變量
- 13. 替換主機在htaccess
- 14. 來自別名域(cPanel共享主機)的日誌重定向
- 15. IP如何解析主機名?
- 16. 從IP地址獲取主機名
- 17. android:將主機名映射到ip
- 18. gitlab改變主機名IP/gitlab
- 19. For循環遍歷IP和主機名
- 20. 將主機名解析爲IP地址
- 21. 如何從IP獲取主機名?
- 22. web.config中的Azure DNS主機名或IP引用?
- 23. 將主機名添加到日誌文件
- 24. 的logback:使用Java主類名作爲日誌文件名
- 25. 與用戶/ ip的Django日誌記錄
- 26. 錯誤日誌中的IP阻塞PHP
- 27. LogParser - 事件日誌數據值替換
- 28. 我想檢查IP地址到相應的主機名和主機名IP範圍
- 29. 共享Ip主機
- 30. 如何在Eureka中使用IP地址而不是主機名?
我嘗試發佈堆棧溢出,我等待答案。 – Kaz 2012-03-20 04:19:24
IPv4或IPv6或兩者兼而有之?你在/ etc/hosts中有主機名嗎? – 2012-03-20 05:46:25