Skip to content

Commit 7373e80

Browse files
committed
Merge remote-tracking branch 'origin/master'
# Conflicts: # ScanPortsAsync.ps1
2 parents 43d1eb1 + 5ef899d commit 7373e80

File tree

2 files changed

+187
-8
lines changed

2 files changed

+187
-8
lines changed

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,44 @@
11
# PowerShell-Async-PortScanner
2+
3+
Powerful asynchronus Port-Scanner which returns a custom PowerShell-Object with basic informations about the scanned Port-Range include Port and Status.
4+
5+
## Description
6+
7+
This is a powerful asynchronus Port-Scanner working with the PowerShell RunspacePool. You can scan any Port-Range you want.
8+
9+
This script also work fine along with my asychronus IP-Scanner published on GitHub too. You can easily pipe the output of the IP-Scanner result in this script.
10+
11+
## Syntax
12+
13+
```powershell
14+
.\ScanPortsAsync.ps1 [-IPv4Address] <IPAddress> [[-StartPort] <Int32>] [[-EndPort] <Int32>] [[-Threads] <Int32>] [[-IncludeClosed]] [<CommonParameters>]
15+
```
16+
17+
## Example
18+
19+
Simple Port Scan
20+
```powershell
21+
.\ScanPortsAsync.ps1 -IPv4Address 192.168.1.100 -StartPort 1 -EndPort 5000
22+
```
23+
24+
Show closed Ports in result
25+
```powershell
26+
.\ScanPortsAsync.ps1 -IPv4Address 172.16.2.5 -Threads 200 -IncludeClosed
27+
```
28+
29+
30+
## Output
31+
32+
```powershell
33+
Port Status
34+
---- ------
35+
21 Open
36+
80 Open
37+
139 Open
38+
443 Open
39+
445 Open
40+
```
41+
42+
## ToDo
43+
- Integrate Port-List
44+
like: 80 (http), 443 (https) ...

ScanPortsAsync.ps1

Lines changed: 144 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
11
###############################################################################################################
22
# Language : PowerShell 4.0
3-
# Script Name : ScanNetworkAsync.ps1
3+
# Script Name : ScanPortsAsync.ps1
44
# Autor : BornToBeRoot (https://github.com/BornToBeRoot)
55
# Description : Asynchronus Port Scanner
66
# Repository : https://github.com/BornToBeRoot/PowerShell-Async-PortScanner
77
###############################################################################################################
88

99
<#
1010
.SYNOPSIS
11+
Powerful asynchronus Port-Scanner which returns a custom PowerShell-Object with basic informations about the
12+
scanned Port-Range include Port and Status.
1113
1214
.DESCRIPTION
15+
This is a powerful asynchronus Port-Scanner working with the PowerShell RunspacePool. You can scan any
16+
Port-Range you want.
17+
18+
This script also work fine along with my asychronus IP-Scanner published on GitHub too. You can easily
19+
pipe the output of the IP-Scanner result in this script.
20+
21+
If you found a bug or have some ideas to improve this script... Let me know. You find my Github profile in
22+
the links below.
1323
1424
.EXAMPLE
25+
.\ScanPortsAsync.ps1 -IPv4Address 172.16.0.1 -StartPort 1 -EndPort 1000
26+
27+
.EXAMPLE
28+
.\ScanPortsAsync.ps1 -IPv4Address 192.168.1.100 -IncludeClosed
1529
1630
.LINK
1731
Github Profil: https://github.com/BornToBeRoot
@@ -23,20 +37,32 @@ Param(
2337
[Parameter(
2438
Position=0,
2539
Mandatory=$true,
26-
HeldMessage='Enter IP-Address of the device which you want to scan')]
40+
HelpMessage='Enter IP-Address of the device which you want to scan')]
2741
[IPAddress]$IPv4Address,
2842

