Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· OpenAIAi2 WildChat-4.8M (AI2)

帮我写一个ping 192.168.1.0整个网段的脚本,并返回结果

  1. User

    帮我写一个ping 192.168.1.0整个网段的脚本,并返回结果

  2. Assistant

    以下是一个Python脚本,用于ping 192.168.1.0网段中的所有IP地址,并返回每个IP的ping结果。

    import os
    import platform
    import subprocess
    
    def ping_ip(ip_address):
        param = "-n" if platform.system().lower() == "windows" else "-c"
        
        try:
            subprocess.check_output(["ping", param, "1", ip_address])
            return True
        except subprocess.CalledProcessError:
            return False
    
    def main():
        network_prefix = "192.168.1."
        for i in range(1, 255):
            ip = network_prefix + str(i)
            if ping_ip(ip):
                print(f"{ip} is online")
            else:
                print(f"{ip} is offline")
    
    if __name__ == "__main__":
        main()
    

    当前脚本将会在Windows和Linux平台上运行,根据不同平台自动切换ping命令参数。当执行脚本时,将会尝试ping网段192.168.1.1至192.168.1.254的所有IP地址,并输出每个IP的在线或离线状态。

    请注意,ping的结果可能受到防火墙、网络策略以及设备设置的影响。某些设备可能禁用了ICMP协议(ping所使用的协议),导致它们即使在线也无法ping通。

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.