From dbe09db841e4ac5f42d71539c50e8f33219b83fe Mon Sep 17 00:00:00 2001 From: smolkik_adm Date: Thu, 3 Sep 2026 03:08:02 +0000 Subject: [PATCH] powershell ver. --- agents_ip.ps1 | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 agents_ip.ps1 diff --git a/agents_ip.ps1 b/agents_ip.ps1 new file mode 100644 index 0000000..7e80c34 --- /dev/null +++ b/agents_ip.ps1 @@ -0,0 +1,80 @@ +$BASE_URL = "https://nameserver:9877" + +# Отключаем проверку SSL-сертификатов +add-type @" +using System.Net; +using System.Security.Cryptography.X509Certificates; +public class TrustAllCertsPolicy : ICertificatePolicy { + public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate cert, WebRequest req, int certProblem) { return true; } +} +"@ +[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy +[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12 + +# Запрос логина и пароля +$username = Read-Host "Введите имя пользователя" +$password = Read-Host "Введите пароль" -AsSecureString +$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password) +$plainPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) + +if (-not $username -or -not $plainPassword) { + Write-Host "Не введён User или Password" -ForegroundColor Red + exit 1 +} + +# Аутентификация +$authUrl = "$BASE_URL/idp/token" +$body = "username=$([uri]::EscapeDataString($username))&password=$([uri]::EscapeDataString($plainPassword))&grant_type=password" + +try { + $authResponse = Invoke-RestMethod -Uri $authUrl -Method Post -Body $body -ContentType "application/x-www-form-urlencoded" +} catch { + Write-Host "Ошибка аутентификации: $_" -ForegroundColor Red + exit 1 +} + +if (-not $authResponse.token_type -or -not $authResponse.access_token) { + Write-Host "Ошибка: в ответе отсутствует токен" -ForegroundColor Red + exit 1 +} + +$headers = @{ + "Content-Type" = "application/json" + "Authorization" = "$($authResponse.token_type) $($authResponse.access_token)" +} + +# Получение списка агентов +try { + Write-Host "Сбор информации об агентах..." + $agentsUrl = "$BASE_URL/api/resource_manager/v1/agents" + $response = Invoke-RestMethod -Uri $agentsUrl -Method Get -Headers $headers +} catch { + if ($_.Exception.Response.StatusCode.value__ -eq 401) { + Write-Host "Ошибка 401: токен недействителен или истёк." -ForegroundColor Red + } else { + Write-Host "Ошибка: $_" -ForegroundColor Red + } + exit 1 +} + +$agents = $response.items | Where-Object { $_.type -eq "agent" } | ForEach-Object { + [PSCustomObject]@{ + Name = $_.name + Type = $_.type + Online = $_.communication.online + OS = $_.details.os.name + IP = ($_.details.ipAddresses -join ", ") + } +} + +Write-Host "" +Write-Host "Всего найдено агентов/устройств: $($agents.Count)" -ForegroundColor Green +Write-Host "" + +$agents | Format-Table -Property @( + @{Label="Имя устройства"; Expression={$_.Name}; Width=40} + @{Label="Тип"; Expression={$_.Type}; Width=15} + @{Label="Статус"; Expression={if($_.Online){"Онлайн"}else{"Офлайн"}}; Width=8} + @{Label="ОС"; Expression={$_.OS}; Width=30} + @{Label="IP-адреса"; Expression={$_.IP}} +) -AutoSize \ No newline at end of file