Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache access log for the most common IP address bash script

so i am running a bash script on an apache log file i can sort the IP addresses and show the most common on but it shows it at the bottom of the page no the top how do i show it from highest to lowest

this is my script so far

cat access_log.txt | awk '{print $1}'| uniq -c |sort -n -k 1| tail

in my txt file i have

10.23.234.0
250.40.56.78
8.45.98.250
10.23.234.0
250.40.56.78
8.45.98.250
10.23.234.0
250.40.56.78
10.23.234.0
250.40.56.78
10.23.234.0 
10.23.234.0

the output is

2 8.45.98.250
4 250.40.56.78
6 10.23.234.0

i want the output to be

6 10.23.234.0
4 250.40.56.78
2 8.45.98.250

also want would be the best way to print out the DNS name next to it so example

66.249.73.234 - - [12/Fegb/2013:12:00:09 +1100] "GET /java/tut/tut.sgml-065.html HTTP/1.1" 200 752 "-" "Mozilla/6.0 (compatible; Googlebot/2.1; +http://www.google.com.html)"  

so it would be

66.249.73.234 - http://www.google.com.html

Thanks

like image 378
user3504020 Avatar asked Nov 02 '22 02:11

user3504020


1 Answers

You can use this:

awk '{a[$1]++} END {for (i in a) print a[i],i | "sort -rnk1"}' access_log.txt
5 10.23.234.0
4 250.40.56.78
2 8.45.98.250
1 10.23.234.0

or

awk '{a[$1]++} END {for (i in a) print a[i],i}' access_log.txt | sort -rnk1

r in sort is reverse


To get html data

awk '{split($0,a,"http|[)]");print $1" - http"a[2]}' file
66.249.73.234 - http://www.google.com/bot.html

Top print DNS only for top two records.

awk '{split($0,b,"http|[)]");a[$1" - http"b[2]]++} END {for (i in a) print a[i],i}' file | sort -rnk 1 | awk 'NR>2 {$0=$1FS$2} 1'
like image 78
Jotne Avatar answered Nov 11 '22 14:11

Jotne