Home > Powershell Scripts > Portscan with Powershell

Portscan with Powershell

[Update 16-05-2009 – Format update]

So I found myself in need of a tool which could check whether or not one or more ports are open on a large amount of servers.

I ended up with this in powershell:

param([string]$list1,[string]$list2)
if ($list1 -eq ""){
    Write-Host "Please supply Host-list!!" -ForegroundColor Red
    break
    }
If ($list2 -eq ""){
    Write-Host "Please supply Port-List!!" -ForegroundColor Red
    break
    }
[Array]$hostlist = Get-Content $list1
[Array]$ports = Get-Content $list2
$ErrorActionPreference = "SilentlyContinue"
$ping = new-object System.Net.NetworkInformation.Ping
foreach ($ip in $hostlist) {
    $rslt = $ping.send($ip)
    if (! $?){
        Write-Host "Host: $ip - not found" -ForegroundColor Red
    }
    else {
        if ($rslt.status.tostring() –eq “Success”) {
            write-host "Host: $ip - Ports: " -foregroundColor Green -NoNewline
            foreach ($port in $ports){
                $socket = new-object System.Net.Sockets.TcpClient($ip, $port)
                if ($socket –eq $null) {
                    write-host "$port," -ForegroundColor Red -NoNewline
                }
                else {
                    write-host "$port,"-foregroundcolor Green -NoNewline
                    $socket = $null
                }
            }
        }
        else {
            write-host "Host: $ip - down" -ForegroundColor Red
        }
    }
Write-Host ""
}
$ping = $null

The script is executed in the following manner:

[ ] PS> .\script.ps1 hostlist.txt portlist.txt

In this version, the output of the script is not suited to be piped to a file, as port status is indicated with color.

  1. 12/05/2009 at 18:04

    I like what you’ve done here. I hope you don’t mind but I took this as a teaching opportunity for people just learning PowerShell and expanded on your solid start.

    http://blog.sapien.com/index.php/2009/05/12/a-powershell-port-scan/

  2. Steve
    10/02/2011 at 02:07

    Hi there, awesome script and perfect for what i need, although i needed to output this to a file rather than screen. I’ve asked the same Q over at Powershellcommunity.org, but referenced your site, so hope you dont mind?

    Steve

  3. Vlad
    09/03/2012 at 15:06

    Hi,
    how to specify whether it is TCP or UDP port?

    Vlad

  4. mark
    01/06/2012 at 16:38

    Nice man, dont need to find out myself now 😉 Although I am gonna modify it a little for my own purposes. Again Nice job!

  5. Magnus Ahlén
    14/05/2013 at 12:34

    Hi I have used your script but change it little, just to support port ranges.
    Example 21,80,118,135-139,443,445,512-515,1023-1080,1433,1521,3389

    Here is the code:

    Function PortScan
    {
    param([string]$Hostname,[string]$PortList)
    $PortArr = $Null
    $tmpPortArr = $Null
    $NumArr = $Null

    # Turn off error pipeline
    $ErrorActionPreference = “SilentlyContinue”

    Write-Host “”
    Write-Host “Check these ports:”
    Write-Host $PortList
    Write-Host “”

    $tmpPortArr = @($PortList -split “,”)

    foreach($Numb in $tmpPortArr)
    {
    if($Numb -match “-“)
    {
    $i = 0
    $NumArr = $Numb -split “-“, 2
    $PortArr += @([int]$NumArr[0])

    do
    {
    $i++
    $PortArr += @([int]$NumArr[0] + $i)
    }
    until ([int]$NumArr[1] -eq $PortArr[$PortArr.Length-1])
    }
    else
    {
    $PortArr += @($Numb)
    }
    }

    $Ports = $PortArr

    # Server Response
    $ping = new-object System.Net.NetworkInformation.Ping
    $Result = $ping.send($Hostname)

    if ($Result.status.tostring() –eq “Success”) {
    write-host “Host: $Hostname – Ports: ” -foregroundColor Green -NoNewline

    foreach ($port in $Ports){
    $socket = new-object System.Net.Sockets.TcpClient($Hostname, $port)

    if ($socket –eq $null) {
    write-host “$port, ” -ForegroundColor Red -NoNewline
    }
    else {
    write-host “$port, “-foregroundcolor Green -NoNewline
    $socket = $null
    }
    }

    }
    else {
    write-host “Host: $Hostname – down” -ForegroundColor Red
    }

    $ping = $null
    }

  6. 01/04/2014 at 13:03

    Thank you so much. Scanning for alive hosts is totally horrible: https://gist.github.com/mbrownnycnyc/9913361

  1. 12/05/2009 at 18:01
  2. 17/05/2009 at 13:27
  3. 21/09/2012 at 18:17
  4. 09/04/2014 at 13:30

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

%d bloggers like this: