Looking for help learning about queues

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 11 months ago.

  • Author
    Posts
  • #5936

    by willsteele at 2012-10-19 17:07:42

    I am trying to write a function that checks a port range to see what ports are open. As it is, the function looks at one port at a time. I want to stretch my brain a little and explore how to work with queues (or some other, similar technique) to take advantage of running multiple port tests at once as opposed to a sequential (synchronous) port-by-port test. Here is what I am starting with:
    function Test-Port
    {
    param(
    $IPAddress,
    $Port
    )

    try
    {
    (New-Object -TypeName System.Net.Sockets.TcpClient).Connect($IPAddress,$Port)
    $Port
    }
    catch
    {
    }
    }

    function Find-OpenPort
    {
    param(
    [Parameter(
    Mandatory = $true,
    Position = 0,
    ValueFromPipeline = $true
    )]
    [System.Net.IPAddress]
    $IPAddress,

    [Parameter(
    Mandatory = $false,
    Position = 1
    )]
    $StartingPort,

    [Parameter(
    Mandatory = $false,
    Position = 2
    )]
    $EndingPort
    )

    $queue = New-Object -TypeName System.Collections.Queue
    $StartingPort..$EndingPort |
    ForEach-Object {
    $queue.Enqueue($_)
    Test-Port -IPAddress $IPAddress -Port $_
    }
    }

    Find-OpenPort -IPAddress ([System.Net.IPAddress] "127.0.0.1") -StartingPort 1 -EndingPort 1024
    Part of what I hope to do is throttle how many ports are tested at once. I am thinking perhaps of doing this as a parameter -ThrottleCount. I have never done anything with queues before, so, there is probably something obvious I can do. My thought was to test entire port blocks, 16 at a time, or, 32 at a time. This way, I would be able to do a port scan significantly faster than if I let the ForEach walk through the range one by one. Also, I am not trying to put something out there for others to solve. I want to actually learn the concept of queues. I just need a little help thinking through it, so, if you want to hint instead of lay an answer all out there I'd be okay with that. The mechanics of how to tests port blocks in sets is what eludes me.

    by MattG at 2012-10-19 19:09:25

    Hey Will,

    It sounds like ultimately what you're after is a multi-threaded port scanner. What you'll need to accomplish that is asyncronous network IO. The *async methods of the System.Net.Sockets.Socket will get the job done. To interact with these methods in PowerShell, you'll need to become comfortable with Register-ObjectEvent. Specifically, you'll want to set up event handlers for SocketAsyncEventArgs.Completed. Let me know if you have any questions along the way. You have my email address. 😀

    ~Matt

    by willsteele at 2012-10-19 20:04:21

    I will do my background research on those topics and get back to you. Thanks for the direction. Feels like I'm going from the tips of my toes near the deep end to the unnumbered parts. : )

You must be logged in to reply to this topic.