2943
[Parameter(
3044
Position=1,
3145
Mandatory=$false,
32-
HelpMessage='')]
46+
HelpMessage='Enter the Start-Port (Default=1)')]
3347
[Int32]$StartPort=1,
3448

3549
[Parameter(
3650
Position=2,
3751
Mandatory=$false,
38-
HelpMessage='Enter the Start Port')]
39-
[Int32]$EndPort=65535
52+
HelpMessage='Enter the End-Port (Default=65535)')]
53+
[Int32]$EndPort=65535,
54+
55+
[Parameter(
56+
Position=3,
57+
Mandatory=$false,
58+
HelpMessage='Set the maximum number of threads at the same time (Default=100)')]
59+
[Int32]$Threads=100,
60+
61+
[Parameter(
62+
Position=4,
63+
Mandatory=$false,
64+
HelpMessage='Show closed Ports in result')]
65+
[Switch]$IncludeClosed
4066
)
4167

4268
Begin{
@@ -49,21 +75,131 @@ Begin{
4975
# Validate Port-Range
5076
if($StartPort -gt $EndPort)
5177
{
78+
<<<<<<< HEAD
5279
Write-Host "Check your input! Invalid Port-Range (-StartPort can't be lower than -EndPort)" -ForegroundColor Red
5380
exit
5481
}
5582

5683
# Some User-Output about the selected or default settings
5784

85+
=======
86+
Write-Host "Check your input! Invalid Port-Range... (-StartPort can't be lower than -EndPort)" -ForegroundColor Red
87+
exit
88+
}
89+
90+
$PortRange = ($EndPort - $StartPort)
91+
92+
if(-not( Test-Connection -ComputerName $IPv4Address -Count 2 -Quiet))
93+
{
94+
Write-Host "IP-Address not reachable!" -ForegroundColor Red
95+
exit
96+
}
97+
98+
# Some User-Output about the selected or default settings
99+
Write-Host "`nScript ($ScriptFileName) started at $StartTime" -ForegroundColor Green
100+
Write-Host "`n+---------------------------------------Settings----------------------------------------`n|"
101+
Write-Host "| IP-Address:`t$IPv4Address"
102+
Write-Host "| Port-Range:`t$StartPort-$EndPort"
103+
Write-Host "| Threads:`t`t$Threads"
104+
Write-Host "|`n+---------------------------------------------------------------------------------------`n"
105+
>>>>>>> origin/master
58106
}
59107

