-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolve_ips_in_log
executable file
·70 lines (63 loc) · 1.94 KB
/
resolve_ips_in_log
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/bash
#By Josef Meile <jmeile@hotmail.com> @ June, 2019
#This script will process a log file and resolve all the ip addresses in the
#specified column, then it will return all lines with the ips and the resolved
#hosts
#You can use it as follows:
#./resolve_ips_in_log ip_column log_file > new_log_file
#Where:
#ip_column: is the column with the ips to resolve
#log_file: is the log file with the data to process
#new_low_file is the file to store the processed data
#This script is based on the code posted here:
#* Replace IPs with Hostnames in a log
# Link: https://stackoverflow.com/questions/9781063/replace-ips-with-hostnames-in-a-log
# Answer by: wisent
#Why not using logresolve or the apache HostnameLookups directive?
#* logresolve will replace all the ips by its resolved hostnames, so the original
# data is lost. I need both: the ips and the hosts
#* HostnameLookups: with this directive you could use a custom LogFormat with
# %h (Remote hostname) and %a (Client IP address of the request); however,
# turning this on may slow down your webserver because dns lookups on the fly
# will require some extra time
function replaceIp {
ipCol=$1
logFile=$2
while read line
do
declare -i colIndex=1
for word in $line
do
#Prints the original column
echo -n $word
echo -ne "\t"
#if we got to the column of interest
if [ "$colIndex" -eq "$ipCol" ]
then
# if word is an ip address change it 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`
else
echo -n "invalid_ip"
fi
# else invalid ip
else
echo -n "invalid_ip"
fi
echo -ne "\t"
fi
colIndex+=1
done
# new line
echo
done < "$logFile"
}
replaceIp $1 $2