需求以下:shell
寫一個shell腳本,把10.0.1.0/24網段在線的ip列出來。bash
參考解答以下多線程
#!/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
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
#!/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