NETCONFに続き、RESTCONFのエントリー
このエントリーはRESTCONFの概要と、ルータから情報を引きだせるところまで。
ビデオで概要を知る
参考モジュール: Network Device APIs
- Learn to CRUD with GET, POST and DELETE using RESTCONF … RESTCONFの使い方概要
ラボ環境を作る
AWSに立てたCSR1000Vでテストを行う
-
- CSR1000Vを立ち上げる
- sshでCSR1000Vにアクセス
- RESTCONFでアクセスできる設定
ip-172-31-2-170# ip-172-31-2-170#sh ver | inc Cisco IOS Cisco IOS XE Software, Version 16.07.01a Cisco IOS Software [Fuji], Virtual XE Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 16.7.1a, RELEASE SOFTWARE (fc4) Cisco IOS-XE software, Copyright (c) 2005-2017 by cisco Systems, Inc. All rights reserved. Certain components of Cisco IOS-XE software are ip-172-31-2-170# ip-172-31-2-170#conf t Enter configuration commands, one per line. End with CNTL/Z. ip-172-31-2-170(config)#restconf ip-172-31-2-170(config)# ip-172-31-2-170(config)#ip http secure-server ip-172-31-2-170(config)#exit ip-172-31-2-170#
RESTCONFを使ってみる
GigabitEthernet1の状態を確認するスクリプト
import requests import pprint def get_json(interface): source = 'https://xxxxxx.compute-x.amazonaws.com/restconf/data/' module = 'ietf-interfaces:' container = 'interfaces' leaf = '/interface=' + interface options = '' url = source + module + container + leaf + options headers = {'Content-type': 'application/yang-data+json', 'Accept': 'application/yang-data+json'} r = requests.get(url, auth=('api-user', 'xxxxxx'), headers=headers, verify=False) return r.json() if __name__ == '__main__': interface = 'GigabitEthernet1' pprint.pprint(get_json(interface))
CSR1000Vに対して実行したところ。インタフェースの情報が取得できた。
$ python3 get_one_interface.py {'ietf-interfaces:interface': {'enabled': True, 'ietf-ip:ipv4': {'address': [{'ip': '172.31.2.170', 'netmask': '255.255.255.0'}]}, 'ietf-ip:ipv6': {}, 'name': 'GigabitEthernet1', 'type': 'iana-if-type:ethernetCsmacd'}} $
GigabitEthernet1の設定を変更するスクリプト
import requests import json import get_one_interface def put_json(interface, data): source = 'https://xxxxxx.compute-x.amazonaws.com/restconf/data/' module = 'ietf-interfaces:' container = 'interfaces' leaf = '/interface=' + interface options = '' url = source + module + container + leaf + options headers = {'Content-type': 'application/yang-data+json', 'Accept': 'application/yang-data+json'} r = requests.put(url, data=data, auth=('api-user', 'xxxxxx'), headers=headers, verify=False) return r.status_code if __name__ == '__main__': # retrieve current config of the interface interface = 'GigabitEthernet1' interface_config = get_one_interface.get_json(interface) # add 'description' into the interface interface_config['ietf-interfaces:interface']['description'] = 'Configured By RESTCONF' print(put_json(interface, json.dumps(interface_config)))
実行した結果がこちら。ステータスコードは204なので成功。
$ python3 put_one_interface.py 204 $
もう一度インターフェース情報を取得すると、descriptionが追加されているのが確認できる
$ python3 get_one_interface.py {'ietf-interfaces:interface': {'description': 'Configured By RESTCONF', 'enabled': True, 'ietf-ip:ipv4': {'address': [{'ip': '172.31.2.170', 'netmask': '255.255.255.0'}]}, 'ietf-ip:ipv6': {}, 'name': 'GigabitEthernet1', 'type': 'iana-if-type:ethernetCsmacd'}} $