且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

找到没使用的ip地址 plus版

更新时间:2022-09-08 20:09:55

功能:使用ping 命令测试某个网段可能未使用的 ip地址
 

脚本如下:


  1. # cat sip.sh  
  2. #!/bin/bash 
  3. #Not used to find the ip 
  4. #20120415 
  5.  
  6. #variables 
  7. user=`id -u` 
  8.  
  9.  
  10. #function 
  11. netmask() { 
  12.   mask=(0 128.0.0.0 192.0.0.0 224.0.0.0 240.0.0.0 248.0.0.0 252.0.0.0 254.0.0.0 \ 
  13.       255.0.0.0 255.128.0.0 255.192.0.0 255.224.0.0 255.240.0.0 255.248.0.0 255.252.0.0 255.254.0.0 \ 
  14.       255.255.0.0 255.255.128.0 255.255.192.0 255.255.224.0 255.255.240.0 255.255.248.0 255.255.252.0 255.255.254.0 \ 
  15.       255.255.255.0 255.255.255.128 255.255.255.192 255.255.255.224 255.255.255.240 255.255.255.248 255.255.255.252 255.255.255.254 \  
  16.       255.255.255.255) 
  17.   echo ${mask[$1]}  
  18. sping() { 
  19. #echo $1 $2 $3 
  20. echo "The following ICMP packet not through" 
  21. for((ip=$1;ip<=$2;ip++));do 
  22.     ping -f -c 5 -W 1 "${3}$ip" > /dev/null || echo "${3}$ip" 
  23. done 
  24. }   
  25.  
  26.  
  27. #main 
  28. if [ $user -ne 0 ];then 
  29.     echo "must root !!" 
  30.     exit 1 
  31. fi 
  32. if [ $# -eq 0 ];then 
  33.     echo "Usage: $0 10.10.10.0/24" 
  34.     echo -e " 
  35.     59.151.*.*/24 
  36. 114.112.*.*/26
  37.     10.10.10.0/24" 
  38.     exit 1 
  39. fi 
  40. shell_arg=$(echo $1 | awk -F/ '{print $2}') 
  41. get_ip_head=$(echo $1 | awk -F/ '{print $1}'| awk -F. '{print $1"."$2"."$3"."}') 
  42. get_ip_trailing=$(echo $1 | awk -F/ '{print $1}'| awk -F. '{print $4}') 
  43. get_mask=$(netmask $shell_arg) 
  44. get_gj=$(echo $((256-$(echo $get_mask | awk -F. '{print $4}')))) 
  45. put_ip_start=$(echo $(($get_ip_trailing + 1))) 
  46. put_ip_end=$(echo $(($get_ip_trailing + $get_gj - 2))) 
  47. #echo -e "$shell_arg $get_mask $get_gj $get_ip_head $get_ip_trailing $put_ip_start $put_ip_end" 
  48. sping $put_ip_start $put_ip_end $get_ip_head 

使用方法:


  1. sh sip.sh 10.0.0.0/24 
  2. The following ICMP packet not through 
  3. 10.0.0.1 
  4. 10.0.0.2 
  5. 10.0.0.4 
  6. 10.0.0.8 

参考

http://dngood.blog.51cto.com/446195/667009

结束

使用网段自动算出ip地址,根据ping命令判断这些ip是否在用。

更多欢迎到此讨论:
71921660   37275208 (已满)


本文转自 dongnan 51CTO博客,原文链接:http://blog.51cto.com/dngood/837534