Python 编写的自动验证端口的脚本
以下是一个使用 Python 编写的自动验证端口的脚本示例:
import socket
def connection_host(host, port):
cli = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM)
cli.settimeout(1)
try:
cli.connect((host, int(port)))
cli.close()
#print(host + ":" + str(port) + " success!!")
except Exception as e:
print("检测不通过======")
print(host + ":" + str(port))
# except socket.timeout as scto:
# """
# 处理主机不通
# """
# print(host + ":" + str(port) + scto)
def read_file_tocheck():
with open('host_port.txt','r') as f:
lines = f.readlines()
for line in lines:
data = line.split(":",1)
host = data[0]
port = data[1]
connection_host(host,port)
新建host_port.txt文件,文件内容示例:
127.0.0.1:8080
127.0.0.1:8090
然后执行即可,这样可以快速批量检查端口网络策略是否通。