如果我想提取ttl并从ping命令显示它,我该怎么做呢? [英] If i wanted to extract the ttl and display it from the ping command how could i go about doing that?

查看:73
本文介绍了如果我想提取ttl并从ping命令显示它,我该怎么做呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写脚本并想对网络上的设备执行ping操作,告诉我是否可访问,然后从ping获取ttl数据并告诉我操作系统.

Scripting and want to ping devices on a network, tell me if it's reachable or not, and then get the ttl data from the ping and tell me the operating system.

我曾经尝试过使用awk命令,但是我对脚本编写还是陌生的,可能无法正确使用它.

Ive tried using the awk command, but I am also new to scripting and may not be using it correctly.

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

I need the ttl value and to store it into a variable and then tell me what operating system it is based on Linux has a ttl of 64, windows a ttl of 128, and ios of 255

推荐答案

使用 -w (或 -W )选项.例如,您可以在确定主机是否在线的同一调用中保存 ping 中的 ttl = XX 值,然后可以使用简单的参数扩展从等号右侧提取数字 ttl 值,例如

You can do things in a bit more concise manner and minimize the time waiting for an Offline host by setting a timeout using the -w (or -W) option. For example you can save the ttl=XX value from ping in the same call that determines whether the host is online or not and then you can use a simple parameter expansion to extract the numeric ttl value from the right side of the equal sign, e.g.

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

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

Above the command substitution $(...) executes ping and pipes the output to grep and assigns the results to ttlstr. The command substitution return is the return of the last command in the pipeline telling you whether grep for "ttl=####" succeeded or failed. That's all you need to determine whether the host is online or not. On failure output your "Offline" message and try the next, e.g.

    ## 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 ,以删除所有字符,直到并包括从字符串开头的'='符号,仅保留数字 ttl ,例如

If the command substitution succeeds, you can output your "Online" message and you can isolate the numeric ttl using a simple parameter expansion to remove all characters up to, and including, the '=' sign from the beginning of the string leaving only the numeric ttl, e.g.

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

完全将其放入您可以做的事情:

Putting it altogether you could do:

#!/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 )

note: the sn is taken as the 1st argument to the program (using a default of 192.168.6 above)

$ 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>

仔细检查一下,如果还有其他问题,请告诉我.

Look things over and let me know if you have further questions.

这篇关于如果我想提取ttl并从ping命令显示它,我该怎么做呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