#!/bin/bash
##################################################################
# discription:ping tools,使用ping命令监控主机是否在线。
# author.:frogchou
# date:Wed Dec 25 20:57:09 PST 2019
##################################################################


showtable(){
	echo "ip 使用情况如下："
	echo ""
	count=1
	cat /tmp/iparr | while read line
	do
		if [ `expr $count % 5` -eq 0 ];then
			echo -e "\t" "|" $line "|"
			echo -e "\t-----------------------------------------------------------------------------------------------------------------------------------------------------------"
		else
			echo -e "\t" "|" $line "|" "\c"
		fi
		let count++
	done
	echo ""
	rm -f /tmp/iparr
	echo "完毕" 
}


doinit(){
	if [ $# -lt 1 ];then
		read -p "请指出ip地址网段，例如：192.168.3:" var
		doinit $var
	else
		for i in $1.{1..254};
			do
			{
				ping -c 2 -W 2 $i > /dev/null 2>$1
				[ $? -eq 0 ] && result="\e[1;32m"${i}"-online\e[0m" || result="\e[1;31m"${i}"-offline\e[0m"
				echo -e $result	>> /tmp/iparr			
			}&
			done
			wait
			showtable
	fi
}
doinit $1
