2017-11-19 5 views
0

목표는이 AWK 파일을하는 것입니다.PowerShell을 로그 파일 처리

그리고 Powershell을 통해 동등한 출력으로 변환하십시오.

나는 현재 다음 명령을

gc sample.log | sls "INext-DROP-DEFLT" | ForEach-Object { $_.line -match "SRC=(.*?)\s" > $null;$matches[1] + ":" + $matches[2] } | sort | Get-Unique | ForEach-Object -Begin { $LastSource = " "; $sources = 0; $ports = 0; } -process { $ip = $_.split(":")[0]; $port = $_.split(":")[1]; if($1 -ne $LastSource){print $1, $2 $LastSource = $1 $sources += 1 $Ports += 1 } else { print $2 $Ports += 1 } } END { print "\n\n" "Total Sources = ", $sources p 
rint "Unique Ports Scanned = ", $Ports } 

을 가지고 진행하는 방법을 enter image description here

확실이 오류가 발생합니다. 참조에 대한

로그 파일 : LogFileTinyUplaod

답변

1

확실 진행하는 방법.

  • 첫째, 일부 Powershell Best Practices (코드 형식, 들여 쓰기, ...)을 적용합니다. 그런 다음
  • , -endEND를 교체 한 후, 당신은 ... 다른 많은 오류를 얻을

목표 호스트에 기록하지 않는 한이 avoid Write-Host에 권장하지만, (일을해야 다음 코드 만; 대신 나중에 사용할 수 있도록 결과를 저장하려면 PSCustomObject을 빌드해야합니다.)

Get-Content D:\Downloads\SO\sample.log | 
    Select-String -Pattern "INext-DROP-DEFLT\s.*SRC=(.*?)\s.*DPT=(.*?)\s" -AllMatches | 
    ForEach-Object {$_.Matches} | 
     ForEach-Object {$_.Groups[1].Value + ':' + $_.Groups[2].Value} | # PSCustomObject place 
     Sort-Object | 
      Get-Unique | 
      ForEach-Object -Begin { 
       $LastSource = [string]::Empty; 
       $sources = 0; 
       $ports = 0; 
      } -process { 
       $ip, $port = $_.split(":"); 
       if ($ip -ne $LastSource) { 
        Write-Host "`n$ip $port" -NoNewline; # print Source IP and Destination Port 
        $LastSource = $ip; 
        $sources += 1; 
        $Ports += 1; 
       } else { 
        Write-Host " $port" -NoNewline; # Print DestPort to current line (multi port per IP) 
        $Ports += 1; 
       } 
      } -end { 
       Write-Host "`nTotal Sources = $sources"; 
       Write-Host "Unique Ports Scanned = $Ports"; 
      } 
+0

이것은 내가 찾고있는 것입니다. 올바른 최종 카운트를 인쇄하지 않기 때문에 조금 더 편집해야 할 것입니다. 그러나 이것은 나에게 훨씬 더 의미가 있습니다. –