LoginSignup
2
4

More than 1 year has passed since last update.

Meraki APIを試す

Last updated at Posted at 2021-07-06

Meraki

Cisco社のMerakiシリーズはクラウド管理型のネットワーク機器でダッシュボードからいろいろな情報へアクセスできて、なかなかおもしろいです。APIも用意されているので、試してみます。

APIキーの取得

MerakiのAPIを利用するには、まずダッシュボードからAPIアクセスを有効にします。
1. ダッシュボードのオーガナイゼーション,設定,ダッシュボードAPIアクセスを有効
1. プロファイルからAPIキーを生成する。キーはAPIアクセス時に利用するので、手元に控えておく。

APIを試してみる

Get Organization

オーガナイゼーションの一覧を取得するのはGet Organizationsを利用する。Meraki Developer hubでは,Configurationをクリックすると,パラメータを設定できるので,取得済みのAPIキーを入力して,Runをクリックすれば結果が得られる。

[
    {
        "id": "#########",
        "name": "########",
        "url": "https://#####.meraki.com/#/#######/manage/organization/overview"
    }
]

curlで実行する場合も,TemplateからCurlを選べば

curl -L --request GET \
--url https://api.meraki.com/api/v1/organizations \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header 'X-Cisco-Meraki-API-Key: <API Key>'

と表示されるので,実行すればよい。読みにくければ,jq コマンドで整形する。

Get Organization Networks

オーガナイゼーションが取得できたら,次はオーガナイゼーションに含まれるネットワークを取得してみる。
ネットワーク一覧を取得するのはGet Organization Networksを利用する。
{Organization Id}には,先ほどget organizationsで取得したidを入力する。

curl -L --request GET \
--url https://api.meraki.com/api/v1/organizations/<organization id>/networks \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header 'X-Cisco-Meraki-API-Key: <API key>'
[
    {
        "id": "<network id>",
        "organizationId": "<organization id>",
        "name": "################",
        "productTypes": [
            "appliance",
            "cellularGateway",
            "environmental",
            "switch",
            "wireless"
        ],
        "timeZone": "Japan",
        "tags": [],
        "enrollmentString": null,
        "url": "https://####.meraki.com/-cellular-gatewa/#/########/manage/usage/list",
        "notes": ""
    }
]

が得られる。

Get Network Wireless Client Count History

今までの操作で<organization id>,<network id>を取得することができた。次はこれらのIDを使ってワイヤレスクライアントの数を問合せてみる。
Get Network Wireless Client Count Historyは<network id>をパラメータとして渡すと,ワイヤレスクライアントの数を時系列で返す。

curl -L --request GET \
--url https://api.meraki.com/api/v1/networks/<network id>/wireless/clientCountHistory \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header 'X-Cisco-Meraki-API-Key: <API key>'

デフォルトでは,過去7日間のクライアント数/1日を返す。より細かい粒度でクライアント数が必要な場合は,resolutionパラメータ(300, 600, 1200, 3600, 14400, 86400(default))を渡す。

[
    {
        "startTs": "2021-06-29T00:00:00Z",
        "endTs": "2021-06-30T00:00:00Z",
        "clientCount": 405
    },
    ...
    {
        "startTs": "2021-07-05T00:00:00Z",
        "endTs": "2021-07-06T00:00:00Z",
        "clientCount": 365
    }
]

デバイス一覧の取得

デバイスの一覧はGet Network Devicesで取得できる。

curl -L --request GET \
--url https://api.meraki.com/api/v1/networks/{networkId}/devices \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header 'X-Cisco-Meraki-API-Key: <API key>'

応答は

[
  {
    "lat": xx.xxxxxxxxxxxxx,
    "lng": xxx.xxxxxxxxxxx,
    "address": "",
    "serial": "XXXX-XXXX-XXXXX",
    "mac": "xx:xx:xx:xx:xx:xx",
    "lanIp": "xxx.xxx.xxx.xxx",
    "tags": [
      "xxxxx-xxxx"
    ],
    "url": "https://xxxx.meraki.com/xx",
    "networkId": "L_xxxxxxxxxxxxxxxxxx",
    "name": "xxx-ap",
    "model": "MR56",
    "firmware": "wireless-28-2",
    "floorPlanId": "g_xxxxxxxxxxxxxxxxxx"
  },
  {
    "lat": xx.xxxxxxxxxxxxx,
    "lng": xxx.xxxxxxxxxxxx,
    ..
  }
]

ここからserialだけを抽出する場合は、jq ".[].serial"と組み合わせる

Meraki機器の管理インターフェース情報取得

Get Device Management Interfaceを利用する。

curl -L --request GET \
--url https://api.meraki.com/api/v1/devices/{serial}/managementInterface \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header 'X-Cisco-Meraki-API-Key: <API key>'

応答は

{
  "wan1": {
    "usingStaticIp": true,
    "staticIp": "xxx.xxx.xxx.xxx",
    "staticSubnetMask": "xxx.xxx.xxx.x",
    "staticGatewayIp": "xxx.xxx.xxx.x",
    "staticDns": [
      "xxx.xxx.xxx.xx",
      "xxx.xxx.xxx.yy"
    ],
    "vlan": null
  }
}

管理インターフェースの設定を変更

Update Device Management Interfaceを利用する。
Get Device Management Interfaceを利用して取得した,設定情報から必要な部分を変更して、postする。
今回はDNSのみを変更したかったので、staticIpなどを省いてDNSの情報のみpostしてみたが,エラーになった。。

curl -L --request PUT \
--url https://api.meraki.com/api/v1/devices/{serial}/managementInterface \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header 'X-Cisco-Meraki-API-Key: <API key>' \
--data '{
  "wan1": {
    "usingStaticIp": true,
    "staticIp": "xxx.xxx.xxx.xxx",
    "staticSubnetMask": "xxx.xxx.xxx.x",
    "staticGatewayIp": "xxx.xxx.xxx.x",
    "staticDns": [
      "xxx.xxx.xxx.aa",
      "xxx.xxx.xxx.bb"
    ],
    "vlan": null
  }
}'

これで無事にDNSのIPを変更できた。

2
4
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
2
4