LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Security (https://www.linuxquestions.org/questions/linux-security-4/)
-   -   Parsing The Entries of a BIND log Query file (https://www.linuxquestions.org/questions/linux-security-4/parsing-the-entries-of-a-bind-log-query-file-4175416272/)

Balvinder87 07-12-2012 12:44 AM

Parsing The Entries of a BIND log Query file
 
I have installed BIND9 on my Debian system
we are in a network having local ips as 192.168.2.1 to 192.168.2.100
our bind log file sends its query details to a file called named_querylog
It has a sample entry like
12-Jul-2012 10:39:09.256 client 192.168.2.4#50151: query: www.godogle.com IN A + (192.168.2.4)
now we want to parse the local ip i.e. 192.168.2.4 into another file and this would be for each local ip between 192.168.2.1 to 192.168.2.100.
As we wan't to see the logs details of each client in the network for security purposes
Can any one help to generate the automated script for it script may be in bash or perl

Noway2 07-12-2012 04:43 AM

There are several ways to approach this, though all of them have some degree of similarity. The ultimate key to the problem lies in what are called regular expressions and this would be a good place for you to begin your research. Regular expressions are a pattern matching language and in your particular case you are looking to pattern match an IP address.

The approach that I would take would be to loop through the range of IP addresses, which runs from .1 to .100 (noting that you can use this number range as a loop variable. Then match the pattern of the IP address created based upon this variable to your file and PRINT the desired field (words or items separated by space). The first tool that comes to mind to do this is AWK (thought SED would probably work just as well, effectively acting as an advanced grep). See this page for some basic instructions on how to use AWK, along with some examples at the bottom: http://www.manpagez.com/man/1/awk/

As a starter example:
given tfile below, which would be your log file:
Code:

192.168.2.1 10000
192.168.2.1 20000
192.168.2.1 30000
192.168.2.2 10000
192.168.2.3 10000
192.168.2.5 10000
192.168.2.8 10000

Use a script like this
Code:

#! /bin/bash
for i in {1..10}
do
ipb="192.168.2."
ip=$ipb$i
echo "looking for $ip"
awk -v x=$ip '$1==x { print $2 }' tfile
done

And this gives:
Code:

looking for 192.168.2.1
10000
20000
30000
looking for 192.168.2.2
10000
looking for 192.168.2.3
10000
looking for 192.168.2.4
looking for 192.168.2.5
10000
looking for 192.168.2.6
looking for 192.168.2.7
looking for 192.168.2.8
10000
looking for 192.168.2.9
looking for 192.168.2.10

Note how it parses through the range of IP addresses, matches the lines with that IP address and prints out the desired field.

Balvinder87 07-12-2012 08:03 AM

thanks for reply


All times are GMT -5. The time now is 09:18 AM.