#!/bin/bash
#端口、IP、间隔时间信息
PORT="" 
IP=""
sleep_time=""


#设置完这些信息最后运行这条条命令执行即可：  nohup /root/check_port.sh > /root/out.log 2>&1 &


#创建对应文件
touch /root/port_status_all.log
touch /root/port_status_open.log
touch /root/port_status_closed.log
# 无限循环检测端口 
while true; do
    # 使用 netcat 检测端口 
    nc -zv $IP $PORT > /dev/null 2>&1 

    # 检查 netcat 命令的退出状态 
    if [ $? -eq 0 ]; then 
       echo "$(date): Port $PORT on $IP is open." >> /root/port_status_all.log
       echo "$(date): Port $PORT on $IP is open." >> /root/port_status_open.log 
    else 
       echo "$(date): Port $PORT on $IP is closed."  >> /root/port_status_all.log
       echo "$(date): Port $PORT on $IP is closed." >> /root/port_status_closed.log
    fi 
   
    # 每隔 XX秒检测一次 
    sleep $sleep_time
done 

