如何通过VMWare Restful API使用传递身份验证

时间:2019-03-22 12:26:17

标签: python vmware

请问有人知道如何在VMware的Restful API上使用passthru auth吗?

我不想以纯文本形式发送密码。我已使用我要使用的用户身份登录到该框中,因此宁愿通过它,也不愿将凭据文件保存在磁盘上。

import requests
from requests.auth import HTTPBasicAuth
import json
from requests.packages.urllib3.exceptions import InsecureRequestWarning

api_url = 'https://10.53.228.11/rest'
api_user = 'administrator@vsphere.local'
api_pass = 'Testpw123!'

def main():
    requests.packages.urllib3.disable_warnings(InsecureRequestWarning) #Disable SSL warnings
    get_vcenter_health_status()

def get_vcenter_health_status():
    resp = get_api_data('{}/appliance/health/system'.format(api_url))
    j = resp.json()
    print('vCenter Health: {}'.format(j['value']))

def auth_vcenter(username,password):
    print('Authenticating to vCenter, user: {}'.format(username))
    resp = requests.post('{}/com/vmware/cis/session'.format(api_url),auth=(api_user,api_pass),verify=False)
    if resp.status_code != 200:
        print('Error! API responded with: {}'.format(resp.status_code))
        return
    return resp.json()['value']

def get_api_data(req_url):
    sid = auth_vcenter(api_user,api_pass)
    print('Requesting Page: {}'.format(req_url))
    resp = requests.get(req_url,verify=False,headers={'vmware-api-session-id':sid})
    if resp.status_code != 200:
        print('Error! API responded with: {}'.format(resp.status_code))
        return
    return resp

main()

0 个答案:

没有答案