60108
Process{
109+
# Scriptblock that will run in runspaces (threads)...
110+
[System.Management.Automation.ScriptBlock]$ScriptBlock = {
111+
# Parameters
112+
$IPv4Address = $args[0]
113+
$Port = $args[1]
114+
115+
try{
116+
$Socket = New-Object System.Net.Sockets.TcpClient($IPv4Address,$Port)
117+
118+
if($Socket.Connected)
119+
{
120+
$Status = "Open"
121+
$Socket.Close()
122+
}
123+
}
124+
catch
125+
{
126+
$Status = "Closed"
127+
}
128+
129+
$Result = New-Object -TypeName PSObject
130+
Add-Member -InputObject $Result -MemberType NoteProperty -Name Port -Value $Port
131+
Add-Member -InputObject $Result -MemberType NoteProperty -Name Status -Value $Status
132+
return $Result
133+
}
134+
135+
# Setting up runspaces
136+
Write-Host "Setting up Runspace-Pool...`t`t" -ForegroundColor Yellow -NoNewline
61137

138+
$RunspacePool = [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspacePool(1, $Threads, $Host)
139+
$RunspacePool.Open()
140+
$Jobs = @()
141+
142+
Write-Host "[" -ForegroundColor Gray -NoNewline; Write-Host "Done" -ForegroundColor Green -NoNewline; Write-Host "]" -ForegroundColor Gray
62143

63-
}
144+
#Setting up jobs
145+
Write-Host "Setting up jobs...`t`t`t" -ForegroundColor Yellow -NoNewline
146+
147+
foreach($Port in $StartPort..$EndPort)
148+
{
149+
if($PortRange -gt 0) { $Progress_Percent = (($Port - $StartPort) / $PortRange) * 100 } else { $Progress_Percent = 100 }
150+
Write-Progress -Activity "Setting up jobs..." -Id 1 -Status "Current Port: $Port" -PercentComplete ($Progress_Percent)
151+
152+
$Job = [System.Management.Automation.PowerShell]::Create().AddScript($ScriptBlock).AddArgument($IPv4Address).AddArgument($Port)
153+
$Job.RunspacePool = $RunspacePool
154+
$Jobs += New-Object psobject -Property @{
155+
RunNum = $Port - $StartPort
156+
Pipe = $Job
157+
Result = $Job.BeginInvoke()
158+
}
159+
}
160+
161+
Write-Host "[" -ForegroundColor Gray -NoNewline; Write-Host "Done" -ForegroundColor Green -NoNewline; Write-Host "]" -ForegroundColor Gray
64162

163+
# Wait until all Jobs are finished
164+
Write-Host "Waiting for jobs to complete...`t`t" -ForegroundColor Yellow -NoNewline
65165

66-
End{
166+
Do {
167+
Start-Sleep -Milliseconds 500
168+
169+
Write-Progress -Activity "Waiting for jobs to complete... ($($Threads - $($RunspacePool.GetAvailableRunspaces())) of $Threads threads running)" -Id 1 -PercentComplete (($Jobs.Count - $($($Jobs | Where-Object {$_.Result.IsCompleted -eq $false}).Count)) / $Jobs.Count * 100) -Status "$(@($($Jobs | Where-Object {$_.Result.IsCompleted -eq $false})).Count) remaining..."
170+
} While ($Jobs.Result.IsCompleted -contains $false)
171+
172+
Write-Host "[" -ForegroundColor Gray -NoNewline; Write-Host "Done" -ForegroundColor Green -NoNewline; Write-Host "]" -ForegroundColor Gray
173+
174+
Write-Host "Process results...`t`t`t" -ForegroundColor Yellow -NoNewline
67175

176+
# Built global array
177+
$Results = @()
68178

69-
}
179+
# Get results and fill the array
180+
foreach($Job in $Jobs)
181+
{
182+
$Results += $Job.Pipe.EndInvoke($Job.Result)
183+
}
184+
185+
Write-Host "[" -ForegroundColor Gray -NoNewline; Write-Host "Done" -ForegroundColor Green -NoNewline; Write-Host "]" -ForegroundColor Gray
186+
}
187+
188+
189+
End{
190+
$EndTime = Get-Date
191+
192+
$ExecutionTimeMinutes = (New-TimeSpan -Start $StartTime -End $EndTime).Minutes
193+
$ExecutionTimeSeconds = (New-TimeSpan -Start $StartTime -End $EndTime).Seconds
194+
195+
# Some User-Output with Device UP/Down and execution time
196+
Write-Host "`n+----------------------------------------Result-----------------------------------------`n|"
197+
Write-Host "| Ports Open:`t`t$(@($Results | Where-Object {($_.Status -eq "Open")}).Count)"
198+
Write-Host "| Ports Closed:`t$(@($Results | Where-Object {($_.Status -eq "Closed")}).Count)"
199+
Write-Host "|`n+---------------------------------------------------------------------------------------`n"
200+
Write-Host "Script duration:`t$ExecutionTimeMinutes Minutes $ExecutionTimeSeconds Seconds`n" -ForegroundColor Yellow
201+
Write-Host "Script ($ScriptFileName) exit at $EndTime`n" -ForegroundColor Green
202+
203+
# Return custom psobject with Port status
204+
if($IncludeClosed) { return $Results } else { return $Results | Where-Object {$_.Status -eq "Open"} }
205+
}

0 commit comments

Comments
 (0)