【问题标题】:multiple ping to hosts using python with linux使用 python 和 linux 对主机进行多次 ping
【发布时间】:2021-08-25 14:10:51
【问题描述】:

我有一些问题。 我在 Linux 中,我需要使用 python 一次检查 254 pc 的 ping 例如:

我的 IP 地址是 10.1.1.1 ... 10.1.1.254 我只需要检查我是否使用 python 成功 ping 到这台电脑。

所以总输出应该是:

电脑 10.1.1.1 Ping

电脑 10.1.1.2 不 Ping

电脑 10.1.1.3 Ping

直到 10.1.1.254

【问题讨论】:

    标签: python linux networking ping


    【解决方案1】:
    import subprocess
    
    
    ips = ["192.168.1.{}".format(i) for i in range(1,255)]
    mainList = set()
    def ping(ips):
        for num,i in enumerate(ips):
            print(f'Searching Host {i}    Itration Number {num}')
            try:
                p = subprocess.Popen(['ping', '-c1', i], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                p.communicate(timeout=(0.1))
            except subprocess.TimeoutExpired:
                pass
            else:
                print(f'***Host found - Add to array***')
                mainList.add(i)
        print(mainList)
    
    ping(ips)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-20
      • 1970-01-01
      • 2012-02-15
      • 2015-05-21
      • 2017-04-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多