79

Introduction: I have created a bash function that is able to check whether a port is available and increments it by 1 if false until a certain maximum port number. E.g., if port 500 is unavailable then the availability of 501 will be checked until 550.

Aim: In order to test this bash function I need to create a range of ports that are in LISTEN state.

Attempts: On Windows it is possible to create a LISTEN port using these PowerShell commands:

PS C:\Users\u> netstat -nat | grep 1234
PS C:\Users\u> $listener = [System.Net.Sockets.TcpListener]1234
PS C:\Users\u> $listener.Start();
PS C:\Users\u> netstat -nat | grep 1234
TCP    0.0.0.0:1234           0.0.0.0:0              LISTENING       InHost
PS C:\Users\u> $listener.Stop();
PS C:\Users\u> netstat -nat | grep 1234
PS C:\Users\u>

Based on this I was trying to think about a command that could do the same on CentOS, but I do not know why and I started to Google without finding a solution that solves this issue.

Expected answer: I will accept and upvote the answer that contains a command that is able to create a LISTEN port and once the command has been run the port should stay in LISTEN state, i.e.:

[user@host ~]$ ss -nat | grep 500
LISTEN     0      128                       *:500                       *:*
030
  • 1,557
  • On a side note: it's possible to find the listening ports without going through all this dance. On Linux: netstat -an --tcp | awk '/LISTEN/ {sub(".*:", "", $4); print $4}' | sort -nu. On BSD: `netstat -an -f inet -p tcp | awk '/LISTEN/ {sub(".\.", "", $4); print $4}' | sort -nu`. – lcd047 Jul 08 '15 at 03:48

6 Answers6

86

you can create a port listener using Netcat .

root@ubuntu:~# nc -l 5000

you can also check if port is open or not using netstat command .

root@vm-ubuntu:~# netstat -tulpen | grep nc
tcp        0      0 0.0.0.0:5000             0.0.0.0:*               LISTEN      0          710327      17533/nc

you can also check with nc :

Netcat Server listener :

nc -l localhost 5000

Netcat Client :

root@vm-ubuntu:~# nc -v localhost 5000
Connection to localhost 5000 port [tcp/*] succeeded!

if port is not open

root@vm-ubuntu:~# nc -v localhost 5000
nc: connect to localhost port 5000 (tcp) failed: Connection refused
sameh Ammar
  • 1,379
66

You could use nc -l as a method to do what you are looking for. Some implementations of nc have a -L option which allows the connections to persist.

If you only need them for a little while you could open this command in a for loop and have a bunch of ports opened that way.

If you need these opened longer you can use one of the super servers to create a daemon.

030
  • 1,557
10

Listen using netcat.

# nc -l 5555

Check using ss

# ss -nat|grep 5555
LISTEN     0      1                         *:5555                     *:*
#
steve
  • 21,892
6

Note that Debian's netcat package has a different implementation where (at least) you need to provide the port via the -p option and the -k option doesn't work. You might run into this issue using Docker.

sudo apt install -y netcat

nc -l -p 1337

You may consider using openbsd-netcat instead where the -k option works.

6

Below python code is helpful to open a dummy port

'''    Simple socket server using threads
'''
import socket
import sys
HOST = ''   # Symbolic name, meaning all available interfaces
PORT = 5500 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'
#Bind socket to local host and port
try:
    s.bind((HOST, PORT))
except socket.error as msg:
    print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
    sys.exit()
print 'Socket bind complete'
#Start listening on socket
s.listen(10)
print 'Socket now listening'
#now keep talking with the client
while 1:
    #wait to accept a connection - blocking call
    conn, addr = s.accept()
    print 'Connected with ' + addr[0] + ':' + str(addr[1])
s.close()

Save the file and run it with python command as shown below

~]# python portlistener.py
Socket created
Socket bind complete
Socket now listening

Then you can verify from required machine.

Rohit
  • 61
1

Below command was immediately exiting without listening on port on RHEL 6 nc -l localhost 5000

For me below command worked where netcat started listening on port. nc -lk localhost 5000