linux 批量ping多个连续的IP地址

#!/bin/bash
#author:zhaoyuqin
#date:20200323
#引用系统函数
. /etc/init.d/functions
#交互式获取网段地址
read -p "请输入网段(255.255.255.255):" net
#截取部分网段地址
network=`echo $net|cut -d "." -f 1,2,3`
#检查IP是否合法
if [ -z "$net"  ];then
              echo -e "\e[32m\e[1m 网段地址不能为空 \e[0m"
              exit 1
 fi


function checkIPAddr()
{
#匹配正确的IP格式
echo $net|grep "^[0-9]\{1,3\}\.\([0-9]\{1,3\}\.\)\{2\}[0-9]\{1,3\}$" > /dev/null 2>&1
if [ $? -eq 1 ];then
	echo -e "\e[32m\e[1m 请勿输入除数字外的其他符号 \e[0m"
	exit 1
fi
a=`echo $net|awk -F . '{print $1}'`
b=`echo $net|awk -F . '{print $2}'`
c=`echo $net|awk -F . '{print $3}'`
for num in $a $b $c
do
	if [ $num -gt 255 ] || [ $num -lt 0 ];then
	echo -e "\e[32m\e[1m 输入的网段地址不合法! \e[0m"
	exit 1
	fi
done
}
checkIPAddr
read -p "请地址输入范围(min-max):" sum1 sum2
#循环ping
for i in `seq $sum1 $sum2`
do
	
	ping -c1 -W1 ${network}.${i}>/dev/null 2>&1
	if [ $? -eq 0 ];then
	action "${network}.${i}" /bin/true
	else
	action "${network}.${i}" /bin/false
	fi
	 
done
#等待以上命令执行完成后再执行
wait
#输出完成时间
end_Time=`date`
echo "完成时间:$end_Time"


该脚本实现的功能:
1.批量ping多个连续的IP地址并返回值
2.写了个函数来检查IP合法性
3.练练手的~

发布了4 篇原创文章 · 获赞 3 · 访问量 121

猜你喜欢

转载自blog.csdn.net/weixin_44086408/article/details/105059483