检查远程服务器端口是否开启
- 检查远程服务器端口是否开启
- 轮询检查远程服务器所有端口是否开启
代码
import socket
import sys
host = sys.argv[1]
mode = sys.argv[2]
def check_port(host, port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(5)
result = sock.connect_ex((host, port))
if result == 0:
print(f"{port} is open")
else:
print(f"{port} is not open")
sock.close()
def check_ports(host, start_port, end_port):
open_ports = []
for port in range(start_port, end_port + 1):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(1)
result = sock.connect_ex((host, port))
if result == 0:
open_ports.append(port)
sock.close()
return open_ports
if mode=="all":
open_ports = check_ports(host, 1, 65535)
print("Open ports on {}: {}".format(host, open_ports))
else:
port = int(mode)
check_port(host, port)
使用示例
python xx.py [host] [port] //指定端口
python xx.py [host] 'all'//轮询所有端口
|