One of the challenging tasks while working in the project team is to perform the necessary connectivity test though services don’t exist.
This is often you have to do when you are working in a DMZ environment for migration or new projects. Let’s take a real-time example – you are working on migration, and you have to ensure connectivity exists between application “A” to “B” on a particular port.
Well. It’s straightforward you can perform
telnet but how about when “B” doesn’t have any service running? That’s where you need the port listener to help in this situation.
If you have a similar situation or feel this would be beneficial for you at work, then here are few ways to achieve this in Windows or UNIX platform.
To create post listener in Windows OS
To have a port listener on a specific port in Windows, you can use the “Port Listener” utility.
This utility is available for free for Windows 95 to Windows 10.
- Download Post Listener as zip or exe format from here
- In this guide, I will download the exe format
- Double click on downloaded postlistener.exe file
- It will prompt to select the location to extract the files, click on unzip
- Go to path where you have extracted the files, in this example; I have at c:listener
- Double click on listener to start the utility
- Enter the port number which you want to test and click on start
- In the above example, I have started listening port on 5500, and it’s time to validate if it’s running.
Open a command prompt and run netstat to validate if port 5500 is listening
So yes, now I have created a port listener successfully in Windows.
To create post listener in Linux OS
The procedure is slightly different in Linux; here we will use netcat (nc) command to start the listener.
- To install nc, you can use yum command
yum install nc
- Once installed, use the following command to start the port listener as 5500 in the background.
nc –l 5500 &
- To validate, let’s use netstat command
[root@Chandan ~]# netstat -anlp |grep 5500 tcp 0 0 0.0.0.0:5500 0.0.0.0:* LISTEN 21085/nc [root@Chandan ~]#
So here I have port 5500 listened successfully. Doing this in Linux is slightly more comfortable, isn’t it?
To create a port listener using Python
The above two examples are limited to OS. How about having a python script that can work on Windows or UNIX?
Well, I found the below python code which works on Windows and Linux both. Create a file – let’s say portlistener.py with below code
''' 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) + ' Message ' + msg 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 + ':' + str(addr) s.close()
Save the file and run it with python command as shown below
[root@Chandan ~]# python portlistener.py Socket created Socket bind complete Socket now listening
Interested in learning Python? Check out this online course.
I hope the above procedure helps you to create a port listener for the connectivity tests.