Add the script to get list of the networks from network names

This commit is contained in:
Be
2024-03-01 10:55:02 +01:00
parent fa40cde691
commit c377b4048b
5 changed files with 1352 additions and 9 deletions

View File

@@ -1,10 +1,12 @@
# AS Network List # Get network lists from AS name or Network name
## Description ## Description
This Python script retrieves and prints the network prefixes announced by a specified Autonomous System (AS). It leverages the RIPE Stat Data API to fetch the data. * If you know AS name, use the script `network_list_from_as.py` to get the list of networks.
* If you know the network name, use the script `network_list_from_netname.py` to get the list of networks.
**Note:** This script has been tested on MacOS and Linux.
**Note:** This scripts has been tested on MacOS and Linux.
## Prerequisites ## Prerequisites
@@ -30,27 +32,52 @@ This Python script retrieves and prints the network prefixes announced by a spec
./requirements.sh ./requirements.sh
``` ```
## Usage ## Usage of the `network_list_from_as.py` script
1. Run the script with the AS number as an argument: 1. Run the script with the AS number as an argument:
```bash ```bash
python as_network_list.py AS61280 python network_list_from_as.py AS61280
``` ```
2. To disable all output except the prefixes, use the `--quiet` or `-q` switch: 2. To disable all output except the prefixes, use the `--quiet` or `-q` switch:
```bash ```bash
python as_network_list.py AS61280 --quiet python network_list_from_as.py AS61280 -q
``` ```
3. To print a help message, use the `-h` or `--help` switch: 3. To print a help message, use the `-h` or `--help` switch:
```bash ```bash
python as_network_list.py --help python network_list_from_as.py --help
``` ```
## Screenshot ## Usage of the `network_list_from_netname.py` script
1. Run the script with the list of network names in a file as an argument:
```bash
python network_list_from_netname.py files/blacklist4.txt
```
2. Run the script with the list of network names in the github repository as an argument:
```bash
python network_list_from_netname.py https://github.com/AntiZapret/antizapret/blob/master/blacklist4.txt
```
or better use the raw file link:
```bash
python network_list_from_netname.py https://raw.githubusercontent.com/AntiZapret/antizapret/master/blacklist4.txt
```
3. To print a help message, use the `-h` or `--help` switch:
```bash
python network_list_from_netname.py --help
```
## Screenshots
![Screenshot](https://github.com/C24Be/AS_Network_List/assets/153936414/574b072c-9104-4e02-b2c0-3609433bdfc4) ![Screenshot](https://github.com/C24Be/AS_Network_List/assets/153936414/574b072c-9104-4e02-b2c0-3609433bdfc4)

1238
files/blacklist4.txt Normal file

File diff suppressed because it is too large Load Diff

77
network_list_from_netname.py Executable file
View File

@@ -0,0 +1,77 @@
#!/usr/bin/env python3
import socket
import argparse
import requests
import ipaddress
import re
whois_server = "whois.ripe.net"
def whois_query(whois_server, query):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((whois_server, 43))
# Prepare the query
query = f"{query}\r\n"
s.send(query.encode())
# Collect the response
response = ''
while True:
data = s.recv(4096)
try:
response += data.decode('utf-8')
except:
response += data.decode('latin-1')
if not data:
break
s.close()
# Extract the inetnum line
for line in response.split('\n'):
if line.startswith('inetnum'):
return line.strip()
return None
def convert_to_raw_github_url(url):
return url.replace("https://github.com/", "https://raw.githubusercontent.com/").replace("/blob", "")
def convert_to_cidr(ip_range):
start_ip, end_ip = ip_range.split(' - ')
start_ip = ipaddress.IPv4Address(start_ip)
end_ip = ipaddress.IPv4Address(end_ip)
cidrs = ipaddress.summarize_address_range(start_ip, end_ip)
return [str(cidr) for cidr in cidrs]
def extract_netname(filename_or_url):
if filename_or_url.startswith('http://') or filename_or_url.startswith('https://'):
if 'github.com' in filename_or_url:
filename_or_url = convert_to_raw_github_url(filename_or_url)
response = requests.get(filename_or_url)
lines = response.text.split('\n')
else:
with open(filename_or_url, 'r') as file:
lines = file.readlines()
for line in lines:
if re.match(r'.*netname:', line):
response = whois_query(whois_server, line.split(':')[1].strip())
if response is not None:
ip_range = response.split(':')[1].strip()
cidrs = convert_to_cidr(ip_range)
for cidr in cidrs:
print(cidr)
return None
def main():
parser = argparse.ArgumentParser(description='Extract netname from file.')
parser.add_argument('filename_or_url', help='The file to extract netname from.')
args = parser.parse_args()
extract_netname(args.filename_or_url)
if __name__ == "__main__":
main()

View File

@@ -2,5 +2,6 @@ certifi==2024.2.2
charset-normalizer==3.3.2 charset-normalizer==3.3.2
cymruwhois==1.6 cymruwhois==1.6
idna==3.6 idna==3.6
netaddr==1.2.1
requests==2.31.0 requests==2.31.0
urllib3==2.2.1 urllib3==2.2.1