Question: Need help with python code Step 1 : Download the compressed table DB_091803_v1.txt and test input file: IPlist.txt. Upload them to your Colab space using

Need help with python code

Step 1: Download the compressed table DB_091803_v1.txt and test input file: IPlist.txt. Upload them to your Colab space using the code below.

from google.colab import files

uploaded = files.upload()

DB = next(iter(uploaded))

print(DB, "uploaded")

uploaded = files.upload()

IP_LIST = next(iter(uploaded))

print(IP_LIST, "uploaded")

Step 2: Develop the IP2AS tool - This tool maps an IP address to an AS. It uses static table address prefix to AS number collected from whois DB and BGP tables. This table is stored in a file and should be given to the tool as a parameter. It will perform longest prefix matching and will map the IP to an AS number. The tool should print out the longest prefix that the IP address is matched to, and the corresponding AS number.

Steps:

1. Put the set of IP addresses you want to map to ASes into the . You can list one IP address per line.

For example, look at IPlist.txt file, which contains the following:

 169.237.33.90 208.30.172.70

2. The has data about which address block belongs to a particular AS (look at DB_091803_v1.txt file, for example). The is constructed based on IRR database and BGP routing table.

3. Run ip2as and specify the and

For example, if you run your code, the output should look like the following:

169.237.0.0/16 1852 169.237.33.90 208.0.0.0/11 1239 208.30.172.70

My code is below and output is below:

import re

def ip2as(db_filename, input_filename):

# Read the database file and store the address prefixes and corresponding AS numbers in a dictionary db_file = open(db_filename, "r") db = {} for line in db_file: line = re.split(r'[\s]+', line.strip()) db[line[0]] = line[1]

# Read the input file. # For each IP address, find the longest prefix match and output the prefix and corresponding AS number. input_file = open(input_filename, "r") for ip_address in input_file: ip_address = ip_address.strip() longest_prefix = "" for prefix in db: if ip_address.startswith(prefix) and len(prefix) > len(longest_prefix): longest_prefix = prefix if longest_prefix: print(f"{longest_prefix} {db[longest_prefix]} {ip_address}") else: print(f"No match found for {ip_address}")

ip2as(DB_091803_v1.txt, IPlist.txt)

Output:

Traceback (most recent call last): File "C:\Users\varun\Desktop\P\MS\CN\ip2as4.py", line 26, in ip2as(DB_091803_v1.txt, IPlist.txt) NameError: name 'DB_091803_v1' is not defined

But the files are in the same directory

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!