【问题标题】:ipscanner python3 (module ipaddress). result list - all hosts offlineip 扫描器 python3(模块 ipaddress)。结果列表 - 所有主机离线
【发布时间】:2015-06-24 19:40:43
【问题描述】:

有扫描指定地址范围的代码:

import os, ipaddress
position_start=input('Start hostname: ')
position_end=input('End hostname: ')
hostname_start = ipaddress.IPv4Address(position_start)
hostname_end = ipaddress.IPv4Address(position_end)
while hostname_start <= hostname_end:
    hostname_response = os.system("ping -c 1 -w 2" + str(hostname_start)+ "> /dev/null 2>&1")
    if hostname_response == 0:
        print (hostname_start, ' is up!\n')
    else:
        print (hostname_start, ' is down!\n')
    hostname_start += 1
input("\nPress the enter key to exit...")

结果它的工作 - 子网中的所有主机都离线

我哪里错了?

【问题讨论】:

    标签: python-3.x


    【解决方案1】:

    您的主要问题是格式化字符串。 "ping -c 1 -w 2"末尾没有空格,应用程序找不到IP地址。这是工作示例

    import os
    import ipaddress
    
    position_start = input('Start hostname: ')
    position_end = input('End hostname: ')
    hostname_start = ipaddress.IPv4Address(position_start)
    hostname_end = ipaddress.IPv4Address(position_end)
    
    
    while hostname_start <= hostname_end:
        hostname_response = os.system("ping -c 1 -w 2 {0} > /dev/null 2>&1".format(hostname_start))
        if hostname_response == 0:
            print(hostname_start, ' is up!\n')
        else:
            print(hostname_start, ' is down!\n')
        hostname_start += 1
    
    input("\nPress the enter key to exit...")
    

    请注意,此示例仅适用于 Linux,因为 *BSD 和 OS X 不支持 -w ping 选项。

    【讨论】:

    • 谢谢! Спасибо! Я невнимателен
    • 另请注意,在现实中使用shlex.quote 的转义shell 参数。
    猜你喜欢
    • 2017-11-26
    • 2014-09-07
    • 1970-01-01
    • 1970-01-01
    • 2021-11-05
    • 1970-01-01
    • 2018-08-17
    • 1970-01-01
    • 2016-08-24
    相关资源
    最近更新 更多