Last active
February 28, 2017 22:13
-
-
Save robcowie/84c7ec4cb8b32c7801d8b8d8297f46c9 to your computer and use it in GitHub Desktop.
IP to Numeric with obfuscation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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