Introduction:
Have you ever googled a port number to figure out what service uses it? I think every system administrator must have done this. But how to approach when there are a lot of port numbers to search?
So, I thought giving it a try with PowerShell, to programmatical get port information of lots of port numbers.
The Idea and Making it work:
- First get information for all the ports, but where to get it? I read somewhere during my engineering graduation days, that port numbers are assigned by IANA (Internet Assigned Numbers Authority). How about checking their website first.So I found a link on IANA’s website which has port numbers in a CSV format and downloaded the file.
- Once you have the information, filter out all unnecessary things and store them in a CSV or JSON format, so that it can be easily uploaded/downloaded from a Public Github gist.Using the below script I filtered out the required information and pasted/committed it to my Github repository , from where it could be downloaded to any machine since it is Public.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Import-Csv "C:\Users\prateeksingh\Downloads\service-names-port-numbers.csv"|` Where-Object {$_.'Port Number'} |select @{n='Port';e={$_.'Port number'}},` @{n='Service';e={$_.'Service Name'}},` @{n='Protocol';e={$_.'Transport Protocol'}},` 'Description'|ConvertTo-Csv -NoTypeInformation|clip - Write a Powershell function that can
- Web request above mentioned public Github gist URL and downloads the content.
- Filter out port number you’re trying to search from the content, and display the results
Following is the PowerShell function to get above done
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters<# .Synopsis Gets information of a port number .DESCRIPTION Function provides detailed information of port numbers, like - the service which use the port, Transport protocol and a small decsription. .EXAMPLE PS > Get-Port -Port 20,21,53 Port Service Protocol Description ---- ------- -------- ----------- 20 ftp-data tcp File Transfer [Default Data] 20 ftp-data udp File Transfer [Default Data] 20 ftp-data sctp FTP 21 ftp tcp File Transfer Protocol [Control] 21 ftp udp File Transfer Protocol [Control] 21 ftp sctp FTP 53 domain tcp Domain Name Server 53 domain udp Domain Name Server .EXAMPLE PS > 135, 25, 23 | Get-Port Port Service Protocol Description ---- ------- -------- ----------- 135 epmap tcp DCE endpoint resolution 135 epmap udp DCE endpoint resolution 25 smtp tcp Simple Mail Transfer 25 smtp udp Simple Mail Transfer 23 telnet tcp Telnet 23 telnet udp Telnet .EXAMPLE PS > port 389 Port Service Protocol Description ---- ------- -------- ----------- 389 ldap tcp Lightweight Directory Access Protocol 389 ldap udp Lightweight Directory Access P #> Function Get-Port { [CmdletBinding(HelpUri ='https://geekeefy.wordpress.com/2017/05/09/query-port-information-using-powershell/')] [Alias('port')] [OutputType([psobject])] Param ( [Parameter( Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Position=0, HelpMessage="Please provide a port number")] [ValidateRange(0,65536)][int[]]$Port ) Begin { $TempFileName = "$env:TEMP\Ports.csv" If(Test-Path $TempFileName){ $Data = Import-Csv $TempFileName } else{ $Data = Invoke-WebRequest 'https://goo.gl/G1i6RU' | ` ForEach-Object content | ` Tee-Object -FilePath $TempFileName -Verbose | ConvertFrom-Csv } } Process { $Port | ForEach-Object { $pv= $_ $Data.Where({$_.port -eq $pv}) } } End { Remove-Variable -Name Data [gc]::Collect() } }
- Make sure when the Powershell function runs for the first time, it creates a local copy of content in a File at a static location, which could be accessed to get port information locally for all Future function calls by avoiding the Web request which is slower.Took care of point-6 in my PowerShell function like in the screenshot below
Running the Function:
You can run the function on demand like in the below screenshot.
Please Note – The First call to the function would be slow, but from the next call you have a local copy of the CSV file on your machine, which can be queried comparatively much faster to web requests.
Hope you’ll find the script useful and Thanks for reading, Cheers! 🙂
Just wanted to point out that the ‘port’ variable is marked as mandatory, but there is no helpmessage for it.
LikeLiked by 1 person
Fixed that, Thanks for pointing out. Cheers!
LikeLiked by 1 person
Also noticed a typo in your port validaterange code. Should be 65535.
Otherwise, cool function.
LikeLiked by 1 person
You are a saviour! 🙂
LikeLiked by 1 person
[…] on May 8, 2017 submitted by /u/Prateeksingh1590 [link] [comments] Leave a […]
LikeLike
Thank you for sharing superb informations. Your web-site is fantastically cool. I am impressed by the details that you have proceeding this web site. It reveals how nicely you perceive this subject. Bookmarked this web call, will be as long as backside in support of more articles. You, my friend, ROCK! I originate simply the info I previously searched all more than the place and just couldn’t extend athwart. Could you repeat that? an ideal trap site.
LikeLike
You have brought up a very superb points , appreciate it for the post.
LikeLike
[…] Query Port Information using Powershell […]
LikeLike
[…] Query Port Information using Powershell […]
LikeLike
[…] Query Port Information using Powershell […]
LikeLike