Sample Header Ad - 728x90

Given a service name, get its port number?

7 votes
2 answers
2420 views
Hi I feel like this is an obvious question but I haven't been able to get a good answer so far. Given the name of the service (which I know running on localhost) is there any networking command line tool like (netstat/ss) which will tell me what port that service is running at? Ideally something like:
$ some-program --service-name='mysql' localhost
'mysql' is running at localhost:3306
I feel like there are solutions out there but non of them address it adequately. For example I have considered the following two ss commands: 1. ss -tuln with output:
Netid   State    Recv-Q   Send-Q       Local Address:Port        Peer Address:Port   Process
udp     UNCONN   0        0            127.0.0.53%lo:53               0.0.0.0:*
udp     UNCONN   0        0                  0.0.0.0:21119            0.0.0.0:*
udp     UNCONN   0        0                  0.0.0.0:37766            0.0.0.0:*
udp     UNCONN   0        0                  0.0.0.0:54399            0.0.0.0:*
udp     UNCONN   0        0                  0.0.0.0:5353             0.0.0.0:*
udp     UNCONN   0        0                     [::]:51755               [::]:*
udp     UNCONN   0        0                     [::]:5353                [::]:*
udp     UNCONN   0        0                        *:1716                   *:*
tcp     LISTEN   0        100              127.0.0.1:25               0.0.0.0:*
tcp     LISTEN   0        70               127.0.0.1:33060            0.0.0.0:*
tcp     LISTEN   0        64                 0.0.0.0:59687            0.0.0.0:*
tcp     LISTEN   0        151              127.0.0.1:3306             0.0.0.0:*
and 2. ss -tul with output:
Netid   State    Recv-Q   Send-Q      Local Address:Port         Peer Address:Port   Process
udp     UNCONN   0        0                 0.0.0.0:36308             0.0.0.0:*
udp     UNCONN   0        0                 0.0.0.0:36570             0.0.0.0:*
udp     UNCONN   0        0           127.0.0.53%lo:domain            0.0.0.0:*
udp     UNCONN   0        0                 0.0.0.0:41124             0.0.0.0:*
udp     UNCONN   0        0                 0.0.0.0:21119             0.0.0.0:*
udp     UNCONN   0        0                 0.0.0.0:37766             0.0.0.0:*
udp     UNCONN   0        0                 0.0.0.0:54399             0.0.0.0:*
udp     UNCONN   0        0                 0.0.0.0:mdns              0.0.0.0:*
udp     UNCONN   0        0                 0.0.0.0:54522             0.0.0.0:*
udp     UNCONN   0        0                    [::]:51755                [::]:*
udp     UNCONN   0        0                    [::]:mdns                 [::]:*
udp     UNCONN   0        0                       *:1716                    *:*
tcp     LISTEN   0        100             127.0.0.1:smtp              0.0.0.0:*
tcp     LISTEN   0        70              127.0.0.1:33060             0.0.0.0:*
tcp     LISTEN   0        64                0.0.0.0:59687             0.0.0.0:*
tcp     LISTEN   0        151             127.0.0.1:mysql             0.0.0.0:*
The first command's output lists the port numbers that are listening while the second command's output is able to resolve them to the services running at the ports. But I can't somehow "combine" the two outputs where I can have the port number mapped to the service running, side by side. For example the rows:
tcp     LISTEN   0        151             127.0.0.1:mysql             0.0.0.0:*
and
tcp     LISTEN   0        151              127.0.0.1:3306             0.0.0.0:*
would be "combined" to give "127.0.0.1:3306 (mysql)" or something to that effect. I only know the above mapping because I googled what the default MySQL port is. Is there a way to do this? It must be said that I am only learning to use these networking tools so any guidance is much appreciated.
Asked by First User (345 rep)
Oct 17, 2023, 03:46 PM
Last activity: Oct 19, 2023, 12:58 PM