I want to see if something is listening on a port on localhost. I was going to use nc
and check the exit code.
Something like this:
echo "" | nc localhost 14881
echo $?
Any other suggestions?
I want to see if something is listening on a port on localhost. I was going to use nc
and check the exit code.
Something like this:
echo "" | nc localhost 14881
echo $?
Any other suggestions?
Maybe netstat would be better because the port might not be listening on localhost or it might be blocked by iptables:
netstat -ln | grep :14881
echo $?
Grep will exit with 1 if there is no match. If you want just tcp and/or udp , add the -u or -t switches to netstat.
If you are root:
netstat -lnp | grep ':14881 '
I use this in bash for exiting when no-one listening to this port.
$port="14881"
if [[ $(netstat -ltn | grep ":${port} " | wc -l) -eq "0" ]] ; then echo "Port $port not listened to" && exit 1; fi
netstat -ano | egrep LISTEN | egrep tcp | egrep $PORTNUMBER
sudo ss -lntup
will tell you what is listening for TCP/UDP connections - you can filter the output as might be suitable (e.g. listening on 127.0.0.1, ::1, 0.0.0.0).
If you have python around, you can also create a dumb portscanner (only really worthwhile for TCP, though):
import socket
for p in range(1,2**16):
try:
s = socket.create_connection(('127.0.0.1',p))
print "Listener on tcp/{}".format(p)
except:
continue