Skip to content

Instantly share code, notes, and snippets.

@robcowie
Last active February 28, 2017 22:13
Show Gist options
  • Save robcowie/84c7ec4cb8b32c7801d8b8d8297f46c9 to your computer and use it in GitHub Desktop.
Save robcowie/84c7ec4cb8b32c7801d8b8d8297f46c9 to your computer and use it in GitHub Desktop.
IP to Numeric with obfuscation
example1 = 1118363648
def numeric_to_ip(ip):
parts = []
while ip:
parts.append(ip & 255)
ip = ip >> 8
return '.'.join([str(p) for p in reversed(parts)])
def ip_to_numeric(ip):
pass
-- For Apache Hive
-- IP to Numeric
SELECT (shiftleft(CAST(split(ip, '\\.')[0] AS int), 24) +
shiftleft(CAST(split(ip, '\\.')[1] AS int), 16) +
shiftleft(CAST(split(ip, '\\.')[2] AS int), 8) +
0) AS user_ip
FROM tbl;
-- Numeric to IP
SELECT printf('%d.%d.%d.%d',
shiftright(user_ip, 24) & 255,
shiftright(user_ip, 16) & 255,
shiftright(user_ip, 8) & 255,
user_ip & 255) as user_ip
FROM tbl;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment