|
40 | 40 | import logging |
41 | 41 | import platform |
42 | 42 | import subprocess |
43 | | -from privex.helpers.exceptions import BoundaryException |
| 43 | +from privex.helpers.exceptions import BoundaryException, NetworkUnreachable |
44 | 44 | from privex.helpers import plugin |
45 | 45 | from ipaddress import ip_address, IPv4Address, IPv6Address |
46 | 46 | from typing import Union |
47 | 47 |
|
48 | 48 | log = logging.getLogger(__name__) |
49 | 49 |
|
50 | 50 | try: |
51 | | - from dns.resolver import Resolver, NoAnswer |
| 51 | + from dns.resolver import Resolver, NoAnswer, NXDOMAIN |
52 | 52 |
|
53 | 53 | def asn_to_name(as_number: Union[int, str], quiet: bool = True) -> str: |
54 | 54 | """ |
@@ -83,7 +83,7 @@ def asn_to_name(as_number: Union[int, str], quiet: bool = True) -> str: |
83 | 83 | asname = str(res[0]).strip('"').split('|')[-1:][0].strip() |
84 | 84 | return str(asname) |
85 | 85 | raise NoAnswer('privex.helpers.net.asn_to_name returned no results.') |
86 | | - except NoAnswer: |
| 86 | + except (NoAnswer, NXDOMAIN): |
87 | 87 | if quiet: |
88 | 88 | return 'Unknown ASN' |
89 | 89 | raise KeyError('ASN {} was not found, or server did not respond.'.format(as_number)) |
@@ -262,5 +262,10 @@ def ping(ip: str, timeout: int = 30) -> bool: |
262 | 262 | if platform.system() not in opts: |
263 | 263 | raise NotImplementedError(f"{__name__}.ping is not fully supported on platform '{platform.system()}'...") |
264 | 264 |
|
265 | | - with subprocess.Popen(opts[platform.system()] + [ip], stdout=subprocess.PIPE) as proc: |
266 | | - return 'bytes from {}'.format(ip) in proc.stdout.read().decode('utf-8') |
| 265 | + with subprocess.Popen(opts[platform.system()] + [ip], stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc: |
| 266 | + out, err = proc.communicate() |
| 267 | + err = err.decode('utf-8') |
| 268 | + if 'network is unreachable' in err.lower(): |
| 269 | + raise NetworkUnreachable(f'Got error from ping: "{err}"') |
| 270 | + |
| 271 | + return 'bytes from {}'.format(ip) in out.decode('utf-8') |
0 commit comments