Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 26633f8

Browse files
authored
Create grep_inet.sql
1 parent c46ebb7 commit 26633f8

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

‎functions/grep_inet.sql‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
create or replace function grep_inet(str text)
2+
returns table (order_num int, "all" text, addr inet, port int, mask int)
3+
stable
4+
returns null on null input
5+
parallel safe -- Postgres 10 or later
6+
language sql
7+
as $func$
8+
select (row_number() over ())::int as order_num,
9+
m[1] as all,
10+
array_to_string(m[2:5], '.')::inet as addr,
11+
m[6]::int as port,
12+
m[7]::int as mask
13+
from regexp_matches(str,
14+
$$
15+
( #1 all
16+
(?<![\d.:/]) #boundary
17+
(\d{1,3}) \. (\d{1,3}) \. (\d{1,3}) \. (\d{1,3}) #2-5 addr 1..255
18+
(?:
19+
: (\d{1,5}) #6 port 1..65535
20+
| / (\d{1,2}) #7 mask 0..32
21+
)?
22+
(?![\d.:/]) #boundary
23+
)
24+
$,ドル 'xg') as t(m)
25+
where not exists(select
26+
from unnest(m[2:5]) u(e)
27+
where e::int not between 0 and 255)
28+
and (m[6] is null or m[6]::int between 1 and 65535)
29+
and (m[7] is null or m[7]::int between 0 and 32);
30+
$func$;
31+
32+
comment on function grep_inet(str text) is $$
33+
Захватывает из строки все существующие IP адреса.
34+
IP адрес может иметь необязательный порт или маску.
35+
$$;
36+
37+
--TEST
38+
do $do$
39+
declare
40+
str_in constant text not null default $$
41+
#valid
42+
0.0.0.0
43+
1.2.3.4
44+
-1.2.3.4
45+
1.2.3.4-
46+
01.02.03.04
47+
001.002.003.004
48+
9.9.9.9
49+
10.10.10.10
50+
99.99.99.99
51+
100.100.100.100
52+
255.255.255.255
53+
127.0.0.1
54+
192.168.1.1:1
55+
192.168.1.255:65535
56+
192.168.1.1/0
57+
192.168.1.255/32
58+
59+
#invalid octet range
60+
256.2.3.4
61+
1.256.3.4
62+
1.2.256.4
63+
1.2.3.256
64+
65+
#invalid boundary
66+
1.1.1.1.
67+
1.1.1.1/
68+
1.1.1.1:
69+
70+
1.1.1.1:9.
71+
1.1.1.1:99:
72+
1.1.1.1:999/
73+
74+
1.1.1.1/0.
75+
1.1.1.1/32:
76+
1.1.1.1/32/
77+
78+
.2.2.2.2
79+
:2.2.2.2
80+
/2.2.2.2
81+
82+
#invalid length
83+
1.2.3.4.5
84+
1.2.3
85+
0.1
86+
3...3
87+
88+
#invalid mask
89+
5.5.5.5/-1
90+
5.5.5.5/33
91+
92+
#invalid port
93+
5.5.5.5:0
94+
5.5.5.5:65536
95+
$$;
96+
97+
str_out constant text not null default '[{"order_num":1,"all":"0.0.0.0","addr":"0.0.0.0","port":null,"mask":null}, {"order_num":2,"all":"1.2.3.4","addr":"1.2.3.4","port":null,"mask":null}, {"order_num":3,"all":"1.2.3.4","addr":"1.2.3.4","port":null,"mask":null}, {"order_num":4,"all":"1.2.3.4","addr":"1.2.3.4","port":null,"mask":null}, {"order_num":5,"all":"01.02.03.04","addr":"1.2.3.4","port":null,"mask":null}, {"order_num":6,"all":"001.002.003.004","addr":"1.2.3.4","port":null,"mask":null}, {"order_num":7,"all":"9.9.9.9","addr":"9.9.9.9","port":null,"mask":null}, {"order_num":8,"all":"10.10.10.10","addr":"10.10.10.10","port":null,"mask":null}, {"order_num":9,"all":"99.99.99.99","addr":"99.99.99.99","port":null,"mask":null}, {"order_num":10,"all":"100.100.100.100","addr":"100.100.100.100","port":null,"mask":null}, {"order_num":11,"all":"255.255.255.255","addr":"255.255.255.255","port":null,"mask":null}, {"order_num":12,"all":"127.0.0.1","addr":"127.0.0.1","port":null,"mask":null}, {"order_num":13,"all":"192.168.1.1:1","addr":"192.168.1.1","port":1,"mask":null}, {"order_num":14,"all":"192.168.1.255:65535","addr":"192.168.1.255","port":65535,"mask":null}, {"order_num":15,"all":"192.168.1.1/0","addr":"192.168.1.1","port":null,"mask":0}, {"order_num":16,"all":"192.168.1.255/32","addr":"192.168.1.255","port":null,"mask":32}]';
98+
begin
99+
--positive and negative both
100+
assert (select json_agg(to_json(t))::text = str_out
101+
from grep_inet(str_in) as t);
102+
end;
103+
$do$;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /