Skip to content

Instantly share code, notes, and snippets.

@ericwastaken
Created March 29, 2024 01:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericwastaken/8fc57239144f6402963f39fb956bcf33 to your computer and use it in GitHub Desktop.
Save ericwastaken/8fc57239144f6402963f39fb956bcf33 to your computer and use it in GitHub Desktop.
NetCat (nc) very simple clone on Powershell
<#
.SYNOPSIS
Checks if a specified port on a given hostname is open or closed, with a simplified output.
.DESCRIPTION
This script uses the Test-NetConnection cmdlet to attempt a TCP connection to a specified hostname and port.
It then checks the result of this attempt to determine if the port is open (indicating the host is listening on that port)
or if the port is closed/not reachable. The result is printed to the console with a color-coded message.
.PARAMETER hostname
The hostname or IP address you want to check the port status for.
.PARAMETER port
The port number you want to check the status of on the specified hostname.
.EXAMPLE
.\ScriptName.ps1 -hostname "example.com" -port 80
Checks if port 80 is open on example.com and prints the result.
.NOTES
Author: [Your Name]
Version: 1.0
Last Modified: [Date]
#>
param(
# The hostname or IP address to check.
[string]$hostname,
# The port number to check on the hostname.
[int]$port
)
# Attempt a TCP connection using Test-NetConnection with detailed information level.
$result = Test-NetConnection -ComputerName $hostname -Port $port -InformationLevel Detailed
# Check if the TCP test succeeded (port is open) and print a success message.
if ($result.TcpTestSucceeded) {
Write-Host "${hostname}:$port is open" -ForegroundColor Green
} else {
# If the TCP test failed (port is closed or not reachable), print a failure message.
Write-Host "${hostname}:$port is closed or not reachable" -ForegroundColor Red
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment