使用 sed 解析 nmap -oG 输出
Parsing nmap -oG output using sed
我有一个日志文件
...
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
日志文件
中只有两种类型的端口字段
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).*/ /p'
这个脚本会为你做,你不需要 sed
:
#!/bin/bash
while read -r line; do
if echo $line | grep -q "http"; then
host=$(echo "$line" | grep -Po '(?<=^Host: )[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+')
ports=$(echo "$line" | grep -Po '[0-9]*((?=\/open\/tcp\/\/http)|(?=\/open\/tcp\/\/ssl\|http))' | tr '\n' ',')
echo "$host:${ports:0:-1}"
fi
done < ../log
在 Look behind 的帮助下,第一个 grep 将捕获 IP 地址。 -P
是像regex一样使用perl,-o
是只输出匹配的字符串
第二个正则表达式与第一个非常相似,但使用后视而不是后视。它只会捕获后跟 /open/tcp//http
或 /open/tcp//ssl|http
的端口。紧随其后的 tr
将用逗号替换换行符。
${ports:0:-1}
只是为了去掉结尾的逗号。
希望对您有所帮助!
首先处理重复端口,然后将 Host/Port 替换为所需格式。
sed -r 's/(Ports:|,) ([0-9]*)[^,]*//g;s/Host: ([^ ]*).*Ports:/:/' ../host_ports.txt
编辑:
首先,我在某处给出了带有 http 的一行的所有端口,现在将结果限制为在其描述中带有 http
的端口。
sed -nr 's/Ports: /, /;
s/, ([0-9]*)[^,]*http[^,]*/,/g;
s/,[^,]*\/[^,]*//g;
s/Host: ([^ ]*)[^,]*,/:/p' ../host_ports.txt
我有一个日志文件
...
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
日志文件
中只有两种类型的端口字段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).*/ /p'
这个脚本会为你做,你不需要 sed
:
#!/bin/bash
while read -r line; do
if echo $line | grep -q "http"; then
host=$(echo "$line" | grep -Po '(?<=^Host: )[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+')
ports=$(echo "$line" | grep -Po '[0-9]*((?=\/open\/tcp\/\/http)|(?=\/open\/tcp\/\/ssl\|http))' | tr '\n' ',')
echo "$host:${ports:0:-1}"
fi
done < ../log
在 Look behind 的帮助下,第一个 grep 将捕获 IP 地址。 -P
是像regex一样使用perl,-o
是只输出匹配的字符串
第二个正则表达式与第一个非常相似,但使用后视而不是后视。它只会捕获后跟 /open/tcp//http
或 /open/tcp//ssl|http
的端口。紧随其后的 tr
将用逗号替换换行符。
${ports:0:-1}
只是为了去掉结尾的逗号。
希望对您有所帮助!
首先处理重复端口,然后将 Host/Port 替换为所需格式。
sed -r 's/(Ports:|,) ([0-9]*)[^,]*//g;s/Host: ([^ ]*).*Ports:/:/' ../host_ports.txt
编辑:
首先,我在某处给出了带有 http 的一行的所有端口,现在将结果限制为在其描述中带有 http
的端口。
sed -nr 's/Ports: /, /;
s/, ([0-9]*)[^,]*http[^,]*/,/g;
s/,[^,]*\/[^,]*//g;
s/Host: ([^ ]*)[^,]*,/:/p' ../host_ports.txt