Skip to content

3451. Find Invalid IP Addresses

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
WITH
  InvalidIPs AS (
    SELECT ip
    FROM Logs
    WHERE
      LENGTH(ip) - LENGTH(REPLACE(ip, '.', '')) != 3
      OR ip REGEXP '(^|\\.)0[0-9]'
      OR ip REGEXP '(^|\\.)([0-9]{4,}|[3-9][0-9]{2}|2[6-9][0-9]|25[6-9])(\\.|$)'
  )
SELECT ip, COUNT(*) AS invalid_count
FROM InvalidIPs
GROUP BY ip
ORDER BY invalid_count DESC, ip DESC;