1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#!/bin/bash
# blog: http://lizhenliang.blog.51cto.com
function
check_ip() {
IP=$1
VALID_CHECK=$(
echo
$IP|
awk
-F.
'$1<=255&&$2<=255&&$3<=255&&$4<=255{print "yes"}'
)
if
echo
$IP|
grep
-E
"^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$"
>
/dev/null
;
then
if
[ ${VALID_CHECK:-no} ==
"yes"
];
then
echo
"IP $IP available."
else
echo
"IP $IP not available!"
fi
else
echo
"IP format error!"
fi
}
# Example
check_ip 192.168.1.1
check_ip 256.1.1.1
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#!/bin/bash
# blog: http://lizhenliang.blog.51cto.com
function
check_ip() {
IP=$1
if
[[ $IP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]];
then
FIELD1=$(
echo
$IP|
cut
-d. -f1)
FIELD2=$(
echo
$IP|
cut
-d. -f2)
FIELD3=$(
echo
$IP|
cut
-d. -f3)
FIELD4=$(
echo
$IP|
cut
-d. -f4)
if
[ $FIELD1 -
le
255 -a $FIELD2 -
le
255 -a $FIELD3 -
le
255 -a $FIELD4 -
le
255 ];
then
echo
"IP $IP available."
else
echo
"IP $IP not available!"
fi
else
echo
"IP format error!"
fi
}
# Example
check_ip 192.168.1.1
check_ip 256.1.1.1
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
#!/bin/bash
# blog: http://lizhenliang.blog.51cto.com
function
check_ip() {
local
IP=$1
VALID_CHECK=$(
echo
$IP|
awk
-F.
'$1<=255&&$2<=255&&$3<=255&&$4<=255{print "yes"}'
)
if
echo
$IP|
grep
-E
"^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$"
>
/dev/null
;
then
if
[ $VALID_CHECK ==
"yes"
];
then
echo
"IP $IP available!"
return
0
else
echo
"IP $IP not available!"
return
1
fi
else
echo
"IP format error!"
return
1
fi
}
while
true
;
do
read
-p
"Please enter IP: "
IP
check_ip $IP
[ $? -
eq
0 ] &&
break
done
|