py-netaddr - replace netaddr for standard ipaddress package in cases where it's not required to search the mac databases. closes https://github.com/opnsense/core/issues/7415

This commit is contained in:
Ad Schellevis 2024-04-27 19:14:39 +02:00
parent dd46067d73
commit 67f6aeed2b
3 changed files with 12 additions and 13 deletions

View File

@ -33,14 +33,15 @@ import subprocess
import os
import sys
import ujson
from netaddr import IPAddress, AddrFormatError
import ipaddress
if __name__ == '__main__':
# IP should have been passed as a command line argument
if len(sys.argv) >= 1:
try:
IPAddress(sys.argv[1])
ipaddress.ip_address(sys.argv[1])
result = {'status': 'ok', 'matches': []}
tables = []
@ -56,7 +57,7 @@ if __name__ == '__main__':
result['matches'].append(table)
print(ujson.dumps(result))
except AddrFormatError:
except ValueError:
print(ujson.dumps({'status': 'Invalid IP specified!'}))
else:

View File

@ -29,16 +29,15 @@
import argparse
import asyncio
import decimal
import ipaddress
import subprocess
import os
import sys
import math
import ujson
import netaddr
import dns.resolver
from dns.asyncresolver import Resolver
from concurrent.futures import ThreadPoolExecutor
from netaddr import IPNetwork, IPAddress, AddrFormatError
def iftop(interface, target):
@ -57,11 +56,11 @@ def local_addresses():
for line in sp.stdout.split('\n'):
if line.find('\tinet') > -1:
try:
ip = IPAddress(line.split()[1].split('%')[0])
except AddrFormatError:
ip = ipaddress.ip_address(line.split()[1].split('%')[0])
except ValueError:
ip = None
if ip and not ip.is_loopback() and not ip.is_link_local():
if ip and not ip.is_loopback and not ip.is_link_local:
result.append(ip)
return result
@ -148,10 +147,10 @@ if __name__ == '__main__':
}
# attach tags (type of address)
try:
ip = IPAddress(parts[0])
ip = ipaddress.ip_address(parts[0])
if ip in all_local_addresses:
item['tags'].append('local')
if not ip.is_global():
if not ip.is_global:
item['tags'].append('private')
except subprocess.TimeoutExpired:
pass

View File

@ -28,8 +28,7 @@ import os
import datetime
import glob
import collections
import netaddr
import ipaddress
class SortKeyHelper:
""" generate item key for sort function
@ -158,7 +157,7 @@ class Helpers(object):
:param network: network
:return: IPNetwork generated by netaddr
"""
return netaddr.IPNetwork(network)
return ipaddress.ip_network(network, False)
@staticmethod
def getUtcTime():