조건부로 다음 행을 반환합니다.

조건부로 다음 행을 반환합니다.

보안 과정에서 사용하기 위해 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)

관련 정보