[英]Parse nginx access log and extract IP, detect geo location for each parsed IP
I have question about parsing nginx access log, 我有关于解析nginx访问日志的问题,
have error in this code <<< grep "pagename" <<< "$line0"
此代码中有错误
<<< grep "pagename" <<< "$line0"
not work grep "pagename"
不工作
grep "pagename"
while IFS= read -r line0
do
ipList=$( grep -oP '\b(?:(?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2}))\b'
<<< grep "pagename" <<< "$line0")
for oneIP in $ipList
do
curl -s "http://ipinfo.io/$oneIP"
done
done < /var/log/nginx/access.log
Thanks. 谢谢。
The syntax <<< grep "pagename" <<< "$line0"
is slighly off, <<<X
inputs the string X to the stdin. 语法
<<< grep "pagename" <<< "$line0"
关闭, <<<X
将字符串X输入到标准输入。
The syntax you want is the process substitution syntax (combined with a redirection-arrow): 您需要的语法是进程替换语法(与重定向箭头结合):
< <(grep "pagename" <<< "$line0")
Here's also a simpler solution: 这也是一个更简单的解决方案:
#! /bin/bash
while read -r oneIP; do
curl -s "http://ipinfo.io/$oneIP"
done < <(grep "pagename" /var/log/nginx/access.log \
| grep -oP '\b(?:(?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2}))\b')
The result of the first grep
is piped to the second, and that stream of ips is then redirected to the while
-loop which runs the curl for each of the ips. 第一个
grep
的结果通过管道输送到第二个,然后将ips流重定向到while
-loop,它运行每个ips的curl。
You could also parallelize this with something like gnu parallel : 您还可以将此与gnu parallel并行化 :
#! /bin/bash
grep "pagename" /var/log/nginx/access.log \
| grep -oP '\b(?:(?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2}))\b' \
| parallel echo curl -s "http://ipinfo.io/"{}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.