0

This question already has an answer here:

I have a specific port number with me and want to check if it is active or not. I tried:

netstat -an | grep $portnum

Is this correct?

marked as duplicate by muru, Eric Carvalho, Charles Green, Zanna command-line Nov 21 '17 at 14:10

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

0

You can scan for open ports with nmap

nmap {ip addr}

Like so:

nmap 192.168.0.1

The output should be something like this:

Not shown: 998 closed ports
PORT    STATE SERVICE

8080/tcp  open  http

9020/tcp open  iss-realsecure
0

To test for a specific port on your machine you can use nmap with your own IP (obtained by hostname -I) and grep for the port number:

portnum=22
ip=$(hostname -I)
nmap $ip | grep -Eq ^$portnum/ && echo yes || echo no

or with if:

if nmap $ip | grep -Eq ^$portnum/; then
  echo yes
else
  echo no
fi

Not the answer you're looking for? Browse other questions tagged or ask your own question.