Batch Ping Bash script execution

! # / bin / the bash 
# Script the Ping Batch 

# connection time 
the TMOUT = . 3 
# maximum number of threads 
MAX_THREAD = 4000 
# reserved memory size 
min_mem = 400 


# ip default configuration, by -r ip-ip specified format:. ping_batch SH -R & lt 192.168 . 1.1 - 192.168 . 2.254 
# IP prefix 
ip_preffix = " 192.168 " 
# 16-bit host range 192.168 . . 1 - 192.168 . 2 
ip_network_range = " 1-2 " 
# range of ping192.168.1.1-192.168.2.254
ip_host_range="1-254"

# ping脚本
PING_SCRIPT='ping -c 1 ${ip_preffix}.${ip_network}.${ip_host} &> /dev/null;[[ $? -eq 0 ]] && echo -e "\033[32m${ip_preffix}.${ip_network}.${ip_host} yes\033[0m" || echo -e "\033[32m${ip_preffix}.${ip_network}.${ip_host} no\033[0m" &>/dev/null'

# Ip格式错误提示
function ip_format_error()
{
echo -e "\033[31mIp format is incorrect.\nIp format: ping_batch.sh -r ip-ip.\nsimple: ping_batch.sh -r 192.168.1.1-192.168.2.1\033[0m"
}

# Ip format determination and the parameters passed to format 
Case  " $. 1 "  in 
' -R & lt ' )
 Shift 
ip_start = ` echo $ . 1 | awk -F ' - '  ' {}. 1 Print $ ' ` 
ip_end, = ` echo $ . 1 | awk - F. ' - '  ' {2} Print $ ' `
 echo $ ip_start | grep -E " (([0-9] | [1-9] [0-9] |. 1 [0-9] {2} | 2 [ 0-4] [0-9] | 25 [ 0-5]) \) {3} ([0-9] |. [1-9] [0-9] | 1 [0-9] {2} | 2 [0-4] [0-9] | 25 [0-5])" &>/dev/null
[[ $? -eq 0 ]] || ip_format_error
echo $ip_end|grep -E "(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])" &>/dev/null
[[ $? -eq 0 ]] || ip_format_error
ip_start_preffix=`echo $ip_start|awk -F'.' '{print $1"."$2}'`
ip_end_preffix=`echo $ip_end|awk -F'.' '{print $1"."$2}'`
if [[ "$ip_start_preffix" == "$ip_end_preffix" ]]
then
ip_preffix=$ip_start_preffix
else
echo -e "\033[31m Ip 192.168.1.1-192.168.2.1\n192.168==192.168\nSubnet is 16 bits.\033[0m"
fi
ip_network_range_start=`echo $ip_start|awk -F'.' '{print $3}'`
ip_network_range_end=`echo $ip_end|awk -F'.' '{print $3}'`
ip_host_range_start=`echo $ip_start|awk -F'.' '{print $4}'`
ip_host_range_end=`echo $ip_end|awk -F'.' '{print $4}'`
ip_network_range="${ip_network_range_start}-${ip_network_range_end}"
ip_host_range="ip_host_range_start} {$ - $} {ip_host_range_end " 
;;
 * )
 echo -e " \ 033 [31M parameter error, format: ping_batch.sh -r 192.168.1.1-192.168.2.254 \ 033 [0m " 
;; 
esac 

# thread over configuration values, waiting 
function thread_wait () 
{ 
SLEEP  . 1 
IF [[$ ( PS -ef | WC -l) - gt MAX_THREAD $]]
 the then 
thread_wait 
Fi 
} 

# check memory retention, than the set value wait 
function free_check () 
{ 
SLEEP  2 ;
 IF [[$ ( Free -m | grep  'MEM: ' | awk  ' {Print $. 4} ' ) - lt min_mem]]
 the then 
free_check 
Fi 
} 

# format Ip and ping script 
for ip_network in ` SEQ $ ( echo $ ip_network_range | awk -F ' - '  ' {Print $. 1 } ' ) $ ( echo $ ip_network_range | awk -F ' - '  ' {} Print $ 2 ' ) `
 do 
[[$ ( PS -ef | WC -l) -gt $MAX_THREAD ]] && thread_wait
for ip_host in `seq $(echo $ip_host_range|awk -F '-' '{print $1}') $(echo $ip_host_range|awk -F '-' '{print $2}')`
do
[[ $(free -m|grep 'Mem:'|awk '{print $4}') -lt MIN_MEM ]] && free_check
(eval "${PING_SCRIPT}") &
done
done

 

Guess you like

Origin www.cnblogs.com/tianyuanchen/p/11371751.html