1

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?

muru
  • 193,181
  • 53
  • 473
  • 722
Anony
  • 773
  • 4
  • 10
  • 16

2 Answers2

1

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
Gospot Bog
  • 84
  • 4
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
dessert
  • 39,392
  • 12
  • 115
  • 163