Skip to content

Instantly share code, notes, and snippets.

@nullbind
Last active December 1, 2016 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nullbind/d1ea79cf0284b5b667b9a63a326f912c to your computer and use it in GitHub Desktop.
Save nullbind/d1ea79cf0284b5b667b9a63a326f912c to your computer and use it in GitHub Desktop.
hacky ps script to convert ip,openport to ip, allopenports
# hacky ps script to convert ip,openport to ip,allopenports
# Container for final list
$FullList = New-Object System.Data.DataTable
$FullList.Columns.Add("Dest") | Out-Null
$FullList.Columns.Add("Port") | Out-Null
# import full list of open ports – one per line (dest,port)
$ImportList = import-csv C:\temp\open-ports-by-line.csv
# Create unique ip list
$IPs = $ImportList | Select-Object dest -Unique
# loop through ips
$IPs |
ForEach-Object {
$IP = $_.Dest
# loop through full list
$ImportList |
ForEach-Object{
$Dest = $_.Dest
$Port = $_.Port
# check if it is current ip
if($IP -eq $Dest){
# build port list
$ports = "$ports $port,"
$GoodDest = $_.Dest
$GoodPort = $_.Port
}
}
# remove trailing
$ports = $ports.Substring(0,$ports.Length-1)
# Add ip info to final list
$FullList.Rows.Add($GoodDest,$ports) |out-null
# clear port list
$ports = ""
}
# return list and write to file
$FullList
$FullList | Export-Csv -NoTypeInformation c:\temp\open-ports-summary-by-ip.csv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment