blob: 3712314ca8b4613c7b7f2ca6013d01d834f8371a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
function valid_ipv4 --description "Validate IPv4 address" -a ipv4addr
if test -z "$ipv4addr"
return 1
end
if not string match -rq '^([0-9]{1,3}\.){3}[0-9]{1,3}$' -- $ipv4addr
return 1
end
for quadpart in (string split . -- $ipv4addr)
if not string match -rq '^(0|[1-9][0-9]{0,2})$' -- $quadpart
return 1
end
if test $quadpart -gt 255; or test $quadpart -lt 0
return 1
end
end
return 0
end
function reverse_ipv4 --description "Print reverse IPv4 address" -a ipv4addr
if test -z "$ipv4addr"
return
end
set -l index '4'
set -l rquad '' '' '' ''
for item in (string split --right --max 3 . $ipv4addr)
set rquad[$index] $item
set index (math $index-1)
end
echo "$rquad[1].$rquad[2].$rquad[3].$rquad[4]"
end
|