forked from pybites/challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from request_db import request_as_prefixes, request_as_neighbor | ||
from tabulate import tabulate | ||
|
||
|
||
def as_prefixes(AS): | ||
descr = [] | ||
route = [] | ||
list_prefix_data = request_as_prefixes(AS) | ||
i = 1 | ||
for diction in list_prefix_data: | ||
if (diction['name'] == 'route' or diction['name'] == 'route6') and i == 0: | ||
descr.append('') | ||
route.append(diction["value"]) | ||
i = 0 | ||
elif diction['name'] == 'descr': | ||
descr.append(diction["value"]) | ||
i = 1 | ||
elif (diction['name'] == 'route' or diction['name'] == 'route6') and i == 1: | ||
route.append(diction["value"]) | ||
i = 0 | ||
if len(descr) < len(route): | ||
descr.append('') | ||
prefix_tuple = tuple(zip(range(1, len(route)+1), route, descr)) | ||
return prefix_tuple | ||
|
||
|
||
def as_neighbor(AS): | ||
dict_data_neighbor = request_as_neighbor(AS) | ||
return dict_data_neighbor | ||
|
||
|
||
def main(): | ||
columns = ['№', 'Route', 'Description'] | ||
AS = input('Please, insert AS number: ') | ||
print() | ||
print(f'AS has {as_neighbor(AS)["left"]} uplink AS and has {as_neighbor(AS)["right"]} downlink clients AS') | ||
print() | ||
print(f'AS has {len(as_prefixes(AS))} public prefixes registered') | ||
print() | ||
print(tabulate(as_prefixes(AS), headers=columns, tablefmt="grid")) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# RIPE NCC Request Tool | ||
|
||
This script print table with all public prefixes that belong particular AS | ||
from AS pool managed by RIPE NCC(European Regional Internet Registries) | ||
|
||
# Description | ||
|
||
Written script make request to RIPE NCC DataBase(http://rest.db.ripe.net/) | ||
and try to retrieve all public prefixes that belongs to public AS. If request | ||
return response with error script print 'Incorrect AS number' and empty table. | ||
Database request in file request_db.by. | ||
Parsing json output in file program.py | ||
|
||
For pretty printing used module named tabulate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import json | ||
import requests | ||
|
||
|
||
def request_as_prefixes(AS): | ||
prefix_r = requests.get(f'https://rest.db.ripe.net/search.json?inverse-attribute=origin&source=ripe&query-string' | ||
f'=AS{AS}&types=route') | ||
prefix_r.raise_for_status() | ||
data = json.loads(prefix_r.text) | ||
data_list = [row_at for row in data['objects']['object'] for row_at in row['attributes']['attribute']] | ||
common_data_list = [diction for diction in data_list if diction['name'] == 'route' or diction['name'] == 'route6' | ||
or diction['name'] == 'descr'] | ||
return common_data_list | ||
|
||
|
||
def request_as_neighbor(AS): | ||
as_neighbor_r = requests.get(f'https://stat.ripe.net/data/asn-neighbours/data.json?resource={AS}') | ||
as_neighbor_r.raise_for_status() | ||
data = as_neighbor_r.json() | ||
as_neighbor = data['data']['neighbour_counts'] | ||
return as_neighbor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
requests==2.20.0 | ||
tabulate==0.8.2 |