Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check Network port access and display useful message?

I was trying to check whether the port is opened or not using powershell like follows.

(new-object Net.Sockets.TcpClient).Connect("10.45.23.109", 443) 

This method works , but the output is not user-friendly. It means if there are no errors then it has access. Is there any way to check for success and display some message like " Port 443 is operational"?

like image 613
Samselvaprabu Avatar asked Mar 05 '12 11:03

Samselvaprabu


People also ask

How can I see what ports are open on my network?

One of the simplest ways to check for open ports is to use NetStat.exe. You can find this tool in the System32 folder on Windows 10. With NetStat, you can see open ports or ports that a specific host uses. Netstat is short for network statistics.

How do I check if a port is open?

Type "Network Utility" in the search field and select Network Utility. Select Port Scan, enter an IP address or hostname in the text field, and specify a port range. Click Scan to begin the test. If a TCP port is open, it will be displayed here.

How do I know if port 443 is open?

You can use netstat command to list the tcp port, if 443 port is listed there and state is established means 443 is open for outbound communication.

How do I test a port?

On a Windows computerPress the Windows key + R, then type "cmd.exe" and click OK. Enter "telnet + IP address or hostname + port number" (e.g., telnet www.example.com 1723 or telnet 10.17.xxx.xxx 5000) to run the telnet command in Command Prompt and test the TCP port status.


2 Answers

If you're running Windows 8/Windows Server 2012 or newer, you can use the Test-NetConnection command in PowerShell.

Ex:

Test-NetConnection -Port 53 -ComputerName LON-DC1 
like image 84
SimonOzturk Avatar answered Sep 19 '22 23:09

SimonOzturk


I improved Salselvaprabu's answer in several ways:

  1. It is now a function - you can put in your powershell profile and use anytime you need
  2. It can accept host as hostname or as ip address
  3. No more exceptions if host or port unavaible - just text

Call it like this:

Test-Port example.com 999 Test-Port 192.168.0.1 80 

function Test-Port($hostname, $port) {     # This works no matter in which form we get $host - hostname or ip address     try {         $ip = [System.Net.Dns]::GetHostAddresses($hostname) |              select-object IPAddressToString -expandproperty  IPAddressToString         if($ip.GetType().Name -eq "Object[]")         {             #If we have several ip's for that address, let's take first one             $ip = $ip[0]         }     } catch {         Write-Host "Possibly $hostname is wrong hostname or IP"         return     }     $t = New-Object Net.Sockets.TcpClient     # We use Try\Catch to remove exception info from console if we can't connect     try     {         $t.Connect($ip,$port)     } catch {}      if($t.Connected)     {         $t.Close()         $msg = "Port $port is operational"     }     else     {         $msg = "Port $port on $ip is closed, "         $msg += "You may need to contact your IT team to open it. "                                      }     Write-Host $msg } 
like image 21
mshutov Avatar answered Sep 22 '22 23:09

mshutov