bgp

- research & experiments with border gateway protocol
git clone git://git.acid.vegas/bgp.git
Log | Files | Refs | Archive | README

ip_asn.py (3109B)

      1 __author__ = "Pooja Pathak"
      2 __email__ = "<pmpathak@ucsd.edu>"
      3 # This software is Copyright © 2020 The Regents of the University of
      4 # California. All Rights Reserved. Permission to copy, modify, and
      5 # distribute this software and its documentation for educational, research
      6 # and non-profit purposes, without fee, and without a written agreement is
      7 # hereby granted, provided that the above copyright notice, this paragraph
      8 # and the following three paragraphs appear in all copies. Permission to
      9 # make commercial use of this software may be obtained by contacting:
     10 #
     11 # Office of Innovation and Commercialization
     12 #
     13 # 9500 Gilman Drive, Mail Code 0910
     14 #
     15 # University of California
     16 #
     17 # La Jolla, CA 92093-0910
     18 #
     19 # (858) 534-5815
     20 #
     21 # invent@ucsd.edu
     22 #
     23 # This software program and documentation are copyrighted by The Regents of
     24 # the University of California. The software program and documentation are
     25 # supplied “as is”, without any accompanying services from The Regents. The
     26 # Regents does not warrant that the operation of the program will be
     27 # uninterrupted or error-free. The end-user understands that the program
     28 # was developed for research purposes and is advised not to rely
     29 # exclusively on the program for any reason.
     30 #
     31 # IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
     32 # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
     33 # INCLUDING LOST PR OFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
     34 # DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF
     35 # THE POSSIBILITY OF SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY
     36 # DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     37 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
     38 # SOFTWARE PROVIDED HEREUNDER IS ON AN “AS IS” BASIS, AND THE UNIVERSITY OF
     39 # CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
     40 # ENHANCEMENTS, OR MODIFICATIONS.
     41 
     42 #!/usr/bin/env python
     43 
     44 import pyasn
     45 import argparse 
     46 import datetime
     47 import resource
     48 import os
     49 import psutil
     50 
     51 def returnTime():
     52     return datetime.datetime.now()
     53 
     54 def returnMemUsage():
     55     process = psutil.Process(os.getpid())
     56     return process.memory_info()[0]
     57     
     58 
     59 parser = argparse.ArgumentParser()
     60 parser.add_argument('-p', dest = 'prefix2asn_file', default = '', help = 'Please enter the prefix2asn file name')
     61 parser.add_argument('-i', dest = 'ips_file', default = '', help = 'Please enter the file name of the ips file')
     62 args = parser.parse_args()
     63 
     64 
     65 # Get list of ips 
     66 ips = []
     67 with open(args.ips_file) as f:
     68     for line in f:
     69         line = line.rstrip().split("\t")[1]
     70         ips.append(line)
     71 
     72 
     73 asndb = pyasn.pyasn(args.prefix2asn_file)
     74 
     75 begin_time = returnTime()
     76 begin_mem = returnMemUsage() 
     77 
     78 # Create ip2asn mapping
     79 ip2asn = {}
     80 for ip in ips:
     81     if asndb.lookup(ip):
     82         asn,prefix =  asndb.lookup(ip)
     83         if asn:
     84              ip2asn[ip] = asn
     85 
     86 # print(ip2asn)
     87 end_time = returnTime()
     88 end_mem = returnMemUsage()
     89 
     90 # hour:minute:second:microsecond
     91 print("Delta time:" , end_time - begin_time)
     92 print("Delta memory use:", end_mem - begin_mem)
     93 
     94     
     95