Wednesday, 19 August 2015

Access Log Manipulation

#!/bin/bash

tail -n1 /var/log/httpd/access_log | awk '{print $4}' | while read line
do
in=${line}
#echo "Input: $in"
rfc_form="${in:1:2}-${in:4:3}-${in:8:4} UTC ${in:13:2}:${in:16:2}:${in:19:2}"
#echo "Converted: $rfc_form"
epoch_time=$(date -d "$rfc_form" +%s)
echo "Only Seconds: $epoch_time"
#date -d "$rfc_form"
date -d @$epoch_time
done

One more technique:

echo "18-06:25:34 " | awk -F: '{ print ($1*24*3600) + ($2*3600) + ($3*60) + $4 }'

========================================================================

Take this log file :

[
    [
        "52.4.30.109"
    ], 
    [
        "54.210.143.237"
    ], 
    [], 
    [], 
    [
        "54.210.164.230"
    ], 
    [], 
    [], 
    [
        "54.210.247.55"
    ], 
  
    [
        "54.210.162.121"
    ], 
    [
        "54.210.145.45"
    ], 
    [], 
    [
        "107.23.2.112"
    ], 
    [], 
    [], 
    [
        "52.7.146.177"
    ], 
    [
        "54.210.222.151"
    ], 
    [
        "54.210.170.90"
    ], 
    [], 
    [
        "54.210.163.107"
    ], 
    [
        "54.210.171.244"
    ], 
    [
        "52.7.33.54"
    ], 
    [
        "54.210.192.121"
    ], 
    []
]


Apply this :

cat new.txt | awk '{$1=$1}{ print }' | sed 's/[][]//g;s/,//g;s/"//g' | sed '/^$/d'

Expect Magic !!

Full Script:

#!/bin/bash

rm -fr new*
rm -fr fresh*

aws ec2 --region eu-central-1 describe-instances --query 'Reservations[*].Instances[*].PublicIpAddress' >> neweu.txt
aws ec2 --region us-east-1 describe-instances --query 'Reservations[*].Instances[*].PublicIpAddress' >> newusa.txt

cat neweu.txt | awk '{$1=$1}{ print }' | sed 's/[][]//g;s/,//g;s/"//g' | sed '/^$/d' >> fresheu.txt
cat newusa.txt | awk '{$1=$1}{ print }' | sed 's/[][]//g;s/,//g;s/"//g' | sed '/^$/d' >> freshusa.txt

counteu=$(cat fresheu.txt | wc -l)
countusa=$(cat freshusa.txt | wc -l)

echo "EU count is $counteu"
echo "USA count is $countusa"

rm -fr new*
rm -fr fresh*

OR do this simply:

aws ec2 --region eu-central-1 describe-instances --query 'Reservations[*].Instances[*].[PublicIpAddress, Tags[0].Value]' --output text | sort -k 2

aws ec2 --region us-west-1 describe-instances --query 'Reservations[*].Instances[*].[PublicIpAddress, Tags[0].Value]' --output text | sort -k 2

No comments:

Post a Comment