このエントリーはJUNOSでのRESTの概要と、SRXファイアウォールから情報を引きだせるところまで。
API概要
参考URL: REST API Guide
- Retrieve configuration information in XML, ASCII (plain text), or JSON.
- Retrieve operational data in XML, ASCII, or JSON.
ラボ環境を作る
AWSでvSRXを立ち上げる。
-
- vSRXを立ち上げる
- sshでvSRXにアクセス
- RESTでアクセスできる設定
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
root@% root@% cli root> configure Entering configuration mode Users currently editing the configuration: root terminal p1 (pid 9176) on since 2018-03-15 00:24:19 UTC, idle 00:13:19 [edit] [edit] root# run show interfaces terse | match 172 fxp0.0 up up inet 172.31.12.74/20 [edit] root# set system services rest http port 8080 [edit] root# show | compare [edit system] + services { + rest { + http { + port 8080; + } + } + } [edit] root# commit commit complete [edit] root# |
RESTを使ってみる
JUNOSでのXMLデザインはJunos XML API Operational Developer Reference dが参考になる。ただし16,000ページ以上の大長編。JUNOSコマンドに対応した情報は、CLIからメソッドを確認するのが楽。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
root> show interfaces fxp0.0 | display xml rpc <rpc-reply xmlns:junos="http://xml.juniper.net/junos/15.1X49/junos"> <rpc> <get-interface-information> <interface-name>fxp0.0</interface-name> </get-interface-information> </rpc> <cli> <banner></banner> </cli> </rpc-reply> root> |
この例では、http://”vSRXのIPアドレス”:”ポート”/rpc/get-interface-information/interface-name=fxp0.0 にリクエストを送ればいい。
このようなスクリプトを実行すると
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import requests import pprint def get_json(interface): source = 'http://****.eu-central-*.compute.amazonaws.com:8080/rpc' method = '/get-interface-information' param = '/interface-name=' + interface options = '' url = source + method + param + options headers = {'Content-type': 'application/json', 'Accept': 'application/json'} r = requests.get(url, auth=('api-user', '*****'), headers=headers) return r.json() pprint.pprint(get_json('fxp0.0')) |
こんな感じで情報が返ってくる
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
{'interface-information': [{'attributes': {'junos:style': 'normal', 'xmlns': 'http://xml.juniper.net/junos/15.1X49/junos-interface'}, 'logical-interface': [{'address-family': [{'address-family-flags': [{'ifff-is-primary': [{'data': None}], 'ifff-sendbcast-pkt-to-re': [{'data': None}]}], 'address-family-name': [{'data': 'inet'}], 'interface-address': [{'ifa-broadcast': [{'data': '172.31.15.255'}], 'ifa-destination': [{'data': '172.31.0/20'}], 'ifa-flags': [{'ifaf-current-default': [{'data': None}], 'ifaf-current-preferred': [{'data': None}], 'ifaf-current-primary': [{'data': None}]}], 'ifa-local': [{'data': '172.31.12.74'}]}], 'mtu': [{'data': '1500'}]}], 'encapsulation': [{'data': 'ENET2'}], 'filter-information': [{}], 'if-config-flags': [{'iff-snmp-traps': [{'data': None}], 'iff-up': [{'data': None}], 'internal-flags': [{'data': '0x4000000'}]}], 'local-index': [{'data': '4'}], 'name': [{'data': 'fxp0.0'}], 'snmp-index': [{'data': '13'}], 'traffic-statistics': [{'attributes': {'junos:style': 'brief'}, 'input-packets': [{'data': '4062'}], 'output-packets': [{'data': '2683'}]}]}]}]} |