Powershell TCP Port Scan

I had a piece of code to check a TCP port on a given host, and decided I would build a quick port scanner out of it.

function portCheck () {
    param ($hostName,$port);

    $portTest = (new-object net.sockets.tcpclient);

    try { $portTest.connect($hostName, $port) } catch {}

    if ( $portTest.Connected -eq "True") {
        echo "$hostName,$port - Success";
        $portTest.Dispose();
    } else {
        echo "$hostName,$port - Failed";
    }
}

#$portRange = ("22","80","3389");
$portRange = [System.Linq.Enumerable]::Range(1,65535);

Foreach ( $prt in $portRange ) {
    portCheck -hostName "192.168.1.1" -port $prt;
}
Advertisements

One thought on “Powershell TCP Port Scan

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s