【问题标题】:Unable to ping multiple IP addresses from file无法从文件 ping 多个 IP 地址
【发布时间】:2017-01-19 18:14:21
【问题描述】:

我有一个文件 ip.txt 文件,其中包含 IP 地址列表。我想对文件中的每个地址执行 ping 操作并永远重复此操作。但我的脚本只 ping 最后一行中包含的地址(见下面的输出)。如何修改我的脚本来解决这个问题?

import cmd
import time
import sys
import os

my_file = open("ip.txt","rb")
for line in my_file:
        l = [i.strip() for i in line.split(' ')]
        IP = l[0]

def Main():

    while True:
        ping = os.system("ping", "-c", "1", "-n", "-W", "2", IP)
        if ping:
            print IP 'no connection'
            CT =time.strftime("%H:%M:%S %d/%m/%y")
            alert=' No Connection'
            with open('logfile.txt','a+') as f:
                f.write('\n'+CT)
                f.write(alert)

        time.sleep(4)

if __name__ == "__main__":
    Main()

输出:

[root@localhost PythonScript]# python pingloop.py
PING 192.168.1.100 (192.168.1.100) 56(84) bytes of data.
64 bytes from 192.168.1.100: icmp_seq=1 ttl=128 time=0.655 ms
64 bytes from 192.168.1.100: icmp_seq=2 ttl=128 time=1.15 ms
64 bytes from 192.168.1.100: icmp_seq=3 ttl=128 time=1.14 ms
64 bytes from 192.168.1.100: icmp_seq=4 ttl=128 time=0.529 ms
64 bytes from 192.168.1.100: icmp_seq=5 ttl=128 time=0.538 ms

--- 192.168.1.100 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4006ms
rtt min/avg/max/mdev = 0.529/0.805/1.156/0.287 ms
PING 192.168.1.100 (192.168.1.100) 56(84) bytes of data.
64 bytes from 192.168.1.100: icmp_seq=1 ttl=128 time=0.476 ms
64 bytes from 192.168.1.100: icmp_seq=2 ttl=128 time=0.416 ms
64 bytes from 192.168.1.100: icmp_seq=3 ttl=128 time=0.471 ms
64 bytes from 192.168.1.100: icmp_seq=4 ttl=128 time=0.478 ms
64 bytes from 192.168.1.100: icmp_seq=5 ttl=128 time=0.574 ms

ip.txt文件:

192.168.1.91
192.168.1.92
192.168.1.93
192.168.1.94
192.168.1.95
192.168.1.96
192.168.1.97
192.168.1.98
192.168.1.99
192.168.1.100

【问题讨论】:

  • 您使用IP = l[0] 语句多次分配给IP,所以最后读取的值就是它的值。可能应该创建一个它们的列表,然后处理列表中的每一个,或者修改你的代码以读取一行并立即处理它。
  • 因为您似乎还想跟踪输出,您可能需要为每个 IP 使用单独的线程进行多线程处理。类似于for line in my_file{ thread = PingThread(ip) }PingThread 类中的def run(self){ while(true){ ping = os.system("ping", "-c", "1", "-n", "-W", "2", self.IP); if(ping){....}/*end if*/ }/*end while*/ }/*end run()*/。 (完全是伪代码,但这将是我将采用的基本方法。)如果你对它们不并发运行没问题,你可以创建一个列表并在它上循环无限顺序地运行它们。

标签: python loops while-loop ping


【解决方案1】:
for line in my_file:
        l = [i.strip() for i in line.split(' ')]
        IP = l[0]

在这里,您在每次迭代中覆盖以前读取的 IP 地址与新地址。所以最后,正如您所观察到的,您只有最后一个地址。

相反,构建一个列表地址:

addresses = []
for line in my_file:
    IP = line.split()[0].strip()
    addresses.append(IP)

或者干脆

addresses = [line.split()[0].strip() for line in my_file]

稍后,您必须在地址列表上添加一个额外的循环。而不是:

while True:
    ping = os.system("ping", "-c", "1", "-n", "-W", "2", IP)
    # etc.

while True:
    for IP in addresses:
        ping = os.system("ping", "-c", "1", "-n", "-W", "2", IP)
        # etc.

【讨论】:

    【解决方案2】:

    IP定义为全局列表变量,并将所有ip地址添加到该列表中,然后遍历该列表:

    IP = []
    for line in my_file: 
        l = [i.strip() for i in line.split(' ')] 
        IP.append(l[0])
    
    # instead while use for 
    for ip in IP: 
        ping = os.system("ping", "-c", "1", "-n", "-W", "2", ip)
        ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多