このエントリーはPANOSでのRESTの概要と、REST APIを使ってPaloAltoファイアウォールから情報を読むところまで。
API概要
参考URL: PAN-OS® and Panorama™ 8.0 XML API Usage Guide
ラボ環境を作る
AWSでVM Series PaloAlto FWを立ち上げる。
-
- FWを立ち上げる
- sshでFWにアクセス
- RESTでアクセスできる設定は特に必要なし
APIを使ってみる
コマンドに対応した情報は、CLIから実行したいコマンドを打ってXPATHを確認するのが楽。下の例では”show interface logical”のXPATHは”<operations><show><interface>logical</interface></show></operations>”とわかる
admin@PA-VM> debug cli on admin@PA-VM> show interface logical (leaf-tag: interface value: logical) ((eol-matched: . #t) (context-inserted-at-end-p: . #f)) <request cmd="op" cookie="8212554970679817" uid="500"><operations><show><interface>logical</interface></show></operations></reques t> <response status="success"><result><![CDATA[total configured logical interfaces: 1 name id vsys zone forwarding tag address ------------------- ----- ---- ---------------- ------------------------ ------ ------------------ ethernet1/1 16 1 N/A 0 10.1.1.254/32 ]]></result></response> total configured logical interfaces: 1 name id vsys zone forwarding tag address ------------------- ----- ---- ---------------- ------------------------ ------ ------------------ ethernet1/1 16 1 N/A 0 10.1.1.254/32 admin@PA-VM>
PAN-OSの場合はCiscoやJuniperと異なり、実際のAPIリクエストをする前にキーを取得する必要がある。
import requests import xmltodict import pprint source = 'https://*****.eu-central-1.compute.amazonaws.com/api' def get_key(): url = source + '/?type=keygen&user=api-user&password=******' r = requests.get(url, verify=False) r_dict = xmltodict.parse(r.text) return r_dict['response']['result']['key'] def get_logical_interface(key): # cmd=""を取得したいXPATHに変更する param = '?type=op&cmd=<show><interface>logical</interface></show>' url = source + param + '&key=' + key r = requests.get(url, verify=False) return r.text key = get_key() pprint.pprint(get_logical_interface(key))
こんな感じで情報が返ってくる
('<response status="success"><result>\n' ' <ifnet>\n' ' <entry>\n' ' <name>ethernet1/1</name>\n' ' <zone/>\n' ' <fwd>N/A</fwd>\n' ' <vsys>1</vsys>\n' ' <dyn-addr/>\n' ' <addr6/>\n' ' <tag>0</tag>\n' ' <ip>10.1.1.254/32</ip>\n' ' <id>16</id>\n' ' <addr/>\n' ' </entry>\n' ' </ifnet>\n' '</result></response>')