
セキュリティプロセスで使用するために、WindowsオペレーティングシステムがインストールされているコンピューターのIPアドレスのリストを返そうとします。出力は次の形式です。
Nmap scan report for 192.168.xx.xxx
Host is up (0.066s latency).
PORT STATE SERVICE
139/tcp open netbios-ssn
445/tcp open microsoft-ds
MAC Address:
Host script results:
| smb-os-discovery:
| OS: Windows Server (R) 2008 Standard 6001 Service Pack 1 (Windows Server (R) 2008 Standard 6.0)
| OS CPE: cpe:/o:microsoft:windows_server_2008::sp1
| Computer name:
| NetBIOS computer name:
| Workgroup: WORKGROUP
|_ System time: 2015-12-22T17:01:33-08:00
以下を使用して値をより良い形式に変換することができました。grep "for\|Windows"
Nmap scan report for 192.168.xx.xx1
| OS: Windows XP (Windows 2000 LAN Manager)
Nmap scan report for 192.168.xx.xx5
| OS: Windows 2000 (Windows 2000 LAN Manager)
Nmap scan report for 192.168.xx.xx8
Nmap scan report for 192.168.xx.x15
次の行に「|」が含まれている場合は、前の行(grep)の値を取得しようとしている方法がわかりません。試してみましたがtr "|" "\b"
効果がありませんでした。
入力(テキストファイルに保存)
Line 1
| Line2
Line 3
| Line 4
Line 5
| Line 6
Line 7
Line 8
Line 9
| Line 10
Line 11
希望の出力
Line 1
Line 3
Line 5
Line 9
答え1
grep
たとえば、sed
バッファを保存し、OSがWindowsの場合にのみパターンスペースにコピーするためにIPを使用して行を保存できます(他のすべての行を削除)。
... | sed '/^Nmap scan report for/h;/^|[[:blank:]]*OS: Windows/!d;g'
または同様の方法でを使用しますawk
。今回は、行全体ではなくIPのみを印刷します。
... | awk '/^Nmap scan report for/{t=$5};/^\|[[:blank:]]*OS: Windows/{print t}'
答え2
sed -e'$!N;/\n|/P;D' \
<<""
Line 1
| Line2
Line 3
| Line 4
Line 5
| Line 6
Line 7
Line 8
Line 9
| Line 10
Line 11
Line 1
Line 3
Line 5
Line 9
もっと深く入ると...
sed -e'/^ *|/!{$!N;/\n|/P;}' -eD \
<<""
Line 1
| Line2
| Line 3
|___| Line 4
| Line 5
|___Line 6
Line 7
Line 8
Line 9
| Line 10
Line 11
Line 1
Line 9
grep
次に削除できます。
sed -ne'/^Nmap.* /!{/^|.*: Win.*(W/!d;}' \
-e's///;/)/H;x;s/\n/: (W/p' \
<<""
Nmap scan report for 192.168.xx.xxx
Host is ...
bla... and more ...
and bla and so on...
#
Host script results:
| smb-os-discovery:
| OS: not windows Server (R) ... !(Windows Server (R) 2008 Standard 6.0)
| OS CPE: cpe:/o:microsoft:windows_server_2008::sp1
| Comp ... some words ...
| Net... more words ...
| Work... words again ...
|_ Sys...
#
Nmap scan report for 192.168.xx.xxx
Host is ...
MAC Address:
#
Host script results:
| smb-os-discovery:
| OS: Windows Server (R) 2008 Standard 6001 Service Pack 1 (Windows Server (R) 2008 Standard 6.0)
|_ System time: 2015-12-22T17:01:33-08:00
192.168.xx.xxx: (Windows Server (R) 2008 Standard 6.0)