How To: Easily Find What Port a Service Runs On and Vice-Versa

Easily Find What Port a Service Runs On and Vice-Versa

How to Easily Find What Port a Service Runs On and Vice-Versa

I wrote a python script that allows you to easily ask what service runs on a specific port and vice-versa. Now, I am sure that such a program already exists on our lovely linux OS's, but... I didn't think of that before I started, and haven't found it yet. So if you haven't found it yet either, maybe you'll find this of some use.

It's just called getport. If I have a port number, and want to know what services run on that port, I do:

getport 1234 (or whatever number)

and if I have the name of a service, or a keyword (sql, oracle, ssh etc.) I can do:

getport mysql
OR
getport ssh etc..

If there is no match for your search, the script simply does nothing..

Get the script:

You can get it here:

http://pastebin.com/q5Ae0gRj

When you get it you'll need to make it executable like this:
go to the directory it's in
cd ~/directory/of/script (wherever it is)
make it executable
sudo chmod +x getport
And preferably put it somewhere on your system PATH so you can call it from anywhere.

And ready to go. Thanks for reading!

Extra stuff - What it's doing:

It's basically just looking for your port/service/keyword, in the file /etc/unicornscan/ports.txt which is on my kali linux by default, and I assume is also on yours.. thus I have hardcoded that path into the script. If you don't have it though, or if you want this to work on other linux OS's, there is a file: /etc/services which looks very similar to the unicorn one, except seemlingly far more established on linux systems, seeing as it has it's own manpage(you want man services...). However, I preferred the layout of the ports.txt for this, but the script could easily be changed to use /etc/services instead.

6 Comments

Lol, I just shut down my Kali right before you posted this.

+1 Kudos Like Follow Favorite Retweet

Cool script. Even if something like this was already out there, it is always fun to create or recreate something yourself. +1

Edit:

BTW, just a suggestion here but you should put your name in the top of the script so we know who the author is. Especially if someone wanted to use it somewhere and give credit

Thanks both of you ;)

When i need a specific port, it doesn't do just that but pulls along services that have the same input which is kind-off not cool.

For Eg:

When i need a port running on number 4, getport.py 4 : It pulls along other services that have number 4 in their service port number. Can you make it pull just port 4 and not other 4's in other services

? Any help with that ? Awesome post and nice thinking any way...

There is a linux command to do this. It is as follows:

  • netstat -lpn | grep <port/service>

But great job on the script!

Another one with the PC-Name "Deep Thought"... Douglas Adams sure is popluar, and that for good reason.

Thanks for the script. Jinx' command is a bit messier, I would say. Yours is quite clean, which I appreciate.

Share Your Thoughts

  • Hot
  • Latest