且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

linux下检测局域网在线主机

更新时间:2022-05-30 04:41:34

用户输入一个启始主机和结束主机,检测局域网这个范围内的主机在线状态

pingCK.sh:

#!/bin/bash

#study

#v0.0.2

#ping 192.168.21.1 - 192.168.21.254

read -p "enter start host:" snum

read -p "enter end host:" enum

for i in $(seq $snum 1 $enum);do

ping -c 1 -t 1 192.168.21.$i > /tmp/res

grep ttl /tmp/res>/tmp/rus

        if  [ `wc -l /tmp/rus |awk '{print $1}'`  = 0 ];then

        echo "21.$i is off"

        else

        echo "21.$i is online"

        fi

done


测试结果:

[root@localhost /]# bash -n /tmp/pingCK.sh 

[root@localhost /]# /tmp/pingCK.sh 

enter start host:128

enter end host:135

21.128 is off

21.129 is off

21.130 is off

21.131 is off

21.132 is off

21.133 is online

21.134 is off

21.135 is off

done





      本文转自echoroot 51CTO博客,原文链接:http://blog.51cto.com/echoroot/1923578,如需转载请自行联系原作者