0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Shell Scripting APIC API

Last updated at Posted at 2024-11-28

APIC APIに接続してTenantを表示するシェルスクリプトを作る。

方針

必要な情報を取得してプログラムを書く。
・CiscoのAPI sandboxにアクセスし、APIを起動しUSER、PASSを取得
・環境変数を設定
・シェルスクリプトを書く

CiscoのAPI sandboxにアクセスし、APIを起動しUSER、PASSを取得

以下のサイトにアクセスしてログインする。
https://devnetsandbox.cisco.com/

ACI Simulator Always-OnのLaunchをクリック
Screenshot_2024-11-28_19-40-26.png

時間を適当に設定して、Launch Environmentをクリックすると
Sandboxが設定した時間だけ起動される。
Screenshot_2024-11-28_19-41-53.png

鎖マークをクリックすると
User:admin
Pass:!v3G@!4@Y
が取得できた。
Screenshot_2024-11-28_19-43-04.png

環境変数を設定

環境変数のためのファイル

.envrc
export APIC_URL=https://sandboxapicdc.cisco.com
export APIC_USERNAME="admin"
export APIC_PASSWORD="!v3G@!4@Y"

シェルスクリプトを書く

apic_tenants.sh
if [[ -z "${APIC_URL}" ]] || [[ -z "${APIC_USERNAME}" ]] || [[ -z "${APIC_PASSWORD}" ]]; then
    echo "What is the URL for APIC?"
    read APIC_URL
    echo "What is the username for APIC?"
    read APIC_USERNAME
    echo "What is the password for APIC?"
    read -s APIC_PASSWORD
else
    echo "Environment Variables found for APIC..."
fi

LOGIN_PAYLOAD='{
    "aaaUser": {
        "attributes": {
            "name": "'${APIC_USERNAME}'",
            "pwd": "'${APIC_PASSWORD}'"
        }
    }
}'

#User PassからTOKENを取得。
APIC_TOKEN=$(\
curl -k --silent -X POST \
${APIC_URL}/api/aaaLogin.json \
--data-raw "${LOGIN_PAYLOAD}" \
| jq --raw-output '.imdata[0].aaaLogin.attributes.token' \
)

#Tokenでアクセスしてデータを取得
APIC_RESPONSE=$(curl -k --silent \
    --header "Cookie: APIC-cookie=${APIC_TOKEN}" \
    ${APIC_URL}'/api/node/class/fvTenant.json')

echo "List of Tenants:"
echo $APIC_RESPONSE | jq -r '.imdata[].fvTenant.attributes.name'

実行

Tenant一覧が表示された。

$sudo apt install jq
$source .envrc
$chmod +x apic_tenants.sh
$./apic_tenants.sh
Environment Variables found for APIC...
List of Tenants:
infra
common
mgmt
Kelompok1Tenant_Universitas-Scientiarum

まとめと注意
・Curlのエラーの詳細がわからなくなったら-s/--silentオプションをとる。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?