Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Shell 

[PowerShell] TCP Port Scanner

Been messing with this thing for about a day now. I'm by no means a coding expert so I'm pretty thrilled I was able to make my first port scanner. This thing only scans TCP ports, and you are able to define the ports/port range you want to scan.

I put plenty of comments in there, also. If you find any errors while running it, please let me know so I can correct them. Also feel free to tweak based on your needs or make your own improvements.

Code
# PowerShell port scanner v 0.1.3
#
# added $scan.dispose()
#
# clear screen
cls
# change title window name
$host.ui.RawUI.WindowTitle = "Power Port Scanner"
# port range listed in an array
# define range of ports to scan
# using ".." creates a range to read from
$ports = 1..1024
# store the time in a variable
$dateTime = Get-Date
Write-Host "Welcome to the Power Port Scanner"
$remoteIP = Read-Host "What is the IP of the remote host to scan: "
Write-Host "Starting scan of $remoteIP at $dateTime"
# create Scanner Function
# use foreach loop to scan each port in $ports
Function Scanner {
  foreach ($port in $ports)
  {
    try {
        $scan = New-Object System.net.sockets.TcpClient
        $scan.Connect($remoteIP, $port);
        Write-Host "Port $Port is up"
      }
      # send all errors to oblivion
    catch {
      $_ > null
      }
    }
    $scan.dispose()
}
Scanner
Write-Host "Scan completed at $dateTime"
# create an exit to the script
Write-Host "Press any key to continue ..."
# silently exit running script
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
[Image: pia_banner_3.png]
close the TcpClient if its open
[Image: bitcoin.png] Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP
(Aug 20, 2017 08:38 AM)HFDone Wrote:  close the TcpClient if its open

Would you be able to elaborate on that? Why would I need to do this if I'm just checking to see if the ports are open?

Are you suggesting that I leave a $scan.dispose at the end of the script?
[Image: pia_banner_3.png]
you open it, when you done you close it !
$scan.Connect($remoteIP, $port);
if ($scan.Connected()) {$scan.Close()}
[Image: bitcoin.png] Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP
(Aug 20, 2017 10:16 AM)HFDone Wrote:  you open it, when you done you close it !
$scan.Connect($remoteIP, $port);
if ($scan.Connected()) {$scan.Close()}

Yes, ok. Close it when I'm done. But could you explain why?
[Image: pia_banner_3.png]
why not ? you have 1024 open connection do nothing
[Image: bitcoin.png] Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP
(Aug 20, 2017 12:32 PM)HFDone Wrote:  why not ? you have 1024 open connection do nothing

Right. I was looking for a much more detailed answer, but Google saved the day. Thanks, Google.

(Aug 20, 2017 10:16 AM)HFDone Wrote:  you open it, when you done you close it !
$scan.Connect($remoteIP, $port);
if ($scan.Connected()) {$scan.Close()}

Adding this section, as you have described, in the script causes it to not work correctly. I have added $scan.dispose() to clean up resources that were being used by the $scan object, inside the Function that I called. This is more tidy and adds fewer characters to the code overall. Thank you for suggesting this.
[Image: pia_banner_3.png]
if ($scan.Connected()) {Write-Host "Port $Port is up";$scan.Close()}
[Image: bitcoin.png] Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by samir5000Mar 13, 2025 01:32 PM 3 324 Mar 13, 2025 01:32 PM
Last Post: Bastille
[Shell] by PELockDec 9, 2024 09:42 AM 3 657 Dec 9, 2024 09:42 AM
Last Post: PELock
[Source] by QismonNov 27, 2024 04:29 AM 2 305 Nov 27, 2024 04:29 AM
Last Post: mikeyp