Portscan against a single host

This topic contains 9 replies, has 4 voices, and was last updated by  Max Kozlov 4 months ago.

  • Author
    Posts
  • #73346

    rustyjarz
    Participant

    Hi all,

    I would like to write a one-liner to scan a host for a list of predefined ports and then output that to a csv so I can import to excel and then manipulate the data. From the console the below works fine (without the export-csv), but when when I export it to excel the output is screwed. I also tried out-file and had similar results. you may recognise the ports, they are the ports required for vmware virtual centre. Thanks in advance.

    
    $ports = “80”,”88”,”389”,”443”,”902”,”903”,”1234”,”1235”,”2012”,”2013”,”2014”,”8080”,”8085”,”8089”,”8443”,”60099”,”6501”,”6502”,”7005”,”7009”,”7080”,”7331”,”7343”,”7444”,”8000”,”8009”,”8100”,”8182”,”8200”,”9000”,”9001”,”9002”,”9003”,”9004”,”9005”,”9006”,”9007”,”9008”,”9009”,”9010”,”9443”,”9875”,”9876”,”9877”,”9090”,”10080”,”10111”,”10443”,”11711”,”11712”,”12721”,”8190”,”8191”,”22000”,”22100”,”31010”,”31100”,”32010”,”32100”,”12443”
    
    foreach($port in $ports){ Test-NetConnection -ComputerName 'the computer I was to port scan' -port $port | export-csv -notypeinformation 'pathtothefiletoexprot' }
    
  • #73370

    js
    Participant

    Works ok for me. Pipe it to select first if you want fewer properties. There's more properties than get printed by default. Welcome to Powershell. I wish test-netconnection had a timeout option. You might like this invoke-tspingsweep script. Ping and port scan with a little .net added to powershell is pretty easy. https://blogs.technet.microsoft.com/heyscriptingguy/2012/07/02/use-powershell-for-network-host-and-port-discovery-sweeps/

    • #73412

      rustyjarz
      Participant

      Thanks but I disagree. Yes the script works, but the export of the results doesn't and that's what I wan't, the resultsto a CSV.

  • #73420

    Max Kozlov
    Participant

    move export-csv outside of a loop

    $result = foreach ...
    $resilt | export-csv ...
    

    or change foreach to foreach-object

    $ports | foreach-obeject { Test-NetConnection -Port $_ -Comp... } | Export-csv ...
    
    • #73442

      js
      Participant

      My bad. I should have noticed the csv only had 2 rows. I wish the -port option took a list too.

    • #73484

      Ben Camareno
      Participant

      You mean like:

       $Ports = Get-Content .\portlist.txt # portlist.txt being the list of your ports
    • #73489

      js
      Participant

      I mean like:

      Test-NetConnection -ComputerName 'the computer I was to port scan' -port $ports
    • #73499

      Ben Camareno
      Participant

      Well you still need to loop the list of ports somehow, best you could do would be something like this:

      foreach($port in (Get-Content .\portlist.txt)){ Test-NetConnection -ComputerName 'the computer I was to port scan' -port $port}
    • #73543

      rustyjarz
      Participant

      Thanks Max that's what I needed (and forgot to do), now I need a drink!

  • #73546

    Max Kozlov
    Participant

    🙂
    btx, get rid of quotes around ports, it not strings, it is integers


    $ports = 80,443, ...

You must be logged in to reply to this topic.