Junior — Middle
How can you ensure that a specific port on a remote machine is accessible for connection?
sobes.tech AI
Answer from AI
To check the availability of a specific port on a remote machine, you can use several methods:
- telnet command
telnet <ip-address> <port>
If the connection is established, the port is open.
- nc (netcat) command
nc -zv <ip-address> <port>
The -z option checks only if the port is open without transmitting data, -v provides detailed output.
- Using the nmap utility
nmap -p <port> <ip-address>
Shows the port status (open, closed, filtered).
- Programmatically (e.g., in Python)
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(3) # timeout of 3 seconds
result = sock.connect_ex(('ip-address', port))
if result == 0:
print('Port is open')
else:
print('Port is closed or inaccessible')
sock.close()
Thus, port checking boils down to attempting to establish a TCP connection with the specified address and port.