1

I have a database, one of the field having value of IP address. and the default value is 12.123.60.165. From those default value i have to generate IP address. The maximum value will be 256.256.256.256.

This is my function, still not cover my needed

/* Formatted on 9/26/2017 9:44:51 AM (QP5 v5.115.810.9015) */
SET SERVEROUTPUT ON

DECLARE
   IP    INTEGER;
   IP1   INTEGER;
   IP2   INTEGER;
   IP3   INTEGER;
   IP4   INTEGER;
   IPI   INTEGER := 1;
BEGIN

   IP1 := FLOOR (IPI / POWER (2, 24));
   IPI := IPI - (IP1 * POWER (2, 24));
   IP2 := FLOOR (IPI / POWER (2, 16));
   IPI := IPI - (IP2 * POWER (2, 16));
   IP3 := FLOOR (IPI / POWER (2, 8));
   IPI := IPI - (IP3 * POWER (2, 8));
   IP4 := IPI;

   DBMS_OUTPUT.PUT_LINE (IP1 || '.' || IP2 || '.' || IP3 || '.' || IP4);
END;
/

any help will be appreciate so much

4
  • What do you mean from those default value I have to generate IP addresses? Do you just need the complete IP4 address range where the IP address is > 12.123.60.256? Not sure what you're after.
    – xQbert
    Sep 26, 2017 at 13:19
  • I mean i have default value of IP number. It is 12.123.60.165. Then I have to generate new IP number that will be 12.123.60.167, 12.123.60.168, 12.123.60.169 and so on. until it ends with (256) 12.123.60.256. the maximum IP number until the number of IP will be 256.256.256.256.
    – Ras Rass
    Sep 27, 2017 at 1:55
  • Well generating 4,213,638,225 records... That's 4 billion 213 million is going to take some time. but 4 cross joins of a table having 256 records would seem to work; and your starting IP is only going to eliminate around 14,612,400 records; or 0.3456% don't expect it to be fast... but you could create a table with these #'s I suppose...
    – xQbert
    Sep 27, 2017 at 13:18
  • thank you so much for your reply. yes you are right, this is will be so many records will be. I already plan also to create a table on this. thank you very much for your answer. it help me so much
    – Ras Rass
    Sep 28, 2017 at 1:57

2 Answers 2

2

You can use dbms_random package to generate random values. You need privileges on this package. for example:

SQL>  select trunc(dbms_random.value(12,256) ) ip1,
  2        trunc(dbms_random.value(123,256) ) ip2,
  3        trunc(dbms_random.value(60,256) ) ip3,
  4        trunc(dbms_random.value(165,256) ) ip4
  5*  from dual

   IP1        IP2        IP3        IP4
---------- ---------- ---------- ----------
   156        163         78        231

Or generate in a loop. Something like this:

declare
  ip1 int;
  ip2 int;
  cnt int := 0;
begin
  for i in 12..256 loop
    ip1 := i;
    for j in 123..256 loop
      ip2 := j; 
    end loop;
  end loop;
end;
/
2
  • dbms_random make the IP produce in randomly . but what i want its generate not in randomly. in structured 1,2,3,4,5... anyway thank you so much for your reply dude
    – Ras Rass
    Sep 26, 2017 at 9:18
  • then we can use a loop. I added an example in my answer Sep 26, 2017 at 13:15
1

To generate a set of all IPv4's one could use a cross join and a CTE with 16 records and a cross join. But I'm not sure what you're really after yet.

with cte (A)  as (
SELECT 0 from dual UNION ALL
SELECT 1 from dual UNION ALL
SELECT 2 from dual UNION ALL
SELECT 3 from dual UNION ALL
SELECT 4 from dual UNION ALL
SELECT 5 from dual UNION ALL
SELECT 6 from dual UNION ALL
SELECT 7 from dual UNION ALL
SELECT 8 from dual UNION ALL
SELECT 9 from dual UNION ALL
SELECT 10 from dual UNION ALL
SELECT 11 from dual UNION ALL
SELECT 12 from dual UNION ALL
SELECT 13 from dual UNION ALL
SELECT 14 from dual UNION ALL
SELECT 15 from dual), 
--Get data set for 0 to 255 numbers. 
Get256Range as (SELECT  row_number() over (order by A.A)-1 seg
    FROM CTE A
    CROSS JOIN cte B)
--Now cross join the 256 4 times for each class of Ip.

SELECT A.Seg ||'.'|| B.Seg||'.'|| C.Seg||'.'||D.Seg as IPAddress
from Get256Range A
CROSS JOIN Get256Range B
CROSS JOIN Get256Range C
CROSS JOIN Get256Range D
--Not sure what part of the "set" you need but one could change these values 
--Limited for sizing; but one could create a table of all IPv4 addresses...
WHERE A.seg = 12
  and B.seg = 123;

if needed you can include the leading zeros'...

lpad(A.Seg,3,'0') ||'.'|| lpad(B.Seg,3,'0')||'.'|| lpad(C.Seg,3,'0')||'.'|| lpad(D.Seg,3,'0') IpAddressWithLeadZero

This is so one could sort or filter based on a specific range

so only return those greater than 012.123.060.165 (not the most efficient though)

WHERE lpad(A.Seg,3,'0') ||'.'|| lpad(B.Seg,3,'0')||'.'|| lpad(C.Seg,3,'0')||'.'|| lpad(D.Seg,3,'0') >='012.123.060.165'

Possibly more efficient:

WHERE (A.Seg = 12 and B.Seg = 123 and C.Seg = 60 and D.Seg > 165) OR (A.Seg = 12 and B.Seg = 123 and C.Seg > 60) OR (A.Seg = 12 and B.Seg > 123) OR (A.Seg > 12)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.