实战:写一个脚本,扫描本地网络中存活的机器


案例需求:
判断本地网络中哪些IP被使用

案例分析:
采用ping的方式判断IP是否被占用

a、能ping通说明占用 b、不能ping通说明未被占用

b、命令

ping -c1 IP

算法:
1、ping ip
2、分析ping结果
3、输出结果

      代码:   [root@CentOs shell]# cat ShellTest.sh
#! /bin/bash

netsub="192.168.0."

for ip in `seq 1 255`
   do (
    if ping -c1 $netsub$ip &>/dev/null;then
        echo "$netsub$ip is open"
    else
        echo "$netsub$ip is close"
    fi
      )&
done
[root@CentOs shell]#

  结果: