试图在shell脚本中使用traceroute,我无法正常显示它

时间:2015-04-19 00:25:48

标签: traceroute

#!/bin/bash

host_name=A

echo -n " Enter a host name "
read host_name
total_hops=  traceroute $host_name | cut -d " " -f 1 | wc -l
read $total_hops
echo "The host $host_name is $total_hops away"

1 个答案:

答案 0 :(得分:0)

对我来说,有两件事对你的剧本没有意义:首先,你不需要read $total_hops,因为你已经分配了它;第二,如果您只想计算线数,则只需使用wc而不使用cut。但似乎有些额外的行不应该算作跳跃,所以我会使用tail获取最后一行,然后使用cut获取第一列:

#!/bin/bash

echo -n "Enter a host name "
read host_name
total_hops=`traceroute $host_name | tail -n 1 | cut -d " " -f 1 `
echo "The host $host_name is $total_hops hops away"

输出结果为:

> Enter a host name google.com 
> traceroute to google.com (216.58.216.46), 64 hops max, 52 byte packets 
> The host google.com is 12 hops away