#!/bin/bash

# 网段的起始地址和结束地址

START=1

END=254


# 网段的前缀,例如192.168.1

#input="192.168.1"


echo 请输入IP网段前3位

#这里要输入网段前缀,例如192.168.1,最后一位不用输入

read "input"

# 用于改变文本颜色的函数

text_green() {

   echo -e "\033[0;32m$1\033[0m"

}


text_red() {

   echo -e "\033[0;31m$1\033[0m"

}


# 主ping循环

for (( i=START; i<=END; i++ ))

do

   IP=${input}.${i}

   if ping -c 1 -w 1 ${IP} &>/dev/null; then

       # ping通,打印绿色文本

       text_green "${IP} is up."

   else

       # ping不通,打印红色文本

       text_red "${IP} is down."

   fi

done


要使用要先touch一个文件,把上面一段脚本粘贴进去即可

touch  ping.sh

然后给创建都的这个文件赋权

chmod +x  ping.sh



下面这段输出无色差

#!/bin/bash

# 网段地址

echo 请输入IP网段前3位

read "input"

#network="input"

# 循环254次,因为IP地址通常从1到254

for i in {1..254}

do

   # 使用ping命令和通配符来ping每个可能的地址

   ping -c 1 "${input}.${i}" &> /dev/null

   # 检查命令的返回值来判断是否ping通

   if [ $? -eq 0 ]; then

       echo "${input}.${i} is up."

   else

       echo "${input}.${i} is down."

   fi

done

~