我对解析nginx访问日志有疑问,
此代码<<< grep "pagename" <<< "$line0"
无法正常工作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
感谢。
答案 0 :(得分:0)
语法<<< grep "pagename" <<< "$line0"
略微偏离,<<<X
输入
字符串X到标准输入。
您需要的语法是流程替换语法(与重定向箭头结合使用):
< <(grep "pagename" <<< "$line0")
这也是一个更简单的解决方案:
#! /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')
第一个grep
的结果通过管道输送到第二个,然后ips流被重定向到while
- 循环,该循环为每个ips运行curl。
您还可以将其与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/"{}