본문 바로가기

개발 이야기/Database

IP / Prefix 구조에서 IP A/B/C/D / Prefix 로 분류하는 쿼리

반응형

select
substr(prefix, 1, instr(prefix, '.', 1, 1) - 1) ip_a,
substr(prefix, instr(prefix, '.', 1, 1) + 1, instr(prefix, '.', 1, 2) - instr(prefix, '.', 1, 1) - 1) ip_b,
substr(prefix, instr(prefix, '.', 1, 2) + 1, instr(prefix, '.', 1, 3) - instr(prefix, '.', 1, 2) - 1) ip_c,
substr(prefix, instr(prefix, '.', 1, 3) + 1, (instr(prefix, '/', 1, 1)) - (instr(prefix, '.', 1, 3) + 1)) ip_d,
substr(prefix, instr(prefix, '/', 1, 1) + 1) prefix
from cf_bgp_prefix;



IP / Prefix 에서 Start IP Long 과 End IP Long 값을 구하는 경우
==> IP 값과 Prefix 값이 일치하는 경우만 적용.
  ==> 예 :  103.245.20.0 / 24 와 같이 네트워크 영역과 Prefix 값이 일치하는 경우만
select (IP_A * 16777216) + (IP_B * 65536 ) + (IP_C * 256 ) + (IP_D) AS START_IP_LONG,
((IP_A * 16777216) + (IP_B * 65536 ) + (IP_C * 256 ) + (IP_D)) + (power(2, 32 - prefix) - 1) AS END_IP_LONG
FROM
(
    select
    substr(prefix, 1, instr(prefix, '.', 1, 1) - 1) ip_a,
    substr(prefix, instr(prefix, '.', 1, 1) + 1, instr(prefix, '.', 1, 2) - instr(prefix, '.', 1, 1) - 1) ip_b,
    substr(prefix, instr(prefix, '.', 1, 2) + 1, instr(prefix, '.', 1, 3) - instr(prefix, '.', 1, 2) - 1) ip_c,
    substr(prefix, instr(prefix, '.', 1, 3) + 1, (instr(prefix, '/', 1, 1)) - (instr(prefix, '.', 1, 3) + 1)) ip_d,
    substr(prefix, instr(prefix, '/', 1, 1) + 1) prefix
    from cf_bgp_prefix
) IP_BASE;


IP / Prefix 에서 Start IP Long 과 End IP Long 값을 구하는 경우
==> IP 값과 Prefix 값이 일치하는 경우만 적용.
  ==> 예 :  103.245.20.0 / 24 와 같이 네트워크 영역과 Prefix 값이 일치하는 경우만

select (IP_A * 16777216) + (IP_B * 65536 ) + (IP_C * 256 ) + (IP_D) AS START_IP_LONG,
((IP_A * 16777216) + (IP_B * 65536 ) + (IP_C * 256 ) + (IP_D)) + (power(2, 32 - prefix) - 1) AS END_IP_LONG
FROM
(
    select
    substr(prefix, 1, instr(prefix, '.', 1, 1) - 1) ip_a,
    substr(prefix, instr(prefix, '.', 1, 1) + 1, instr(prefix, '.', 1, 2) - instr(prefix, '.', 1, 1) - 1) ip_b,
    substr(prefix, instr(prefix, '.', 1, 2) + 1, instr(prefix, '.', 1, 3) - instr(prefix, '.', 1, 2) - 1) ip_c,
    substr(prefix, instr(prefix, '.', 1, 3) + 1, (instr(prefix, '/', 1, 1)) - (instr(prefix, '.', 1, 3) + 1)) ip_d,
    substr(prefix, instr(prefix, '/', 1, 1) + 1) prefix
    from (select '103.245.20.0/24' prefix from dual)
) IP_BASE


반응형