Last active
April 25, 2020 18:21
-
-
Save daniel0x00/58c9f17b4319ec6e275fb871d6e1d5e9 to your computer and use it in GitHub Desktop.
Convert nmap 'grepeable' output into a PowerShell PSObject
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Deprecated. | |
# Usage: | |
# nmap 'grepeable' scan: nmap -oG outputfile.txt --open google.com | |
# when scan finishes, run: | |
# Get-Content outputfile.txt | Convert-NmapOutput | |
# | |
function Convert-NmapOutput { | |
[CmdletBinding()] | |
[OutputType([psobject])] | |
param ( | |
[Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)] | |
[string[]] $Data | |
) | |
begin { $output = @() } | |
process { | |
$Data | ForEach-Object { | |
if ($_.Contains('Ports:')) { | |
$ip = [string](([regex]::Match($_,"(?<ip>(?:(?:2(?:[0-4][0-9]|5[0-5])|[0-1]?[0-9]?[0-9])\.){3}(?:(?:2([0-4][0-9]|5[0-5])|[0-1]?[0-9]?[0-9])))")).groups["ip"].value) | |
$hostname = [string](([regex]::Match($_,"\d\s\((?<hostname>[\w\-\.]+)\)")).groups["hostname"].value) | |
$port = [string](([regex]::Match($_,"Ports:\s(?<port>[\w\/\,\-\s]+\/)")).groups["port"].value) | |
$out = New-Object PSObject | |
$out | Add-Member -type NoteProperty -name ip -Value $ip | |
$out | Add-Member -type NoteProperty -name hostname -Value $hostname | |
$out | Add-Member -type NoteProperty -name port -Value $port | |
$output += $out | |
} | |
} | |
} | |
end { $output } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment