Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ipv4 to asn recipe to use donwloaded pfx2as file #726

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 32 additions & 12 deletions sources/recipe/map_ipv4_address_to_asn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,49 +45,69 @@ Detailed installation and usage instructions [here]( https://github.com/CAIDA/py

The following script returns a dictionary `ip2asn` that maps ips to origin asns.

### Map between ips and origin asns using PyIPMeta
### Map between IPs and origin ASNs using PyIPMeta

For this solution, clone **PyIPMeta** from [here]( https://github.com/CAIDA/pyipmeta)
More data can be found at http://data.caida.org/datasets/routing/routeviews-prefix2as/
For this solution, clone **PyIPMeta** from [here]( https://github.com/CAIDA/pyipmeta).

Sample ips.txt found [here]( http://data.caida.org/datasets/topology/ark/ipv4/dns-names/2019/05/dns-names.l7.20190501.txt.gz)
Download a prefix2asn file by following the directions [here](https://catalog.caida.org/dataset/routeviews_prefix2as). When using the script, pass in the file name after the `-p` flag.
bhuffaker marked this conversation as resolved.
Show resolved Hide resolved

**Usage** : `$ python3 ip_asn.py -i ips.txt`
Sample lists of IPs found [here]( http://publicdata.caida.org/datasets/topology/ark/ipv4/dns-names/2019/05/dns-names.l7.20190501.txt.gz).
bhuffaker marked this conversation as resolved.
Show resolved Hide resolved

**Usage** : `$ python3 ip_asn_pyipmeta.py -p <pfx2as file> -i <ips txt file>`

~~~python
import _pyipmeta
import argparse
import datetime
import os
import psutil

parser = argparse.ArgumentParser()
parser.add_argument('-i', dest = 'ips_file', default = '', help = 'Please enter the file name of the ips file')
args = parser.parse_args()
def returnTime():
return datetime.datetime.now()

def returnMemUsage():
process = psutil.Process(os.getpid())
return process.memory_info()[0]


ipm = _pyipmeta.IpMeta()
# print(ipm)

parser = argparse.ArgumentParser()
parser.add_argument('-p', dest = 'prefix2asn_file', default = '', help = 'Please enter the prefix2asn file name')
parser.add_argument('-i', dest = 'ips_file', default = '', help = 'Please enter the file name of the ips file')
args = parser.parse_args()

# print("Getting/enabling pfx2as provider (using included test data)")
prov = ipm.get_provider_by_name("pfx2as")
print(ipm.enable_provider(prov, "-f http://data.caida.org/datasets/routing/routeviews-prefix2as/2017/03/routeviews-rv2-20170329-0200.pfx2as.gz"))
print(ipm.enable_provider(prov, f"-f {args.prefix2asn_file}"))
print()

# Create list of ips from test file
# Create list of ips from test file
ips = []
with open(args.ips_file) as f:
for line in f:
line = line.rstrip().split("\t")[1]
ips.append(line)

begin_time = returnTime()
begin_mem = returnMemUsage()

# Map between ipv4 addresses and origin asns
ip2asn = {}
for ip in ips:
if ipm.lookup(ip):
(res,) = ipm.lookup(ip)
if res.get('asns'):
ip2asn[ip] = res.get('asns')[-1]
ip2asn[ip] = res.get('asns')

print(ip2asn)
end_time = returnTime()
end_mem = returnMemUsage()

# print(ip2asn)
# hour:minute:second:microsecond
print("Delta time:" , end_time - begin_time)
print("Delta memory:", end_mem - begin_mem)
~~~

### Background
Expand Down
17 changes: 11 additions & 6 deletions sources/recipe/map_ipv4_address_to_asn/ip_asn_pyipmeta.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#!/usr/bin/env python

import _pyipmeta
import argparse
import datetime
import os
import psutil
Expand All @@ -60,31 +61,35 @@ def returnMemUsage():
ipm = _pyipmeta.IpMeta()
# print(ipm)

parser = argparse.ArgumentParser()
parser.add_argument('-p', dest = 'prefix2asn_file', default = '', help = 'Please enter the prefix2asn file name')
parser.add_argument('-i', dest = 'ips_file', default = '', help = 'Please enter the file name of the ips file')
args = parser.parse_args()

# print("Getting/enabling pfx2as provider (using included test data)")
prov = ipm.get_provider_by_name("pfx2as")
# print(prov)
print(ipm.enable_provider(prov, "-f /test/pfx2as/routeviews-rv2-20170329-0200.pfx2as.gz"))
print(ipm.enable_provider(prov, f"-f {args.prefix2asn_file}"))
print()


# Create list of ips from test file
ips = []
with open('ips.txt') as f:
with open(args.ips_file) as f:
for line in f:
line = line.rstrip().split("\t")[1]
ips.append(line)

begin_time = returnTime()
begin_mem = returnMemUsage()

# Map between ipv4 addresses and origin asns
ip2asn = {}
for ip in ips:
if ipm.lookup(ip):
(res,) = ipm.lookup(ip)
if res.get('asns'):
ip2asn[ip] = res.get('asns')


# print(ip2asn)
print(ip2asn)
end_time = returnTime()
end_mem = returnMemUsage()

Expand Down