Skip to content

Commit

Permalink
Code improved
Browse files Browse the repository at this point in the history
  • Loading branch information
BornToBeRoot committed Aug 18, 2016
1 parent 4592b40 commit 96ea87b
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 15 deletions.
Binary file added Documentation/Images/IPv4PortScan.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed Documentation/Images/New-IPv4PortScan.png
Binary file not shown.
9 changes: 4 additions & 5 deletions README.md
Expand Up @@ -8,11 +8,11 @@ This powerful asynchronus IPv4 Port Scanner allows you to scan every Port-Range

The result will contain the Port number, Protocol, Service name, Description and the Status.

![Screenshot](Documentation/Images/New-IPv4PortScan.png?raw=true "New-IPv4PortScan")
![Screenshot](Documentation/Images/IPv4PortScan.png?raw=true "New-IPv4PortScan")

To reach the best possible performance, this script uses a [RunspacePool](https://msdn.microsoft.com/en-US/library/system.management.automation.runspaces.runspacepool(v=vs.85).aspx). As you can see in the following screenshot, the individual tasks are distributed across all cpu cores:

![Screenshot](Documentation/Images/New-IPv4PortScan_CPUusage.png?raw=true "CPU usage")
![Screenshot](Documentation/Images/IPv4PortScan_CPUusage.png?raw=true "CPU usage")

If you are looking for a module containing this script... you can find it [here](https://github.com/BornToBeRoot/PowerShell)!

Expand All @@ -21,17 +21,16 @@ Maybe you're also interested in my asynchronus [IPv4 Network Scanner](https://gi
## Syntax

```powershell
.\New-IPv4PortScan.ps1 [-ComputerName] <String> [[-StartPort] <Int32>] [[-EndPort] <Int32>] [[-Threads] <Int32>] [[-Force]] [[-UpdateList]] [<CommonParameters>]
.\IPv4PortScan.ps1 [-ComputerName] <String> [[-StartPort] <Int32>] [[-EndPort] <Int32>] [[-Threads] <Int32>] [[-Force]] [[-UpdateList]] [<CommonParameters>]
```

## Example

```powershell
PS> .\New-IPv4PortScan.ps1 -ComputerName fritz.box -EndPort 500
PS> .\IPv4PortScan.ps1 -ComputerName fritz.box -EndPort 500
Port Protocol ServiceName ServiceDescription Status
---- -------- ----------- ------------------ ------
21 tcp ftp File Transfer Protocol [Control] open
53 tcp domain Domain Name Server open
80 tcp http World Wide Web HTTP open
```
22 changes: 12 additions & 10 deletions Scripts/New-IPv4PortScan.ps1 → Scripts/IPv4PortScan.ps1
@@ -1,6 +1,6 @@
###############################################################################################################
# Language : PowerShell 4.0
# Filename : New-IPv4PortScan.ps1
# Filename : IPv4PortScan.ps1
# Autor : BornToBeRoot (https://github.com/BornToBeRoot)
# Description : Powerful asynchronus IPv4 Port Scanner
# Repository : https://github.com/BornToBeRoot/PowerShell_IPv4PortScanner
Expand All @@ -16,11 +16,10 @@
The result will contain the Port number, Protocol, Service name, Description and the Status.
.EXAMPLE
.\New-IPv4PortScan.ps1 -ComputerName fritz.box -EndPort 500
.\IPv4PortScan.ps1 -ComputerName fritz.box -EndPort 500
Port Protocol ServiceName ServiceDescription Status
---- -------- ----------- ------------------ ------
21 tcp ftp File Transfer Protocol [Control] open
53 tcp domain Domain Name Server open
80 tcp http World Wide Web HTTP open
Expand Down Expand Up @@ -268,10 +267,13 @@ Process{
$Status = "Closed"
}

[pscustomobject] @{
Port = $Port
Protocol = "tcp"
Status = $Status
if($Status -eq "Open")
{
[pscustomobject] @{
Port = $Port
Protocol = "tcp"
Status = $Status
}
}
}

Expand Down Expand Up @@ -327,7 +329,7 @@ Process{
$Jobs_ToProcess = $Jobs | Where-Object {$_.Result.IsCompleted}

# If no jobs finished yet, wait 500 ms and try again
if($Jobs_ToProcess -eq $null)
if($null -eq $Jobs_ToProcess)
{
Write-Verbose -Message "No jobs completed, wait 500ms..."

Expand All @@ -348,7 +350,7 @@ Process{

Write-Progress -Activity "Waiting for jobs to complete... ($($Threads - $($RunspacePool.GetAvailableRunspaces())) of $Threads threads running)" -Id 1 -PercentComplete $Progress_Percent -Status "$Jobs_Remaining remaining..."

Write-Verbose -Message "Processing $(if($Jobs_ToProcess.Count -eq $null){"1"}else{$Jobs_ToProcess.Count}) job(s)..."
Write-Verbose -Message "Processing $(if($null -eq $Jobs_ToProcess.Count){"1"}else{$Jobs_ToProcess.Count}) job(s)..."

# Processing completed jobs
foreach($Job in $Jobs_ToProcess)
Expand All @@ -361,7 +363,7 @@ Process{
$Jobs.Remove($Job)

# Check if result is null --> if not, return it
if($Job_Result -ne $null -and $Job_Result.Status -eq "Open")
if($Job_Result.Status)
{
if($AssignServiceWithPort)
{
Expand Down

0 comments on commit 96ea87b

Please sign in to comment.