linux - 使用 sed 解析 nmap -oG 输出

标签 linux bash parsing sed nmap

我有一个日志文件

...
Host: 111.222.121.123 (111.222.121.123.deploy.static.akamaitechnologies.com) Ports: 80/open/tcp//http//AkamaiGHost (Akamai's HTTP Acceleration|Mirror service)/, 443/open/tcp//ssl|http//AkamaiGHost (Akamai's HTTP Acceleration|Mirror service)/
Host: 1.2.3.4 ()  Ports: 80/open/tcp//http//cloudflare/, 443/open/tcp//ssl|https//cloudflare/, 2052/open/tcp//clearvisn?///, 2053/open/tcp//ssl|http//nginx/, 2082/open/tcp//infowave?///, 2083/open/tcp//ssl|http//nginx/, 2086/open/tcp//gnunet?///, 2087/open/tcp//ssl|http//nginx/, 2095/open/tcp//nbx-ser?///, 2096/open/tcp//ssl|http//nginx/, 8080/open/tcp//http-proxy//cloudflare/, 8443/open/tcp//ssl|https-alt//cloudflare/, 8880/open/tcp//cddbp-alt?///
Host: 2.3.4.5 (a104-96-1-61.deploy.static.akamaitechnologies.com)   Ports: 53/open/tcp//domain//(unknown banner: 29571.61)/
...

我需要提取 IP 和 http 端口并将其转换为以下格式

1.2.3.4:80,443,2083

日志文件中只有两种类型的port字段

80/open/tcp//http
2083/open/tcp//ssl|http

尝试使用 sed 但没有成功。我最终得到了这个功能失调的命令

cat ../host_ports.txt | sed -rn 's/Host: ([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}).*?([0-9]{1,5}\/open\/tcp\/\/http|[0-9]{1,5}\/open\/tcp\/\/ssl\|http).*/\1 \2/p'

最佳答案

首先处理重复的端口,然后将 Host/Port 替换为所需的格式。

sed -r 's/(Ports:|,) ([0-9]*)[^,]*/\1\2/g;s/Host: ([^ ]*).*Ports:/\1:/' ../host_ports.txt

编辑: 首先,我在某处给出了带有 http 的一行的所有端口,现在将结果限制为在其描述中带有 http 的端口。

sed -nr 's/Ports: /, /;
         s/, ([0-9]*)[^,]*http[^,]*/,\1/g;
         s/,[^,]*\/[^,]*//g;
         s/Host: ([^ ]*)[^,]*,/\1:/p'       ../host_ports.txt

关于linux - 使用 sed 解析 nmap -oG 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58343184/

相关文章:

linux - 信号处理程序后信号无法生成核心转储

linux 查找正则表达式

Python - 解析、分割文本并将其分成单独的行

python - 如何使用 Python 中的线程模块写入命名管道?

python - 使用 Python 安装信号处理程序

python - 拥有子进程允许 rpc-server 在子进程存活时重新启动

c - 如何在C中解析类似路径的字符串

linux - 如何编写一个 Linux bash 脚本来告诉我 LAN 中哪些计算机处于打开状态?

arrays - json 响应的 Bash 脚本条件

vb.net - 使用 Json 解析而不是 XML 解析。 VB网络