Home > Article > Operation and Maintenance > How to write a shell script to count appche site IP visits in Linux
It is often necessary to count apache site visits based on IP address, the most basic script.
Arrange in descending order according to IP visits:
Copy the code The code is as follows:
# !/bin/bash
#script_name: access_count
acc_log=/usr/local/apache2/logs/access_log
/bin/awk '{print $1}' $acc_log | sort | uniq -c | sort -nr
Execution effect:
Copy code The code is as follows:
[root@zabbix ~]# sh access_count
94989 192.168.100.34
38863 192.168.200.92
23658 192.168.1.71
16720 192.168.100.80
13688 192.168.200.34
1618 192.168.100.104
1251 192.168.1.202
1195 192.168.100.30
1058 192.168.1.203
934 192.168.1.208
792 127.0.0.1
773 192.168.5.126
189 192.168.68
#script_name:access_count
acc_log=/usr/local/apache2/logs/access_log
/bin/ awk '{print $1}' $acc_log | sort | uniq -c | sort -nr | head -n 3
94989 192.168.100.34
38863 192.168.200.92
23658 192.168.1.71
#script_name:error_count
err_log=/usr/local/apache2/logs/error_log
cat $err_log | grep -e "^\[" | awk '{print $6}' | sort | uniq -c |sort -nr
701 [core:notice]
30 [mpm_event:notice]
12 [core:warn]
1 [:error]
The above is the detailed content of How to write a shell script to count appche site IP visits in Linux. For more information, please follow other related articles on the PHP Chinese website!