search
HomeOperation and MaintenanceLinux Operation and MaintenanceShell is an efficient command for analyzing log files, super easy to use!


自己的小网站跑在阿里云的ECS上面,偶尔也去分析分析自己网站服务器日志,看看网站的访问量。看看有没有黑客搞破坏!于是收集,整理一些服务器日志分析命令,大家可以试试!

Shell is an efficient command for analyzing log files, super easy to use!

1、查看有多少个IP访问:

awk '{print $1}' log_file|sort|uniq|wc -l

2、查看某一个页面被访问的次数:

grep "/index.php" log_file | wc -l

3、查看每一个IP访问了多少个页面:

awk '{++S[$1]} END {for (a in S) print a,S[a]}' log_file > log.txt  
  
sort -n -t ' ' -k 2 log.txt  # 配合sort进一步排序

4、将每个IP访问的页面数进行从小到大排序:

awk '{++S[$1]} END {for (a in S) print S[a],a}' log_file | sort -n

5、查看某一个IP访问了哪些页面:

grep ^111.111.111.111 log_file| awk '{print $1,$7}'

6、去掉搜索引擎统计的页面:

awk '{print $12,$1}' log_file | grep ^\"Mozilla | awk '{print $2}' |sort | uniq | wc -l

7、查看2015年8月16日14时这一个小时内有多少IP访问:

awk '{print $4,$1}' log_file | grep 16/Aug/2015:14 | awk '{print $2}'| sort | uniq | wc -l

8、查看访问前十个ip地址

awk '{print $1}' |sort|uniq -c|sort -nr |head -10 access_log

uniq -c 相当于分组统计并把统计数放在最前面

cat access.log|awk '{print $1}'|sort|uniq -c|sort -nr|head -10  
  
cat access.log|awk '{counts[$(11)]+=1}; END {for(url in counts) print counts[url], url}

9、访问次数最多的10个文件或页面

cat log_file|awk '{print $11}'|sort|uniq -c|sort -nr | head -10

<span style="outline: 0px;font-size: 17px;">**访问量最大的前20个ip**</span>

cat log_file|awk &#39;{print $11}&#39;|sort|uniq -c|sort -nr|head -20  
  
awk &#39;{print $1}&#39; log_file |sort -n -r |uniq -c | sort -n -r | head -20

10、通过子域名访问次数,依据referer来计算,稍有不准

cat access.log | awk &#39;{print $11}&#39; | sed -e &#39; s/http:\/\///&#39; -e &#39; s/\/.*//&#39; | sort | uniq -c | sort -rn | head -20

11、列出传输大小最大的几个文件

cat www.access.log |awk &#39;($7~/\.php/){print $10 " " $1 " " $4 " " $7}&#39;|sort -nr|head -100

12、列出输出大于200000byte(约200kb)的页面以及对应页面发生次数

cat www.access.log |awk &#39;($10 > 200000 && $7~/\.php/){print $7}&#39;|sort -n|uniq -c|sort -nr|head -100

13、如果日志最后一列记录的是页面文件传输时间,则有列出到客户端最耗时的页面

cat www.access.log |awk &#39;($7~/\.php/){print $NF " " $1 " " $4 " " $7}&#39;|sort -nr|head -100

14、列出最最耗时的页面(超过60秒的)的以及对应页面发生次数

cat www.access.log |awk &#39;($NF > 60 && $7~/\.php/){print $7}&#39;|sort -n|uniq -c|sort -nr|head -100

15、列出传输时间超过 30 秒的文件

cat www.access.log |awk &#39;($NF > 30){print $7}&#39;|sort -n|uniq -c|sort -nr|head -20

16、列出当前服务器每一进程运行的数量,倒序排列

ps -ef | awk -F &#39; &#39; &#39;{print $8 " " $9}&#39; |sort | uniq -c |sort -nr |head -20

17、查看apache当前并发访问数

对比httpd.conf中MaxClients的数字差距多少

