HJM

导航

shell练习题1

需求如下:

写一个shell脚本,把10.0.1.0/24网段在线的ip列出来。

参考解答如下

  • 方法1
#!/bin/bash

ip="10.0.1."
for i in $(seq 1 254)
do
    ping -c 2 $ip$i &> /dev/null
    if [ $? -eq 0 ]
    then
        echo The host $ip$i is online.
    else
        echo The host $ip$i is offline.
    fi
done
  • 方法2
ip="10.0.1."
i=1
while [ $i -le 254 ]
do
    ping -c 2 $ip$i &> /dev/null
        if [ $? -eq 0 ]
        then
            echo The host $ip$i is online.
        else
            echo The host $ip$i is offline.
    fi
        i=$(($i+1))
done
  • 方法3(多线程)
#!/bin/bash

ip="10.0.1."
for i in $(seq 1 254)
do
{
    ping -c 2 $ip$i &> /dev/null
        if [ $? -eq 0 ]
        then
            echo The host $ip$i is online.
        else
            echo The host $ip$i is offline.
    fi
} &
done
wait

posted on 2018-10-19 10:40  kennminn  阅读(148)  评论(0编辑  收藏  举报