Hack Forums
[Shell] [PowerShell] TCP Port Scanner - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Shell] [PowerShell] TCP Port Scanner (/showthread.php?tid=5698531)



[PowerShell] TCP Port Scanner - Travelers - Aug 19, 2017

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")



* - HFDone - Aug 20, 2017

close the TcpClient if its open


* - Travelers - Aug 20, 2017

(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?


* - HFDone - Aug 20, 2017

you open it, when you done you close it !
$scan.Connect($remoteIP, $port);
if ($scan.Connected()) {$scan.Close()}


* - Travelers - Aug 20, 2017

(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?


* - HFDone - Aug 20, 2017

why not ? you have 1024 open connection do nothing


* - Travelers - Aug 20, 2017

(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.


* - HFDone - Aug 20, 2017

if ($scan.Connected()) {Write-Host "Port $Port is up";$scan.Close()}