我的脚本看起来像
import requests
import base64
user="domain\\username"
pass="password"
Authentication_mode="Windows"
tok=Authentication_mode+":"+user+":"+pass #referring the doc for authentication at https://docs.microsoft.com/en-us/rest/api/operationsmanager/authentication/login
token=base64.b64encode(bytes(token,'utf-8')).decode()
headers={'content-type':'application/json','Authorization': 'Basic %s' % token
}
payload={}
url="http://<Servername>/OperationsManager/authenticate"
respone=requests.post(url,headers=headers,data=payload)
print(response)
我得到的响应代码是401,而不是200。我仅供参考,尝试了NTLM auth(给出错误400),HTTPBasicauth,HTTPDigestAuth。
在https://community.squaredup.com/answers/question/scom-1801-rest-api/给出的Powershell脚本,我想用Python做到。
$scomHeaders = New-Object “System.Collections.Generic.Dictionary[[String],[String]]”
$scomHeaders.Add(‘Content-Type’,’application/json; charset=utf-8′)
$bodyraw = “Windows”
$Bytes = [System.Text.Encoding]::UTF8.GetBytes($bodyraw)
$EncodedText =[Convert]::ToBase64String($Bytes)
$jsonbody = $EncodedText | ConvertTo-Json
$uriBase = ‘http://xxxxxx/OperationsManager/authenticate’
$auth = Invoke-RestMethod -Method POST -Uri $uriBase -Headers $scomheaders -body $jsonbody -UseDefaultCredentials -SessionVariable websession
$query = @($query = @( @{ “classid” = “” “displayColumns”= “severity”,“monitoringobjectdisplayname”,“name”,“age”,“repeatcount”,“lastModified” })
$jsonquery = $query | ConvertTo-Json
$Response = Invoke-WebRequest -Uri “http://xxxxxx/OperationsManager/data/alert” -Method Post -Body $jsonquery -ContentType “application/json” -UseDefaultCredentials -WebSession $websession
$alerts = ConvertFrom-Json -InputObject $Response.Content
$alerts.rows | select monitoringobjectdisplayname,name,severity,age