python - fping 在 python3 中使用 IP 列表

标签 python linux python-3.x centos7 ping

我是 python 的新手,我正在尝试使用 fping 扫描多个 IP。我有大量服务器要监视统计信息,我可以编写适用于 2 台主机的以下代码:

 
    import subprocess
    import binascii

    out = ''
    err = ''

    host1 = '172.2.2.5'
    host2 = '172.1.5.8'
    hosts = '/tmp/ip.list'

    ping = subprocess.Popen(['fping', '-C', '10', '-b', '50', '-p', '25', host1, host2], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    try:             
        [out, err] = ping.communicate(timeout=15)

        print('STDOUT')
        print(out.decode('utf-8').strip())

        print('STDERR (Statistics)')
        print(err.decode('utf-8').strip())

    except subprocess.TimeoutExpired:
        print('STDERR (expired timeout)')

但是,尽管我尝试了多种方法,但我无法用主机文件列表替换这两个主机。

我将非常感谢这方面的任何帮助或程序本身的任何改进。

TIA。

最佳答案

我找到了一个简单的修复方法:


with open('ip_list.csv', 'r') as fh:
    all_ips = fh.read().splitlines()

cmd = ['fping', '-C', '10', '-b', '50', '-p', '25']
cmd.extend(all_ips)

ping = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

关于python - fping 在 python3 中使用 IP 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56312105/

相关文章:

python - Python 中 "import as"的用例

python - Keras 训练 CNN - 我应该将热图数据转换为图像还是 2D 矩阵

python - 参数值列表的 Lambda 函数

python-3.x - TensorFlow:实现均方误差

python - 不断调用函数直到满足条件 Python

python - 字符串操作 : partly convert to lowercase

linux - linux下如何通过编译源码安装devels

python - 在 subprocess.Popen 中使用 && 进行命令链接?

java - JMX:MaxFileDescriptorCount 和 OpenFileDescriptorCount

python - 如何从 print() 编写的字符串中获取 Python pandas DataFrame?