简体   繁体   English

解析nginx访问日志并提取IP,检测每个已解析IP的地理位置

[英]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.

相关问题 使用Bash从日志文件的每一行提取IP地址 - Using Bash to Extract IP Addresses From Each Line of Log File 如何收集IP和用户代理信息,并使用AWK从Nginx访问日志中唯一地标识它们的基本IP地址? - How to gather IP and User Agent info and uniq them base IP address from nginx access log with AWK? 如何从 Apache 访问日志中解析每个 IP 并在 ZC617916 脚本中的 CSV 文件中计算来自它们的每个唯一请求? - How to parse every IP from the Apache access log and count each unique request from them in a CSV file in a bash script? bash从日志文件中提取IP并保存到ext文本文件 - bash extract ip from log file and save to ext text file 过滤access.log以返回每个客户端的唯一IP地址列表和成功请求数(代码200)的有效方法是什么? - What's an effective way to filter access.log to return list of unique IP addresses and the number of successful requests (code 200) by each client? Apache访问日志,用于最常见的IP地址bash脚本 - Apache access log for the most common IP address bash script 过滤nginx访问日志 - Filter nginx access log bash中公网IP地址检测方法 - Methods to detect public IP address in bash 从文件中提取以IP地址开头的行 - Extract lines starting with IP address from file 如何在bash中提取变量的IP部分 - How to extract the IP part of a variable in bash
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM