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?
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
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