netstat -an | grep ESTABLISHED | wc -l

18、可以使用如下参数查看数据

ps -ef|grep httpd|wc -l  
1388

统计httpd进程数,连个请求会启动一个进程,使用于Apache服务器。
表示Apache能够处理1388个并发请求,这个值Apache可根据负载情况自动调整

netstat -nat|grep -i "80"|wc -l  
4341

netstat -an会打印系统当前网络链接状态,而grep -i "80"是用来提取与80端口有关的连接的,wc -l进行连接数统计。
最终返回的数字就是当前所有80端口的请求总数

netstat -na|grep ESTABLISHED|wc -l  
376

netstat -an会打印系统当前网络链接状态,而grep ESTABLISHED 提取出已建立连接的信息。然后wc -l统计
最终返回的数字就是当前所有80端口的已建立连接的总数。

netstat -nat||grep ESTABLISHED|wc

可查看所有建立连接的详细记录

19、输出每个ip的连接数,以及总的各个状态的连接数

netstat -n | awk &#39;/^tcp/ {n=split($(NF-1),array,":");if(n<=2)++S[array[(1)]];else++S[array[(4)]];++s[$NF];++N} END {for(a in S){printf("%-20s %s\n", a, S[a]);++I}printf("%-20s %s\n","TOTAL_IP",I);for(a in s) printf("%-20s %s\n",a, s[a]);printf("%-20s %s\n","TOTAL_LINK",N);}&#39;

20、其他的收集

分析日志文件下 2012-05-04 访问页面最高 的前20个 URL 并排序

cat access.log |grep &#39;04/May/2012&#39;| awk &#39;{print $11}&#39;|sort|uniq -c|sort -nr|head -20

查询受访问页面的URL地址中 含有 www.abc.com 网址的 IP 地址

cat access_log | awk &#39;($11~/\www.abc.com/){print $1}&#39;|sort|uniq -c|sort -nr

获取访问最高的10个IP地址 同时也可以按时间来查询。另外,搜索公众号Linux就该这样学后台回复“Linux”,获取一份惊喜礼包。

cat linewow-access.log|awk &#39;{print $1}&#39;|sort|uniq -c|sort -nr|head -10

时间段查询日志时间段的情况

cat log_file | egrep &#39;15/Aug/2015|16/Aug/2015&#39; |awk &#39;{print $1}&#39;|sort|uniq -c|sort -nr|head -10

分析2015/8/15 到 2015/8/16 访问"/index.php?g=Member&m=Public&a=sendValidCode"的IP倒序排列

