bgp

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

download_asn_paths.py (3127B)

      1 #!/usr/bin/env python
      2 __author__ = "Pooja Pathak"
      3 __email__ = "<pmpathak@ucsd.edu>"
      4 # This software is Copyright © 2020 The Regents of the University of
      5 # California. All Rights Reserved. Permission to copy, modify, and
      6 # distribute this software and its documentation for educational, research
      7 # and non-profit purposes, without fee, and without a written agreement is
      8 # hereby granted, provided that the above copyright notice, this paragraph
      9 # and the following three paragraphs appear in all copies. Permission to
     10 # make commercial use of this software may be obtained by contacting:
     11 #
     12 # Office of Innovation and Commercialization
     13 #
     14 # 9500 Gilman Drive, Mail Code 0910
     15 #
     16 # University of California
     17 #
     18 # La Jolla, CA 92093-0910
     19 #
     20 # (858) 534-5815
     21 #
     22 # invent@ucsd.edu
     23 #
     24 # This software program and documentation are copyrighted by The Regents of
     25 # the University of California. The software program and documentation are
     26 # supplied “as is”, without any accompanying services from The Regents. The
     27 # Regents does not warrant that the operation of the program will be
     28 # uninterrupted or error-free. The end-user understands that the program
     29 # was developed for research purposes and is advised not to rely
     30 # exclusively on the program for any reason.
     31 #
     32 # IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
     33 # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
     34 # INCLUDING LOST PR OFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
     35 # DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF
     36 # THE POSSIBILITY OF SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY
     37 # DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     38 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
     39 # SOFTWARE PROVIDED HEREUNDER IS ON AN “AS IS” BASIS, AND THE UNIVERSITY OF
     40 # CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
     41 # ENHANCEMENTS, OR MODIFICATIONS.
     42 
     43 #!/usr/bin/env python
     44 
     45 import pybgpstream
     46 import os.path
     47 
     48 # Create pybgpstream
     49 stream = pybgpstream.BGPStream(
     50     from_time="2017-07-07 00:00:00", until_time="2017-07-07 00:10:00 UTC",
     51     collectors=["route-views.sg", "route-views.eqix"],
     52     record_type="updates",  
     53 )
     54 
     55 prefix_asn = dict()
     56 for elem in stream:
     57     # record fields can be accessed directly from elem
     58     if "as-path" in elem.fields:
     59         asns = elem.fields["as-path"].rstrip().split(" ")
     60     if "prefix" in elem.fields:
     61         prefix = elem.fields["prefix"]
     62     
     63 
     64     if len(asns) < 1:
     65         continue
     66 
     67     # Get origin as 
     68     asn = asns[-1]
     69 
     70     # Drop origin as sets
     71     if len(asn.split(",")) > 1:
     72         continue 
     73 
     74     if asn[0] == '{':
     75         continue
     76 
     77     # Populate prefix_asn with prefix to asn mapping
     78     if asn not in prefix_asn:
     79         prefix_asn[prefix] = set()
     80     prefix_asn[prefix].add(asn)
     81 
     82 # Write prefix-asn mapping to prefix2asn.dat
     83 
     84 fout = open('prefix2asn.dat', "w")
     85 for prefix,asns in prefix_asn.items():
     86     if len(asns) == 1:
     87         fout.write(prefix)
     88         fout.write("\t")
     89         fout.write("".join(prefix_asn[prefix]))
     90         fout.write("\n")
     91 
     92 fout.close()