功能:使用ping 命令测试某个网段可能未使用的 ip地址
脚本如下:
- # cat sip.sh
- #!/bin/bash
- #Not used to find the ip
- #20120415
- #variables
- user=`id -u`
- #function
- netmask() {
- 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 \
- 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 \
- 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 \
- 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 \
- 255.255.255.255)
- echo ${mask[$1]}
- }
- #
- sping() {
- #echo $1 $2 $3
- echo "The following ICMP packet not through"
- for((ip=$1;ip<=$2;ip++));do
- ping -f -c 5 -W 1 "${3}$ip" > /dev/null || echo "${3}$ip"
- done
- }
- #main
- if [ $user -ne 0 ];then
- echo "must root !!"
- exit 1
- fi
- #
- if [ $# -eq 0 ];then
- echo "Usage: $0 10.10.10.0/24"
- echo -e "
- 59.151.*.*/24
- 114.112.*.*/26
- 10.10.10.0/24"
- exit 1
- fi
- #
- shell_arg=$(echo $1 | awk -F/ '{print $2}')
- get_ip_head=$(echo $1 | awk -F/ '{print $1}'| awk -F. '{print $1"."$2"."$3"."}')
- get_ip_trailing=$(echo $1 | awk -F/ '{print $1}'| awk -F. '{print $4}')
- get_mask=$(netmask $shell_arg)
- get_gj=$(echo $((256-$(echo $get_mask | awk -F. '{print $4}'))))
- put_ip_start=$(echo $(($get_ip_trailing + 1)))
- put_ip_end=$(echo $(($get_ip_trailing + $get_gj - 2)))
- #
- #echo -e "$shell_arg $get_mask $get_gj $get_ip_head $get_ip_trailing $put_ip_start $put_ip_end"
- sping $put_ip_start $put_ip_end $get_ip_head
使用方法:
- sh sip.sh 10.0.0.0/24
- The following ICMP packet not through
- 10.0.0.1
- 10.0.0.2
- 10.0.0.4
- 10.0.0.8
参考
http://dngood.blog.51cto.com/446195/667009
结束
使用网段自动算出ip地址,根据ping命令判断这些ip是否在用。
更多欢迎到此讨论:
71921660 37275208 (已满)
本文转自 dongnan 51CTO博客,原文链接:http://blog.51cto.com/dngood/837534