#!/usr/bin/env python3 """DNS и прокси настройки сетевых служб macOS.""" from __future__ import annotations import argparse import json import re import subprocess from pathlib import Path PROXY_COMMANDS = { "web": "-getwebproxy", "secure_web": "-getsecurewebproxy", "ftp": "-getftpproxy", "streaming": "-getstreamingproxy", "socks": "-getsocksfirewallproxy", } def run(command: list[str]) -> str: result = subprocess.run(command, capture_output=True, text=True, check=False) return result.stdout def services() -> list[str]: output = run(["networksetup", "-listallnetworkservices"]) result = [] for line in output.splitlines(): line = line.strip() if not line or line.startswith("An asterisk"): continue result.append(line.lstrip("*").strip()) return result def parse_proxy(text: str) -> dict: values: dict[str, str] = {} for line in text.splitlines(): key, separator, value = line.partition(":") if separator: values[key.strip()] = value.strip() return values def dns_for(service: str) -> list[str]: output = run(["networksetup", "-getdnsservers", service]) if "aren't any DNS Servers" in output: return [] return [line.strip() for line in output.splitlines() if line.strip()] def scutil_proxy() -> dict[str, str]: output = run(["scutil", "--proxy"]) values: dict[str, str] = {} for line in output.splitlines(): match = re.match(r"\s*([A-Za-z0-9]+)\s*:\s*(.*)", line) if match: values[match.group(1)] = match.group(2).strip() return values def main() -> int: parser = argparse.ArgumentParser(description="DNS и прокси настройки macOS") parser.add_argument("--service", help="Только одна служба, например Wi-Fi") parser.add_argument("--json", type=Path) args = parser.parse_args() all_services = services() if args.service: all_services = [name for name in all_services if name == args.service] if not all_services: parser.error(f"Служба не найдена: {args.service}") report = {"services": [], "scutil_proxy": scutil_proxy()} for service in all_services: proxies = {} for name, command in PROXY_COMMANDS.items(): proxies[name] = parse_proxy(run(["networksetup", command, service])) report["services"].append( { "name": service, "dns": dns_for(service), "proxies": proxies, } ) if args.json: args.json.write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n", encoding="utf-8") for service in report["services"]: print(f"\nСлужба: {service['name']}") print(f" DNS: {', '.join(service['dns']) if service['dns'] else 'автоматически'}") for name, values in service["proxies"].items(): enabled = values.get("Enabled", "No") server = values.get("Server", "") port = values.get("Port", "") detail = f" {server}:{port}" if server else "" print(f" proxy {name}: {enabled}{detail}") print("\nscutil --proxy:") if report["scutil_proxy"]: for key, value in report["scutil_proxy"].items(): print(f" {key}: {value}") else: print(" данные недоступны") return 0 if __name__ == "__main__": raise SystemExit(main())