Linux shell 编程:查找一段IP地址是否被占用!


#!/bin/bash

if [ $# != 3 ];then
        echo "ip_test need three parameter ex > { ip_test arg1 arg2 arg3 }" && exit 1
fi

arg1=$(echo $1 | grep [^0-9])
arg2=$(echo $2 | grep [^0-9])
arg3=$(echo $3 | grep [^0-9])

if [ "$arg1" != "" ] || [ "$arg2" != "" ] || [ "$arg3" != "" ]; then
        echo "arg must be positive integer, ex > { ip_test 25 1 220 }" && exit 2
fi

if [ "$1" -lt 0 ] || [ "$1" -gt 255 ]; then
        echo "arg1 is invalid, need > { 0-255 }" && exit 3
fi

if [ "$2" -lt 0 ] || [ "$2" -gt 255 ];then
        echo "arg2 is invalid, need > { 0-255 }" && exit 4
fi

if [ "$3" -lt 0 ] || [ "$3" -gt 255 ];then
        echo "arg3 is invalid, need > { 0-255 }" && exit 5
fi

if [ "$2" -gt "$3" ];then
        echo "arg2 cannt gt arg3, need > { arg2 <= arg3 }" && exit 6
fi

network="135.251.$1"
result=1
for (( hostid=$2; hostid<=$3; hostid=hostid+1 ))
do
        ping -c 1 -w 1 ${network}.${hostid} > /dev/null && result=1 || result=0
        if [ $result -eq 0 ];then
                echo "ping ${network}.${hostid} failure"
        fi
done
exit 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值