Showing posts with label TCP. Show all posts
Showing posts with label TCP. Show all posts

Thursday, July 25, 2024

Test-NetConnection using custom source port.

Powershell utility to test remote port using a custom source address from your computer.

 

If you have multiple ip addresses assigned on your computer the Test-Netconnection utility does not allow you to test the connection to a remote computer using  one of those addresses. 


The script below addresses that drawback.

 #Usage: .\Test-Connect.ps1 -computername 10.10.10.10 -RemotePort 389 -sourceipAddress 123.23.10.5

param([string]$computername,[string]$RemotePort,[string]$sourceipAddress)
$destHostName = $computername
$destPort     = $RemotePort
$src  = [System.Net.IPEndPoint]::new([ipaddress]::Parse($sourceipAddress),0)
$tc   = [System.Net.Sockets.TcpClient]::new($src)
$tc.Connect($destHostName,$destPort)

if ($tc.Connected) {
    "Connected!"
} else {
    "Not connected"
}

$tc.Dispose()

Monday, May 15, 2023

Reservce dynamic tcp udp port.

 

Show excluded port range:

netsh int ipv4 show excludedportrange tcp


Reserve port:

netsh int ipv4 add excludedportrange protocol=tcp startport=49698 numberofports=1


netsh int ipv6 add excludedportrange protocol=udp startport=63566 numberofports=5

 

Tuesday, April 25, 2017

How to test if a port is open using powershell


Use this Powershell command to to test if a port is open on a server and if you are able to connect to it remotely.

$tcp = New-Object System.Net.Sockets.TcpClient
$tcp.connect('servername or ip address', portnumber)

Or you can use this single line of code
(New-Object System.Net.Sockets.TcpClient).Connect('servername or ip address', portnumber)

There is also an in built powershell cmdlet that lets you test a port connection which only works with powershell 4.0

Test-NetConnection -Port portnumber -Computername 'Servername or ip address'

The first commands are much faster compared to the second one but does not give you any output.
The powershell cmdlet does give you a good output that you can use in another script but is much slower.