【问题标题】:Determine OS of remote machine using pings使用 ping 确定远程机器的操作系统
【发布时间】:2019-04-23 17:00:48
【问题描述】:

我想弄清楚如何找到远程机器的操作系统 正在使用 ping 方法运行。

我不知道如何获取 TTL 编号并查看列表以查找它使用的操作系统。

ping 19.217.64.1
Pinging 19.217.64.1 with 32 bytes of data:
Reply from 19.217.64.1: bytes=32 time=3ms TTL=254
Reply from 19.217.64.1: bytes=32 time=3ms TTL=254
Reply from 19.217.64.1: bytes=32 time=5ms TTL=254
Reply from 19.217.64.1: bytes=32 time=4ms TTL=254

Ping statistics for 19.217.64.1:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 3ms, Maximum = 5ms, Average = 3ms

【问题讨论】:

    标签: python python-3.x sockets operating-system ping


    【解决方案1】:

    这里有一个python中ping的实现:

    https://gist.github.com/chidea/955cea841e5c76a7e5ee8aa02234409d

    看看receive_ping函数。 TTL 是接收到的数据包的第 9 个字节(索引 8)。所以我在那个函数中打印了 TTL:

    def receive_ping(my_socket, packet_id, time_sent, timeout):
    # Receive the ping from the socket.
    time_left = timeout
    while True:
        started_select = time.time()
        ready = select.select([my_socket], [], [], time_left)
        how_long_in_select = time.time() - started_select
        if ready[0] == []: # Timeout
            return
        time_received = time.time()
        rec_packet, addr = my_socket.recvfrom(1024)
        print ("TTL:", rec_packet[8]) # IT PRINT THE TTL! HAVE A NICE DAY :)
        icmp_header = rec_packet[20:28]
        type, code, checksum, p_id, sequence = struct.unpack(
            'bbHHh', icmp_header)
        if p_id == packet_id:
            return time_received - time_sent
        time_left -= time_received - time_sent
        if time_left <= 0:
            return
    

    【讨论】:

    • 它适用于大多数情况,但它会遇到麻烦 = select.select([my_socket], [], [], time_left) 并说它必须是一个 int 或有一个 fileno() 方法
    • 您使用 Windows 吗?如果是,请阅读:stackoverflow.com/questions/33777308/…
    猜你喜欢
    • 2014-04-01
    • 2012-07-13
    • 2021-10-31
    • 2019-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-26
    相关资源
    最近更新 更多