code_junkie

Using SQL to determine cidr value of a subnet mask

Nov 12th, 2011
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. select concat(inet_ntoa(ip_addr),'-',
  2. inet_ntoa(ip_addr+(POWER(2,32)-ip_mask-1))) range
  3. from subnets
  4. order by ip_addr
  5.  
  6. CREATE TABLE cidr (
  7. bits INT UNSIGNED PRIMARY KEY,
  8. mask INT UNSIGNED NOT NULL
  9. );
  10.  
  11. INSERT INTO cidr (bits) VALUES
  12. ( 1), ( 2), ( 3), ( 4), ( 5), ( 6), ( 7), ( 8), ( 9), (10),
  13. (11), (12), (13), (14), (15), (16), (17), (18), (19), (20),
  14. (21), (22), (23), (24), (25), (26), (27), (28), (29), (30),
  15. (31), (32);
  16.  
  17. UPDATE cidr SET mask = ((POWER(2,32)-1)<<(32-bits)) & (POWER(2,32)-1);
  18.  
  19. SELECT CONCAT(s.ip_addr, '/', c.bits)
  20. FROM cidr c JOIN subnets s ON (c.mask = inet_aton(s.ip_mask));
  21.  
  22. select CONCAT(INET_NTOA(ip_addr),'/',32-log2((4294967296-ip_mask))) net
  23. from subnets
  24. order by ip_addr
Add Comment
Please, Sign In to add comment