Skip to content

Instantly share code, notes, and snippets.

@kosztik kosztik/aton.py

Last active Mar 5, 2020
Embed
What would you like to do?
#!/usr/bin/env python
import socket
import struct
import sys
import os
def ip2int(addr):
return struct.unpack("!I", socket.inet_aton(addr))[0]
def int2ip(addr):
return socket.inet_ntoa(struct.pack("!I", addr))
filepath = './ip.txt'
with open(filepath) as fp:
line = fp.readline()
cnt = 1
while line:
#print("Line {}: {}".format(cnt, line.strip()))
line = fp.readline()[:-1]
arr = line.split("-")
print "\""+arr[0]+"\""+","+"\""+arr[1]+"\",\""+str(ip2int(arr[0]))+"\""+","+"\""+str(ip2int(arr[1]))+"\",\"HU\",\"Hungary\""
#print "\""+str(ip2int(arr[0]))+"\""+","+"\""+str(ip2int(arr[1]))+"\",\"HU\""
cnt += 1
#print(int2ip(0xc0a80164)) # 192.168.1.100
#print(ip2int('174.36.207.186')) # 167772161
@kosztik

This comment has been minimized.

Copy link
Owner Author

kosztik commented Mar 5, 2020

output format:

"2.16.0.0","2.16.5.255","34603008","34604543","EU","Europe"
"2.16.6.0","2.16.7.255","34604544","34605055","DE","Germany"
"2.16.8.0","2.16.67.255","34605056","34620415","EU","Europe"
"2.16.68.0","2.16.69.255","34620416","34620927","SE","Sweden"
"2.16.70.0","2.16.71.255","34620928","34621439","IT","Italy"

to .csv

then
./csv2dat.py -w ./n.dat mmcountry ./n.txt
with mmutils (https://github.com/mteodoro/mmutils)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.