我的文件内容如下:(这是nmap输出)
Nmap scan report for x.x.x.x
Host is up (0.12s latency).
PORT STATE SERVICE
23/tcp open telnet
| telnet-brute:
| Accounts
| var1:var2
| Statistics
| Performed 5 guesses in 3 seconds, average tps: 1
|
|_ ERROR: Too many retries, aborted ...
Nmap scan report for y.y.y.y
Host is up (0.17s latency).
PORT STATE SERVICE
23/tcp open telnet
| telnet-brute:
| Accounts
| var3:var4
| Statistics
|_ Performed 2 guesses in 13 seconds, average tps: 0
Nmap scan report for z.z.z.z
Host is up (0.19s latency).
PORT STATE SERVICE
23/tcp open telnet
| telnet-brute:
| Accounts
| No valid accounts found
我想提取这种模式:
x.x.x.x var1:var2
y.y.y.y var3:var4
我尝试的是:
#!/bin/bash
NmapResult=$1
rm oooo
while read line
do
if [[ "$line" == *Nmap* ]]; then
OUT=$(grep "Nmap" -A6)
if [[ "$OUT" == *valid* ]]; then
continue
else
grep "Nmap" -A6 >> oooo
echo
fi
fi
done < $NmapResult
但这不是真的,也不完全是我想要的。
你能帮我解决这个问题吗?
由于
答案 0 :(得分:2)
$ awk -v RS= -F'\n' '{n=split($1,a,/ /); sub(/\| */,"",$7); print a[n], $7}' file
x.x.x.x var1:var2
y.y.y.y var3:var4
z.z.z.z No valid accounts found
或者如果要排除第3行输出,您可能需要:
$ awk -v RS= -F'\n' '$7~/:/{n=split($1,a,/ /); sub(/\| */,"",$7); print a[n], $7}' file
x.x.x.x var1:var2
y.y.y.y var3:var4
答案 1 :(得分:2)
这可能适合你(GNU sed):
sed -nr '/^Nmap.* /{s///;h};/Accounts/{n;H;g;s/\n\|\s*/ /;/:/p}' file
这将捕获所需的行并将其转换为所需的格式。