首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过powershell使用ambari rest-api

通过powershell使用ambari rest-api
EN

Stack Overflow用户
提问于 2018-09-13 20:39:54
回答 1查看 160关注 0票数 0

由于我们的监控是在Windows平台上完成的,因此我们希望使用powershell从ambari-rest-api中检索信息。

在浏览器中可以浏览api。首先登录,然后可以粘贴使用的url:https://someazurenode.westeurope.cloudapp.azure.com/ambari/api/v1/clusters它只在浏览器中显示json响应。

在卷曲中:

代码语言:javascript
复制
curl --user myusr:mypwd --insecure -i -H 'X-Requested-By:ambari' -X GET https://someazurenode.westeurope.cloudapp.azure.com/ambari/api/v1/clusters

工作正常

在powershell中(禁用ssl验证**后):

代码语言:javascript
复制
$cred = New-Object System.Management.Automation.PSCredential ("myusr", (ConvertTo-SecureString "mypwd" -AsPlainText -Force))
Invoke-WebRequest -Method Get `
  -UseBasicParsing `
  -Uri "https://someazurenode.westeurope.cloudapp.azure.com/api/v1/clusters" -Headers @{"X-Requested-By"="Ambari"} `
  -Credential $cred

--> 404

这似乎与授权有关,因此我尝试了下面的选项(**):

代码语言:javascript
复制
Invoke-WebRequest -Method GET `
  -Uri "https://someazurenode.westeurope.cloudapp.azure.com/api/v1/clusters" `
  -Headers @{Authorization =[Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes('myusr:mypwd'))}

-->找不到404

**忽略ssl验证的灵感:Ignoring Self-Signed Certificates from Powershell Invoke-RestMethod doesn't work (it's changed again...)

**处理基本鉴权Use Invoke-WebRequest with a username and password for basic authentication on the GitHub API的灵感

EN

回答 1

Stack Overflow用户

发布于 2018-09-19 18:02:30

找到问题了,终于..。在我们的例子中,我们需要在3个步骤中修复连接:

TLS忽略certicate-error (抱歉,我们还没有实现一个整洁的certificate)

  • Use基本身份验证,而不是“普通”的powershell credentials

  • Force
  1. 版本。

从那里很容易检索到信息。

代码语言:javascript
复制
function Disable-SslVerification
{
    if (-not ([System.Management.Automation.PSTypeName]"TrustEverything").Type)
    {
        Add-Type -TypeDefinition  @"
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
public static class TrustEverything
{
    private static bool ValidationCallback(object sender, X509Certificate certificate, X509Chain chain,
        SslPolicyErrors sslPolicyErrors) { return true; }
    public static void SetCallback() { System.Net.ServicePointManager.ServerCertificateValidationCallback = ValidationCallback; }
    public static void UnsetCallback() { System.Net.ServicePointManager.ServerCertificateValidationCallback = null; }
}
"@
    }
    [TrustEverything]::SetCallback()
}
function Enable-SslVerification
{
    if (([System.Management.Automation.PSTypeName]"TrustEverything").Type)
    {
        [TrustEverything]::UnsetCallback()
    }
}

$domain = "my-cluster.westeurope.cloudapp.azure.com/ambari"
$usernm = "myusr"
$userpwd = "mypwd"

Disable-SslVerification
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$uri = "https://{0}/api/v1/clusters" -f $domain
Write-Output $uri

$credstring = "{0}:{1}" -f $usernm, $userpwd
$credbytes = [System.Text.Encoding]::ASCII.GetBytes($credstring)
$credbase64 = [System.Convert]::ToBase64String($credbytes)
$credAuthValue = "Basic {0}" -f $credbase64
$headers = @{ Authorization = $credAuthValue}

$result = "-"
$result = Invoke-RestMethod -Method Get -UseBasicParsing -Uri $uri -Headers $headers
$result
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52313950

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档