Cisco ACI へ Python スクリプトでログインし、認証トークンを取得するサンプルをメモしておきます。 認証用の REST API へアクセスし、取得した認証トークンを以降の処理で使い回す… というのが定型処理です。 今回は Python 3.7.3 を使いました。
#!/usr/bin/env python import json import requests apic = 'https://10.0.0.1' username = 'admin' password = 'password' # create credentials structure name_pwd = {'aaaUser': {'attributes': {'name': username, 'pwd': password}}} json_credentials = json.dumps(name_pwd) # log in to API login_url = apic + '/api/aaaLogin.json' post_response = requests.post(login_url, data=json_credentials, verify=False) # get token from login response structure auth = json.loads(post_response.text) login_attributes = auth['imdata'][0]['aaaLogin']['attributes'] auth_token = login_attributes['token'] # create cookie array from token cookies = {} cookies['APIC-Cookie'] = auth_token print(auth_token)