【sed】用 sed 查看Linux上服务器某个时间段的日志
一、背景
在Linux上查找日志的时候,如果我想找出某个时间段的日志,比如查找今天早上8点到下午2点的日志。
用grep不太方便直接过滤出来,可以使用sed根据时间去查找:
sed -n '/开始时间日期/,/结束时间日期/p' access.log
通过证书首尾标识符打印域名证书:
echo | openssl s_client -showcerts -connect www.example.com:443 2> /dev/null | sed -n '/-----BEGIN CERTIFICATE-----/,/-----END CERTIFICATE-----/p'
image.png
二、应用
比如下面这段日志,前面的时间格式都是类似 2019-10-21 07:44:20
2019-10-24 21:33:31,678 [django.request:93] [base:get_response] [WARNING]- Not Found: /http:/123.125.114.144/
2019-10-24 21:33:31,679 [django.server:124] [basehttp:log_message] [WARNING]- "HEAD http://123.125.114.144/ HTTP/1.1" 404 1678
2019-10-24 22:14:04,121 [django.server:124] [basehttp:log_message] [INFO]- code 400, message Bad request version ('HTTP')
2019-10-24 22:14:04,122 [django.server:124] [basehttp:log_message] [WARNING]- "GET ../../mnt/custom/ProductDefinition HTTP" 400 -
2019-10-24 22:16:21,052 [django.server:124] [basehttp:log_message] [INFO]- "GET /api/login HTTP/1.1" 301 0
2019-10-24 22:16:21,123 [django.server:124] [basehttp:log_message] [INFO]- "GET /api/login/ HTTP/1.1" 200 3876
2019-10-24 22:16:21,192 [django.server:124] [basehttp:log_message] [INFO]- "GET /static/assets/img/main_bg.png HTTP/1.1" 200 2801
2019-10-24 22:16:21,196 [django.server:124] [basehttp:log_message] [INFO]- "GET /static/assets/iconfont/style.css HTTP/1.1" 200 1638
2019-10-24 22:16:21,229 [django.server:124] [basehttp:log_message] [INFO]- "GET /static/assets/img/bg.jpg HTTP/1.1" 200 135990
2019-10-24 22:16:21,307 [django.server:124] [basehttp:log_message] [INFO]- "GET /static/assets/iconfont/fonts/icomoon.ttf?u4m6fy HTTP/1.1" 200 6900
2019-10-24 22:16:23,525 [django.server:124] [basehttp:log_message] [INFO]- "POST /api/login/ HTTP/1.1" 302 0
2019-10-24 22:16:23,618 [django.server:124] [basehttp:log_message] [INFO]- "GET /api/index/ HTTP/1.1" 200 18447
2019-10-24 22:16:23,709 [django.server:124] [basehttp:log_message] [INFO]- "GET /static/assets/js/commons.js HTTP/1.1" 200 13209
2019-10-24 22:16:23,712 [django.server:124] [basehttp:log_message] [INFO]- "GET /static/assets/css/admin.css HTTP/1.1" 200 19660
2019-10-24 22:16:23,712 [django.server:124] [basehttp:log_message] [INFO]- "GET /static/assets/css/common.css HTTP/1.1" 200 1004
2019-10-24 22:16:23,714 [django.server:124] [basehttp:log_message] [INFO]- "GET /static/assets/js/app.js HTTP/1.1" 200 20844
2019-10-24 22:16:26,509 [django.server:124] [basehttp:log_message] [INFO]- "GET /api/report_list/1/ HTTP/1.1" 200 14649
2019-10-24 22:16:51,496 [django.server:124] [basehttp:log_message] [INFO]- "GET /api/test_list/1/ HTTP/1.1" 200 24874
2019-10-24 22:16:51,721 [django.server:124] [basehttp:log_message] [INFO]- "POST /api/add_case/ HTTP/1.1" 200 0
2019-10-24 22:16:59,707 [django.server:124] [basehttp:log_message] [INFO]- "GET /api/test_list/1/ HTTP/1.1" 200 24874
2019-10-24 22:16:59,909 [django.server:124] [basehttp:log_message] [INFO]- "POST /api/add_case/ HTTP/1.1" 200 0
2019-10-24 22:17:01,306 [django.server:124] [basehttp:log_message] [INFO]- "GET /api/edit_case/1/ HTTP/1.1" 200 36504
2019-10-24 22:17:06,265 [django.server:124] [basehttp:log_message] [INFO]- "GET /api/add_project/ HTTP/1.1" 200 17737
2019-10-24 22:17:07,825 [django.server:124] [basehttp:log_message] [INFO]- "GET /api/project_list/1/ HTTP/1.1" 200 29789
2019-10-24 22:17:13,116 [django.server:124] [basehttp:log_message] [INFO]- "GET /api/add_config/ HTTP/1.1" 200 24816
2019-10-24 22:17:19,671 [django.server:124] [basehttp:log_message] [INFO]- "GET /api/config_list/1/ HTTP/1.1" 200 19532
比如我要查找上面的从 2019-10-24 22:16:21 到 2019-10-24 22:16:59 这个时间段的日志:
sed -n '/2019-10-24 22:16:21/,/2019-10-24 22:16:59/p' access.log
看起来使用很简单,但是会有很大坑,比如时间后面的/p不能漏掉了!!!
三、注意点
开始时间和结束时间必须要是日志里面有的:
-
要是开始和结束的时间没有,那查找就没有结果
-
如果开始时间日志里面是没有的,那么查询结果为空
比如开始时间没有2019-10-24 22:16:22
sed -n '/2019-10-24 22:16:22/,/2019-10-24 22:16:59/p' access.log
3.如果结束时间日志里面是没有的,查询的结果就是开始时间到最后的全部日志
sed -n '/2019-10-24 22:16:21/,/2019-10-24 22:16:58/p access.log
四、查询方式
1.模糊查询
如果不知道日志的开始时间,不能精确到秒,可以用模糊查询,比如查询时间段2019-10-24 22:14 到 2019-10-24 22:16
sed -n '/2019-10-24 22:14:*/,/2019-10-24 22:16:*/p' access.log
2019-10-24 22:14:04,121 [django.server:124] [basehttp:log_message] [INFO]- code 400, message Bad request version ('HTTP')
2019-10-24 22:14:04,122 [django.server:124] [basehttp:log_message] [WARNING]- "GET ../../mnt/custom/ProductDefinition HTTP" 400 -
2019-10-24 22:16:21,052 [django.server:124] [basehttp:log_message] [INFO]- "GET /api/login HTTP/1.1" 301 0
按小时模糊查询
sed -n '/2019-10-24 21*/,/2019-10-24 22*/p' access.log
- 正则查询
截取出日志里从19点00开始到19点05的日志
sed -n '/^19:00:00/,/^19:05:59/p' access.log
上面的语句,需要日志文件里有19:00:00时的记录否则是匹配不了。
我们可以使用正则来匹配截取出19:00到19:05的日志如下:
sed -n '/^19:00:[0-5][0-9]/^19:05:[0-5][0-9]/p' access.log
截取出日志文件从第几行到第几行的输出,用于核对
sed -n '113406,114017p' access.log > accesss_out.txt
- 结合grep查询
sed 也可以结合 grep 使用,比如我查询上面日志某个时间段的带有 POST 的日志行
sed -n '/2019-10-24 22:16:21/,/2019-10-21 20:16:58/p' access.log | grep POST
2019-10-24 22:16:23,525 [django.server:124] [basehttp:log_message] [INFO]- "POST /api/login/ HTTP/1.1" 302 0
2019-10-24 22:16:51,721 [django.server:124] [basehttp:log_message] [INFO]- "POST /api/add_case/ HTTP/1.1" 200 0
2019-10-24 22:16:59,909 [django.server:124] [basehttp:log_message] [INFO]- "POST /api/add_case/ HTTP/1.1" 200 0
2019-10-24 22:17:19,864 [django.server:124] [basehttp:log_message] [INFO]- "POST /api/add_case/ HTTP/1.1" 200 0
- 用“\” 转义“/”
如果起始关键字带 / ,需要转义
从 /23/Jul/2018:17:45:00 到 /23/Jul/2018:17:48:00
sed -n '/23\/Jul\/2018:17:45:00/','/23\/Jul\/2018:17:48:00/p' access.log
四、 参考
keytool将他签名证书导入到jre证书管理库
https://www.jianshu.com/p/04bb1b8c20bf
Linux查看某个时间段的日志(sed -n)
https://blog.csdn.net/qq_38747892/article/details/127431750