python - 尝试使用 python 对多台电脑执行 ping 操作。 。

标签 python python-3.x subprocess ping

我正在尝试读取一个包含 300 台计算机的 ip 的文件,然后写入一个新文件(如果每个文件存在或不存在)。

import os
import datetime
import platform
import subprocess

date = datetime.datetime.now()
day = date.day
hour = date.hour

os.chdir ('Path')
openips = open ("ips.txt","r")
ipfile = openips.readlines()
print (ipfile)

for ips in ipfile():
    ips = ips.strip()
    print (ips)
    args = ["ping", "-n", "4", "-l", "1", "-w", "1000", ips]
    pping = subprocess.Popen(args, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
    for line in pping.stdout:
        print (line)
    os.chdir ('Path')
    presult = open ("pingresults_{}_{}.txt".format(day,hour), 'a')
    presult.write ('{}_{}\n'.format(ips, line))
    presult.close ()

不知道为什么,每次我测试我的代码时。 。 。结果是:

TypeError: 'list' object is not callable

我已经尝试了一切,即使我将变量更改为 string 。 。 。说同样的,只是改变字符串列表

最佳答案

更改:

for ips in ipfile:
   ...

没有括号

关于python - 尝试使用 python 对多台电脑执行 ping 操作。 。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52574698/

相关文章:

python - 这两个函数中哪个更多 "pythonic"?

python - 如何填写 list

python - 验证失败后从空表单数据中检索空字符串

python - 使用 Python 检查子进程是否在 Linux 中运行

python - 用 C 模块扩展 python

Python线程转储

python - Tkinter 破坏 Toplevel 并产生另一个错误

python - 覆盖导入模块中导入的类

python - 在 python 中使用重定向运行 bash 命令

python - 与python中子进程打开的 `/dev/tty`交互