Home > Net >  Finding the appropriate port range
Finding the appropriate port range

Time:04-21

I can access the ports used on the server with the cat /etc/services command.

tcpmux      1/tcp               # TCP port service multiplexer
echo        7/tcp
echo        7/udp
discard     9/tcp       sink null
discard     9/udp       sink null
systat      11/tcp      users
daytime     13/tcp
daytime     13/udp
netstat     15/tcp
....
....
....

But I get a long output. But by further customising this search, I need to find out if ports in a certain number range are available.

For example, if there is any port between 10000 and 11000 ports by a service using, how can I check it?

CodePudding user response:

Let me give you a very ugly answer:

grep "10[0-9][0-9][0-9]" /etc/services

The regular expression means that your port number starts with "10", followed by three digits [0-9].

Obviously when you are interested in other ranges (like between 12345 and 54321), this dirty trick won't work and you'll be forced to turn to proper tools, like awk.

Edit
My answer shows how to get the ports from 10000 up to 10999, but in order to include 11000 too, you might opt for:

egrep "10[0-9][0-9][0-9]|11000" /etc/services
  • Related