cat log_file | egrep &#39;15/Aug/2015|16/Aug/2015&#39; | awk &#39;{if($7 == "/index.php?g=Member&m=Public&a=sendValidCode") print $1,$7}&#39;|sort|uniq -c|sort -nr
(7里面包含.php的就输出,本句的意思是最耗时的一百个PHP页面
cat log_file |awk &#39;($7~/\.php/){print $NF " " $1 " " $4 " " $7}&#39;|sort -nr|head -100
列出最最耗时的页面(超过60秒的)的以及对应页面发生次数
cat access.log |awk &#39;($NF > 60 && $7~/\.php/){print $7}&#39;|sort -n|uniq -c|sort -nr|head -100
统计网站流量(G)
cat access.log |awk &#39;{sum+=$10} END {print sum/1024/1024/1024}&#39;
统计404的连接
awk &#39;($9 ~/404/)&#39; access.log | awk &#39;{print $9,$7}&#39; | sort
统计http status
cat access.log |awk &#39;{counts[$(9)]+=1}; END {for(code in counts) print code, counts[code]}&#39;   
cat access.log |awk &#39;{print $9}&#39;|sort|uniq -c|sort -rn
每秒并发
watch "awk &#39;{if($9~/200|30|404/)COUNT[$4]++}END{for( a in COUNT) print a,COUNT[a]}&#39; log_file|sort -k 2 -nr|head -n10"
带宽统计
cat apache.log |awk &#39;{if($7~/GET/) count++}END{print "client_request="count}&#39;   
cat apache.log |awk &#39;{BYTE+=$11}END{print "client_kbyte_out="BYTE/1024"KB"}&#39;
找出某天访问次数最多的10个IP
cat /tmp/access.log | grep "20/Mar/2011" |awk &#39;{print $3}&#39;|sort |uniq -c|sort -nr|head
当天ip连接数最高的ip都在干些什么
cat access.log | grep "10.0.21.17" | awk &#39;{print $8}&#39; | sort | uniq -c | sort -nr | head -n 10
小时单位里ip连接数最多的10个时段
awk -vFS="[:]" &#39;{gsub("-.*","",$1);num[$2" "$1]++}END{for(i in num)print i,num[i]}&#39; log_file | sort -n -k 3 -r | head -10
找出访问次数最多的几个分钟
awk &#39;{print $1}&#39; access.log | grep "20/Mar/2011" |cut -c 14-18|sort|uniq -c|sort -nr|head
取5分钟日志
if [ $DATE_MINUTE != $DATE_END_MINUTE ] ;then   
#则判断开始时间戳与结束时间戳是否相等
START_LINE=sed -n "/$DATE_MINUTE/=" $APACHE_LOG|head -n1 
#如果不相等,则取出开始时间戳的行号,与结束时间戳的行号
查看tcp的链接状态*
netstat -nat |awk &#39;{print $6}&#39;|sort|uniq -c|sort -rn   
     
netstat -n | awk &#39;/^tcp/ {++S[$NF]};END {for(a in S) print a, S[a]}&#39;   
  
netstat -n | awk &#39;/^tcp/ {++state[$NF]}; END {for(key in state) print key,"\t",state[key]}&#39;   
     
netstat -n | awk &#39;/^tcp/ {++arr[$NF]};END {for(k in arr) print k,"\t",arr[k]}&#39;   
     
netstat -n |awk &#39;/^tcp/ {print $NF}&#39;|sort|uniq -c|sort -rn   
     
netstat -ant | awk &#39;{print $NF}&#39; | grep -v &#39;[a-z]&#39; | sort | uniq -cnetstat -ant|awk &#39;/ip:80/{split($5,ip,":");++S[ip[1]]}END{for (a in S) print S[a],a}&#39; |sort -n   
     
netstat -ant|awk &#39;/:80/{split($5,ip,":");++S[ip[1]]}END{for (a in S) print S[a],a}&#39; |sort -rn|head -n 10   
  
awk &#39;BEGIN{printf ("http_code\tcount_num\n")}{COUNT[$10]++}END{for (a in COUNT) printf a"\t\t"COUNT[a]"\n"}&#39;
查找请求数前20个IP(常用于查找攻来源):
netstat -anlp|grep 80|grep tcp|awk &#39;{print $5}&#39;|awk -F: &#39;{print $1}&#39;|sort|uniq -c|sort -nr|head -n20   
  
netstat -ant |awk &#39;/:80/{split($5,ip,":");++A[ip[1]]}END{for(i in A) print A[i],i}&#39; |sort -rn|head -n20
用tcpdump嗅探80端口的访问看看谁最高
牛逼啊!接私活必备的 N 个开源项目!赶快收藏
tcpdump -i eth0 -tnn dst port 80 -c 1000 | awk -F"." &#39;{print $1"."$2"."$3"."$4}&#39; | sort | uniq -c | sort -nr |head -20
查找较多time_wait连接
netstat -n|grep TIME_WAIT|awk &#39;{print $5}&#39;|sort|uniq -c|sort -rn|head -n20
找查较多的SYN连接
netstat -an | grep SYN | awk &#39;{print $5}&#39; | awk -F: &#39;{print $1}&#39; | sort | uniq -c | sort -nr | more
根据端口列进程
netstat -ntlp | grep 80 | awk &#39;{print $7}&#39; | cut -d/ -f1
查看了连接数和当前的连接数
netstat -ant | grep $ip:80 | wc -l   
netstat -ant | grep $ip:80 | grep EST | wc -l
查看IP访问次数
netstat -nat|grep ":80"|awk &#39;{print $5}&#39; |awk -F: &#39;{print $1}&#39; | sort| uniq -c|sort -n
Linux命令分析当前的链接状况
netstat -n | awk &#39;/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}&#39;  
  
watch "netstat -n | awk &#39;/^tcp/ {++S[\$NF]} END {for(a in S) print a, S[a]}&#39;"   
# 通过watch可以一直监控
LAST_ACK 5   #关闭一个TCP连接需要从两个方向上分别进行关闭,双方都是通过发送FIN来表示单方向数据的关闭,当通信双方发送了最后一个FIN的时候,发送方此时处于LAST_ACK状态,当发送方收到对方的确认(Fin的Ack确认)后才真正关闭整个TCP连接;  
SYN_RECV 30       # 表示正在等待处理的请求数;  
ESTABLISHED 1597  # 表示正常数据传输状态;   
FIN_WAIT1 51      # 表示server端主动要求关闭tcp连接;   
FIN_WAIT2 504     # 表示客户端中断连接;   
TIME_WAIT 1057    # 表示处理完毕,等待超时结束的请求数;
<br/>
Shell is an efficient command for analyzing log files, super easy to use!
为了跟上AI时代我干了一件事儿,我创建了一个知识星球社群:ChartGPT与副业。想带着大家一起探索ChatGPT和新的AI时代。有很多小伙伴搞不定ChatGPT账号,于是我们决定,凡是这三天之内加入ChatPGT的小伙伴,我们直接送一个正常可用的永久ChatGPT独立账户。
简单说下这个星球能给大家提供什么:
星球分享:
1、不断分享如何使用ChatGPT来完成各种任务,让你更高效地使用ChatGPT,以及副业思考、变现思路、创业案例、落地案例分享。2、分享ChatGPT的使用方法、最新资讯、商业价值。3、探讨未来关于ChatGPT的机遇,共同成长。4、帮助大家解决ChatGPT遇到的问题。5、不定期邀请大咖进行分享。6、提供一整年的售后服务,一起搞副业
星球福利:1、加入星球4天后,就送ChatGPT独立账号。2、邀请你加入ChatGPT会员交流群。3、赠送一份完整的ChatGPT手册和66个ChatGPT副业赚钱手册。
其它福利还在筹划中... 不过,我给你大家保证,加入星球后,收获的价值会远远大于今天加入的门票费用 !
本星球第一期原价399,目前属于试运营,早鸟价139,每超过50人涨价10元,星球马上要来一波大的涨价,如果你还在犹豫,可能最后就要以更高价格加入了。。
早就是优势。建议大家尽早以便宜的价格加入!

  声明:本文部分素材转载自互联网,如有侵权立即删除 。


往期精彩

<br/>

## Friends who like this article are welcome to comment. Click the picture and follow the subscription number Linux Chinese Community


## to watch More exciting content

The above is the detailed content of Shell is an efficient command for analyzing log files, super easy to use!. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:Linux中文社区. If there is any infringement, please contact admin@php.cn delete
Linux's Essential Components: Explained for BeginnersLinux's Essential Components: Explained for BeginnersApr 17, 2025 am 12:08 AM

The core components of Linux include the kernel, file system, shell and common tools. 1. The kernel manages hardware resources and provides basic services. 2. The file system organizes and stores data. 3. Shell is the interface for users to interact with the system. 4. Common tools help complete daily tasks.

Linux: A Look at Its Fundamental StructureLinux: A Look at Its Fundamental StructureApr 16, 2025 am 12:01 AM

The basic structure of Linux includes the kernel, file system, and shell. 1) Kernel management hardware resources and use uname-r to view the version. 2) The EXT4 file system supports large files and logs and is created using mkfs.ext4. 3) Shell provides command line interaction such as Bash, and lists files using ls-l.

