search_Unused_IP

【背景】
Linux 配置网卡IP时,需要寻找局域网内未占用的可用IP
【解决方案】
参考search_Unused_IP.sh脚本

#!/bin/bash
######################################
# Function: Search Unused IP
# Examples: ./check_ip.sh 128.128.5 1 255
# parameter: 128.128.5.1~128.128.5.255
# Auther: xxxxxx
# Date: 2015-6-1
######################################

function usage()  
{  
    if [ $# -lt 3 ]; then  
        echo "Usage: ./$0 ip_address begin_number end_number" >&2  
        exit 1  
    fi  
}  

ip_address="$1"  
begin_number="$2"  
end_number="$3"  

function Unused_IP()  
{  
while [ $begin_number -lt $end_number ]  
do  
  ping -c 1 -w 1 $ip_address.$begin_number > /dev/null 2>&1  

  if [  $? -ne  0 ]; then  
    echo "$ip_address.$begin_number"  
    num=`expr $num + 1`  
  fi  

 begin_number=`expr $begin_number + 1 `  

done  
echo -e "\\033[1;32m Total:$num \\033[1;37m"  
}  
usage $@  
Unused_IP $@  

你可能感兴趣的:(linux)