| Subcribe via RSS

Howto: Netstat CSV

October 8th, 2010 | No Comments | Posted in Scripting, Security

How to output netstat result to CSV (comma-separated values/character-separated value).

Example netstat to CSV with “-an” flags.
for /F "tokens=1-4 delims= " %A in ('netstat -an') do echo %A,%B,%C,%D

Example netstat to CSV with “-ano” flags with output to file.
for /F "tokens=1-5 delims= " %A in ('netstat -ano') do echo %A,%B,%C,%D,%E>>outputfile.csv

Note if you are going to use it in a batch script, remember to use the following format:
for /F "tokens=1-4 delims= " %%A in ('netstat -an') do echo %%A,%%B,%%C,%%D

And you can of course use this to just list “Listening” ports:
for /F "tokens=1-4 delims= " %A in ('netstat -an ^| find "LISTENING"') do echo %A,%B,C%,%D

Why would you do this?
1. Openports (from DiamonCS is licensed)
2. No need for third party binaries.

Why would you not do this?
1. Hard to parse netstat -anob

If you know how to parse “netstat -anob”, please feel free to leave a comment 😉

Tags: , , , ,
technoblog@trap.threatobs.com