Linux Operations: System Administration and MaintenanceLinux Operations: System Administration and MaintenanceApr 15, 2025 am 12:10 AM

The key steps in Linux system management and maintenance include: 1) Master the basic knowledge, such as file system structure and user management; 2) Carry out system monitoring and resource management, use top, htop and other tools; 3) Use system logs to troubleshoot, use journalctl and other tools; 4) Write automated scripts and task scheduling, use cron tools; 5) implement security management and protection, configure firewalls through iptables; 6) Carry out performance optimization and best practices, adjust kernel parameters and develop good habits.

Understanding Linux's Maintenance Mode: The EssentialsUnderstanding Linux's Maintenance Mode: The EssentialsApr 14, 2025 am 12:04 AM

Linux maintenance mode is entered by adding init=/bin/bash or single parameters at startup. 1. Enter maintenance mode: Edit the GRUB menu and add startup parameters. 2. Remount the file system to read and write mode: mount-oremount,rw/. 3. Repair the file system: Use the fsck command, such as fsck/dev/sda1. 4. Back up the data and operate with caution to avoid data loss.

How Debian improves Hadoop data processing speedHow Debian improves Hadoop data processing speedApr 13, 2025 am 11:54 AM

This article discusses how to improve Hadoop data processing efficiency on Debian systems. Optimization strategies cover hardware upgrades, operating system parameter adjustments, Hadoop configuration modifications, and the use of efficient algorithms and tools. 1. Hardware resource strengthening ensures that all nodes have consistent hardware configurations, especially paying attention to CPU, memory and network equipment performance. Choosing high-performance hardware components is essential to improve overall processing speed. 2. Operating system tunes file descriptors and network connections: Modify the /etc/security/limits.conf file to increase the upper limit of file descriptors and network connections allowed to be opened at the same time by the system. JVM parameter adjustment: Adjust in hadoop-env.sh file

How to learn Debian syslogHow to learn Debian syslogApr 13, 2025 am 11:51 AM

This guide will guide you to learn how to use Syslog in Debian systems. Syslog is a key service in Linux systems for logging system and application log messages. It helps administrators monitor and analyze system activity to quickly identify and resolve problems. 1. Basic knowledge of Syslog The core functions of Syslog include: centrally collecting and managing log messages; supporting multiple log output formats and target locations (such as files or networks); providing real-time log viewing and filtering functions. 2. Install and configure Syslog (using Rsyslog) The Debian system uses Rsyslog by default. You can install it with the following command: sudoaptupdatesud

How to choose Hadoop version in DebianHow to choose Hadoop version in DebianApr 13, 2025 am 11:48 AM

When choosing a Hadoop version suitable for Debian system, the following key factors need to be considered: 1. Stability and long-term support: For users who pursue stability and security, it is recommended to choose a Debian stable version, such as Debian11 (Bullseye). This version has been fully tested and has a support cycle of up to five years, which can ensure the stable operation of the system. 2. Package update speed: If you need to use the latest Hadoop features and features, you can consider Debian's unstable version (Sid). However, it should be noted that unstable versions may have compatibility issues and stability risks. 3. Community support and resources: Debian has huge community support, which can provide rich documentation and

TigerVNC share file method on DebianTigerVNC share file method on DebianApr 13, 2025 am 11:45 AM

This article describes how to use TigerVNC to share files on Debian systems. You need to install the TigerVNC server first and then configure it. 1. Install the TigerVNC server and open the terminal. Update the software package list: sudoaptupdate to install TigerVNC server: sudoaptinstalltigervnc-standalone-servertigervnc-common 2. Configure TigerVNC server to set VNC server password: vncpasswd Start VNC server: vncserver:1-localhostno

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool