import requests import urllib3 from getpass import getpass urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) BASE_URL = "https://nameserver:9877" def authenticate(username, password): auth_url = f"{BASE_URL}/idp/token" auth_payload = { "username": username, "password": password, "grant_type": "password", } try: auth_response = requests.post( auth_url, data=auth_payload, headers={"Content-Type": "application/x-www-form-urlencoded"}, proxies={"http": None, "https": None}, verify=False, ) auth_response.raise_for_status() auth_data = auth_response.json() if "token_type" not in auth_data or "access_token" not in auth_data: print("\033[91mОшибка: в ответе отсутствует токен\033[0m") return None return { "Content-Type": "application/json", "Authorization": f"{auth_data['token_type']} {auth_data['access_token']}", } except requests.exceptions.RequestException as e: print(f"\033[91mОшибка аутентификации: {e}\033[0m") return None def main(): username = input("Введите имя пользователя: ") password = getpass("Введите пароль: ") if not username or not password: print("\033[91mНе введён User или Password\033[0m") return headers = authenticate(username, password) if not headers: return try: print("📊 Сбор информации об агентах...") resources_url = f"{BASE_URL}/api/resource_manager/v1/agents" response = requests.get(resources_url, headers=headers, verify=False) if response.status_code == 401: print("❌ Ошибка 401: токен недействителен или истёк.") return elif response.status_code != 200: print(f"❌ Сервер вернул ошибку HTTP {response.status_code}") print(f"Ответ: {response.text}") return data = response.json() agents = [] for item in data.get("items", []): agent_type = item.get("type") if agent_type in ["agent"]: agents.append({ "name": item.get("name"), "type": agent_type, "online": item.get("communication", {}).get("online", False), "os": item.get("details", {}).get("os", {}).get("name", "N/A"), "ip": ", ".join(item.get("details", {}).get("ipAddresses", ["N/A"])), }) print(f"\n✅ Всего найдено агентов/устройств: {len(agents)}\n") print(f"{'Имя устройства':<40} | {'Тип':<15} | {'Статус':<8} | {'ОС':<30} | {'IP-адреса'}") print("-" * 120) for agent in agents: status = "Онлайн" if agent["online"] else "Офлайн" print( f"{agent['name']:<40} | " f"{agent['type']:<15} | " f"{status:<8} | " f"{agent['os']:<30} | " f"{agent['ip']}" ) except Exception as e: print(f"\n❌ Ошибка выполнения скрипта: {e}") if __name__ == "__main__": main()