网络监控脚本可以使用Python编写,主要用于监控网络连接、端口状态、网络流量等信息。以下是一个简单的网络监控脚本示例:

import subprocess
import time

# 定义需要监控的IP地址和端口号
target_ip = '192.168.1.1'
target_port = 80

while True:
    # 使用ping命令检测目标IP是否可达
    ping_result = subprocess.call(['ping', '-c', '1', target_ip])
    if ping_result == 0:
        print('Target IP is reachable')
    else:
        print('Target IP is not reachable')

    # 使用telnet命令检测目标端口是否开放
    telnet_result = subprocess.call(['telnet', target_ip, str(target_port)])
    if telnet_result == 0:
        print('Target port is open')
    else:
        print('Target port is closed')

    # 每隔一段时间进行一次监控
    time.sleep(10)

这个脚本使用了subprocess模块来执行系统命令,通过ping命令检测目标IP是否可达,通过telnet命令检测目标端口是否开放。脚本将会每隔10秒进行一次监控。你可以根据需要自定义监控的IP地址和端口号,并根据实际情况添加更多的监控项


原文地址: https://www.cveoy.top/t/topic/hWWp 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录