【问题标题】:If i wanted to extract the ttl and display it from the ping command how could i go about doing that?如果我想提取 ttl 并从 ping 命令中显示它,我该怎么做呢?
【发布时间】:2019-09-13 01:58:05
【问题描述】:

编写脚本并想 ping 网络上的设备,告诉我它是否可达,然后从 ping 中获取 ttl 数据并告诉我操作系统。

我尝试过使用 awk 命令,但我也是脚本新手,可能没有正确使用它。

for host in $(seq 1 255);   
do
    ping -c 1 $sn.$host | grep "Unreachable" &>/dev/null
    if [ $? -eq 0 ]; then
        printf "%s\n" "$sn.$host is Offline"
    fi

    ping -c 1 $sn.$host | grep "ttl" &>/dev/null
    if [ $? -eq 0 ]; then
        printf "%s\n" "$sn.$host is Online"
    fi
done

我需要 ttl 值并将其存储到一个变量中,然后告诉我基于 Linux 的操作系统的 ttl 为 64,windows 的 ttl 为 128,ios 为 255

【问题讨论】:

  • 这可能不是一种可靠的检测方式,尤其是因为 TTL 可以根据网络路径而改变(可以随时改变)。例如,如果我从我的 Ubuntu 虚拟机 ping 主机 Windows,我会得到 127,而我通过从同一个虚拟机 ping 同事的盒子得到 128

标签: bash scripting ping ttl


【解决方案1】:

您可以通过使用-w(或-W)选项设置超时,以更简洁的方式做事并最大限度地减少等待离线主机的时间。例如,您可以将来自pingttl=XX 值保存在确定主机是否在线的同一个调用中,然后您可以使用简单的参数扩展来提取数字ttl等号右侧的值,例如

    ttlstr=$(ping -c1 -w1 $sn.$host | grep -o 'ttl=[0-9][0-9]*')

命令替换$(...) 执行ping 并将输出传递给grep 并将结果分配给ttlstr。命令替换返回是管道中最后一个命令的返回,它告诉您grep for "ttl=####" 是成功还是失败。这就是您确定主机是否在线所需的全部内容。失败时输出您的 "Offline" 消息并尝试下一条,例如

    ## ping with 1 sec timeout store ttl=xx in ttlstr
    ttlstr=$(ping -c1 -w1 $sn.$host | grep -o 'ttl=[0-9][0-9]*') || {
        printf "%s is Offline\n" "$sn.$host"
        continue;
    }

如果命令替换成功,您可以输出“在线”消息,并且可以使用简单的参数扩展来隔离数字ttl,以删除所有字符,包括@ 987654336@ 符号从字符串的开头只留下数字 ttl,例如

    ttl="${ttlstr#*=}"  ## parameter expansion separating numeric ttl
    printf "%s is Online, ttl=%d\n" "$sn.$host" "$ttl"

总之你可以做到:

#!/bin/bash

sn=${1:-192.168.6}

for host in $(seq 1 255); do
    ## ping with 1 sec timeout store ttl=xx in ttlstr
    ttlstr=$(ping -c1 -w1 $sn.$host | grep -o 'ttl=[0-9][0-9]*') || {
        printf "%s is Offline\n" "$sn.$host"
        continue;
    }
    ttl="${ttlstr#*=}"  ## parameter expansion separating numeric ttl
    printf "%s is Online, ttl=%d\n" "$sn.$host" "$ttl"
done

使用/输出示例

注意:sn 被作为程序的第一个参数(使用上面的默认 192.168.6

$ bash ~/scr/utl/chksubnet.sh
<snip>
192.168.6.14 is Offline
192.168.6.15 is Offline
192.168.6.16 is Offline
192.168.6.17 is Online, ttl=64
192.168.6.18 is Offline
192.168.6.19 is Offline
<snip>

查看一下,如果您还有其他问题,请告诉我。

【讨论】:

  • 哇。我希望我能不止一次地给你加分——我真的很喜欢代码和散文。我很高兴看到你在 awk 之外做这件事。
  • 谢谢@Mark。在 shell 脚本中,总是有不止一种方法可以给猫换皮。多年来,您只是从可以将事物组合在一起的各种方式中汲取了一些精髓。 (我称之为将它添加到我的工具箱)与任何事情一样,您花在一个主题上的时间越多,您倾向于在工具箱中收集的工具就越多:)
【解决方案2】:

这是一种使用 awk 提取 ttl 的方法:

$ ping -c 1  8.8.8.8
PING 8.8.8.8 (8.8.8.8): 56 data bytes
64 bytes from 8.8.8.8: icmp_seq=0 ttl=53 time=48.575 ms

--- 8.8.8.8 ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 48.575/48.575/48.575/0.000 ms


$ ping -c 1  8.8.8.8 | awk -F'[ =]' '/ttl/ {print $8 }'
53

-F 参数告诉 awk 字段分隔符是什么。我已经指出了空格或等号。 awk 脚本的主体以模式/命令对的形式编写。在单行脚本中,awk 对包含 ttl 的任何行运行 print $8 命令。该命令告诉 awk 打印第八个字段(请记住,-F 指示如何将输入行分成字段。/ttl/ 模式也可以替换为 $7 == "ttl"。后一种形式更准确,因为它只会匹配 ttl 如果它作为第 7 个字段出现。

有更好更通用的实现。

如果您想快速完成此操作,请查看 nmap 实用程序。例如,

nmap -sn 192.168.1.0/24

除了 Nmap,您的程序可以通过 ping IP 一次并将结果通过管道传输到 awk 来改进,让 awk 生成所有输出。

【讨论】:

    【解决方案3】:

    如果主机在线,您可以使用以下脚本根据 ttl 值查找操作系统名称。

       for host in $(seq 1 255);
        do
            ping -c 1 $sn.$host | grep "Unreachable" &>/dev/null
            if [ $? -eq 0 ]
            then
                printf "%s\n" "$sn.$host is Offline"
                continue
            else
                printf "%s\n" "$sn.$host is Online" 
                ttlValue=`ping -c 1 $sn.$host | grep ttl | awk '{print $6}'|awk -F'=' '{print $2}' &>/dev/null`
                if [ $ttlValue -eq 64 ]
                then
                    echo "Operating is Linux"
                elif [ $ttlValue -eq 128 ]
                then
                    echo "Operating is Windows"
                else
                    echo "Operating is IOS"
                fi
            fi
        done
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-26
      • 1970-01-01
      • 2022-11-12
      • 2011-12-14
      • 1970-01-01
      • 1970-01-01
      • 2011-01-22
      • 1970-01-01
      相关资源
      最近更新 更多