歡迎您光臨本站 註冊首頁

找到沒使用的ip地址 plus版

←手機掃碼閱讀     火星人 @ 2014-03-08 , reply:0

功能:使用ping 命令測試某個網段可能未使用的 ip地址

腳本如下:

  1. # cat sip.sh
  2. #!/bin/bash
  3. #Not used to find the ip
  4. #20120415
  5. #variables
  6. user=`id -u`
  7. #function
  8. netmask() {
  9. 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 \
  10. 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 \
  11. 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 \
  12. 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 \
  13. 255.255.255.255)
  14. echo ${mask[$1]}
  15. }
  16. #
  17. sping() {
  18. #echo $1 $2 $3
  19. echo "The following ICMP packet not through"
  20. for((ip=$1;ip<=$2;ip ));do
  21. ping -f -c 5 -W 1 "${3}$ip" > /dev/null || echo "${3}$ip"
  22. done
  23. }
  24. #main
  25. if [ $user -ne 0 ];then
  26. echo "must root !!"
  27. exit 1
  28. fi
  29. #
  30. if [ $# -eq 0 ];then
  31. echo "Usage: $0 10.10.10.0/24"
  32. echo -e "
  33. 59.151.*.*/24
  34. 114.112.*.*/26
  35. 10.10.10.0/24"
  36. exit 1
  37. fi
  38. #
  39. shell_arg=$(echo $1 | awk -F/ '{print $2}')
  40. get_ip_head=$(echo $1 | awk -F/ '{print $1}'| awk -F. '{print $1"."$2"."$3"."}')
  41. get_ip_trailing=$(echo $1 | awk -F/ '{print $1}'| awk -F. '{print $4}')
  42. get_mask=$(netmask $shell_arg)
  43. get_gj=$(echo $((256-$(echo $get_mask | awk -F. '{print $4}'))))
  44. put_ip_start=$(echo $(($get_ip_trailing 1)))
  45. put_ip_end=$(echo $(($get_ip_trailing $get_gj - 2)))
  46. #
  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是否在用.

 

本文出自 「dongnan」 博客,請務必保留此出處http://dngood.blog.51cto.com/446195/837534


[火星人 ] 找到沒使用的ip地址 plus版已經有335次圍觀

http://coctec.com/docs/linux/show-post-45991.html