wie kann man am schnellsten und elegantesten über zwei Zeilen Parsen ?
such regex'es sind: "info:","A IN", "AAAA IN"
Angenommen im log steht:
Dec 17 22:00:36 server1 unbound: [93528:0] info: 10.10.10.5 http://www.google-analytics.com. A IN
Dec 17 22:00:36 server1 unbound: [93528:0] info: 10.10.10.5 http://www.google-analytics.com. AAAA IN
ich würde zuerst nach "info:" suchen und dann nach "A IN", und setzte eine Variable auf "true", wenn beides zustimmt.
Danach würde ich schauen ob die Variable auf "true" steht, und wieder nach "info: und dann nach "AAAA IN" suchen, wenn das auch zustimmt
gebe eine Meldung aus. Was auch wichtig ist, dass die Zeilen nacheinander folgen und
Dec 17 22:00:36 server1 unbound: [93528:0] info: 10.10.10.5 http://www.google-analytics.com. A IN
bla foo bar bla bla foo bar
Dec 17 22:00:36 server1 unbound: [93528:0] info: 10.10.10.5 http://www.google-analytics.com. AAAA IN
dann nicht eine Erfolgs Meldung ausgegeben wird, dass das gesuchte auf zwei Zeilen zutrifft. Da zwischen den zeilen "http://www.google-analytics.com" , bla foo bar bla bla foo bar vorkommt und somit nicht mehr zwei nacheinander folgende Zeilen sind.
Jemand eine Idee was die schnellste und eleganteste Lösung dafür wäre?
hier mein unfertiges shell script:
Code: Alles auswählen
#!/bin/sh
#Dec 17 22:00:36 sense unbound: [93528:0] info: 10.10.10.5 www.google-analytics.com. A IN
#Dec 17 22:00:36 sense unbound: [93528:0] info: 10.10.10.5 www.google-analytics.com. AAAA IN
searchString1="info:" #awk '{print "$7"}')
searchString2="A IN" #awk '{print $10 " " $11}')"
searchString3="AAAA IN" #awk '{print $10 " " $11}')"
searchString4="domainname" #awk '{print $9}')"
firstlinefound=false
secondlinefound=false
while read -r line
do
#set -x
#set the domainname variable
#searchString4=$(echo "$line" | awk '{print $9}')
#search for "info:"
if [ "$searchString1" == "$(echo "$line" | awk '{print $7}')" ] \
&& [ "$searchString2" == "$(echo "$line" | awk '{print $10 " " $11}')" ]; then
#set firstlinefound to true
firstlinefound=true
#save the domain before overwriting in the next read loop
#olddomain=$(echo $searchString4)
olddomain=$(echo "$line" | awk '{print $9}')
#go to read next line on first found
continue
fi
# #if match the second line with olddomain from the previous line go further
# if [ "$olddomain" == $(echo "$line" | awk '{print $9}') ] && [ "$firstlinefound" == true ] \
# && [ "$(echo "$line" | awk '{print $9}')" == $searchString4 ]; then
# #search for "info:"
# if [ "$searchString1" == "$(echo "$line" | awk '{print $7}')" ]; then
#search for "AAAA IN"
# if [ "$searchString3" == "$(echo "$line" | awk '{print $10 " " $11}')" ]; then
# secondlinefound=true
# fi
# fi
# fi
#reset olddomain and firstlinefound if not found on the second read line attempt
if [ ! "$olddomain" == $(echo "$line" | awk '{print $9}') ]; then
olddomain=""
firstlinefound=false
continue
fi
#print a match
if [ "$firstlinefound" == true ] && [ "$secondlinefound" == true ]; then
echo "match: $searchString4"
#reset variables
firstlinefound=false
secondlinefound=false
searchString4=""
fi
done < 234
Code: Alles auswählen
Dec 17 22:00:36 sense unbound: [93528:0] info: 10.10.10.5 www.gravatar.com. AAAA IN
Dec 17 22:00:36 sense unbound: [93528:0] info: 10.10.10.5 i.stack.imgur.com. A IN
Dec 17 22:00:36 sense unbound: [93528:0] info: 10.10.10.5 ssum-sec.casalemedia.com. AAAA IN
Dec 17 22:00:36 sense unbound: [93528:0] info: 10.10.10.5 static.adzerk.net. A IN
Dec 17 22:00:36 sense unbound: [93528:0] info: 10.10.10.5 static.adzerk.net. AAAA IN
Dec 17 22:00:36 sense unbound: [93528:0] info: 10.10.10.5 www.google-analytics.com. A IN
Dec 17 22:00:36 sense unbound: [93528:0] info: 10.10.10.5 www.google-analytics.com. AAAA IN
Dec 17 22:00:36 sense unbound: [93528:0] info: 10.10.10.5 edge.quantserve.com. A IN
123 433 324 468 34 5 2562 832 32 2 626 8376 222
Dec 17 22:00:36 sense unbound: [93528:0] info: 10.10.10.5 edge.quantserve.com. AAAA IN
Dec 17 22:00:36 sense unbound: [93528:0] info: 10.10.10.5 b.scorecardresearch.com. A IN
Dec 17 22:00:36 sense unbound: [93528:0] info: 10.10.10.5 b.scorecardresearch.com. AAAA IN
Dec 17 22:00:36 sense unbound: [93528:0] info: 192.168.123.64 http://www.gravatar.com. AAAA IN
Dec 17 22:00:36 sense unbound: [93528:0] info: 192.168.123.64 i.stack.imgur.com. A IN
Dec 17 22:00:36 sense unbound: [93528:0] info: 192.168.123.64 ssum-sec.casalemedia.com. AAAA IN
Dec 17 22:00:36 sense unbound: [93528:0] info: 192.168.123.64 static.adzerk.net. A IN <<<<<---------------------------MATCH
Dec 17 22:00:36 sense unbound: [93528:0] info: 192.168.123.64 static.adzerk.net. AAAA IN <<<<<---------------------------MATCH
Dec 17 22:00:36 sense unbound: [93528:0] info: 192.168.123.64 http://www.google-analytics.com. A IN <<<<<---------------------------MATCH
Dec 17 22:00:36 sense unbound: [93528:0] info: 192.168.123.64 http://www.google-analytics.com. AAAA IN <<<<<---------------------------MATCH
Dec 17 22:00:36 sense unbound: [93528:0] info: 192.168.123.64 edge.quantserve.com. A IN <<<<<---------------------------KEIN !!! MATCH
123 433 324 468 34 5 2562 832 32 2 626 8376 222
Dec 17 22:00:36 sense unbound: [93528:0] info: 192.168.123.64 edge.quantserve.com. AAAA IN <<<<<---------------------------KEIN !!! MATCH
Dec 17 22:00:36 sense unbound: [93528:0] info: 192.168.123.64 b.scorecardresearch.com. A IN <<<<<---------------------------MATCH
Dec 17 22:00:36 sense unbound: [93528:0] info: 192.168.123.64 b.scorecardresearch.com. AAAA IN <<<<<---------------------------MATCH
Danke