Junior
How to check if a port is open on a remote or local host?
sobes.tech AI
Answer from AI
For the local host:
# Check ports listened on the local host
ss -tulnp | grep <port_number>
# Or using netstat (on older systems)
netstat -tulnp | grep <port_number>
For the remote host:
# Check using telnet (if installed)
telnet <remote_host> <port_number>
# Or using nc (netcat)
nc -zv <remote_host> <port_number>
# Or using nmap (if installed)
nmap -p <port_number> <remote_host>
For remote host using Bash without additional utilities (less reliable, may be slow):
# Check if TCP port is open, timeout 1 second
timeout 1 bash -c 'cat < /dev/null > /dev/tcp/<remote_host>/<port_number>' 2>/dev/null && echo "Port <port_number> is open" || echo "Port <port_number> is closed"