指定标准 XML 协议会话中操作信息请求Junos格式
在 Junos XML 协议会话中,要请求有关运行 Junos OS 的路由、交换或安全平台的信息,客户端应用程序在标记元素中Junos XML 请求标记元素 <rpc>
。在开放操作请求标记中设置可选属性后,客户端应用程序可指定 format
Junos XML 协议服务器返回的输出格式。信息可以 XML 标记格式、JavaScript 对象标记 (JSON) 或格式化的 ASCII 文本返回。基本语法如下所示:
<rpc> <operational-request format="(ascii | json | text | xml)"> <!-- tag elements for options --> </operational-request> </rpc>
XML 格式
默认情况下,Junos XML 协议服务器以 XML 标记格式返回操作信息。如果属性的值设置为"xml",或者如果省略了属性,服务器将返回 format
format
XML 中的响应。以下示例请求 ge-0/3/0 接口的信息。 format
省略属性。
<rpc> <get-interface-information> <brief/> <interface-name>ge-0/3/0</interface-name> </get-interface-information> </rpc>
当在Junos模式命令之后包括 选项时,Junos XML 协议服务器返回了 XML 标记格式的信息,此信息与 CLI 中显示的 | display xml
输出相同。
<rpc-reply xmlns:junos="http://xml.juniper.net/junos/11.4R1/junos"> <interface-information xmlns="http://xml.juniper.net/junos/11.4R1/junos-interface" junos:style="brief"> <physical-interface> <name>ge-0/3/0</name> <admin-status junos:format="Enabled">up</admin-status> <oper-status>down</oper-status> <link-level-type>Ethernet</link-level-type> <mtu>1514</mtu> <source-filtering>disabled</source-filtering> <speed>1000mbps</speed> <bpdu-error>none</bpdu-error> <l2pt-error>none</l2pt-error> <loopback>disabled</loopback> <if-flow-control>enabled</if-flow-control> <if-auto-negotiation>enabled</if-auto-negotiation> <if-remote-fault>online</if-remote-fault> <if-device-flags> <ifdf-present/> <ifdf-running/> <ifdf-down/> </if-device-flags> <if-config-flags> <iff-hardware-down/> <iff-snmp-traps/> <internal-flags>0x4000</internal-flags> </if-config-flags> <if-media-flags> <ifmf-none/> </if-media-flags> </physical-interface> </interface-information> </rpc-reply>
ASCII 格式
要请求 Junos XML 协议服务器返回格式为 ASCII 文本的操作信息,而不是使用 Junos XML 标记元素标记信息,客户端应用程序在开放请求标记中包含 或 format="text"
format="ascii"
属性。客户端应用程序将请求括在一个 <rpc>
标记元素中。
<rpc> <get-interface-information format="(text | ascii)"> <brief/> <interface-name>ge-0/3/0</interface-name> </get-interface-information> </rpc>
当客户端应用程序在请求标记中包含 或 属性时,Junos XML 协议服务器将回复格式化为 ASCII 文本,并括在 format="text"
format="ascii"
标记 <output>
元素中。和 format="text"
format="ascii"
属性产生的输出完全相同。
<rpc-reply xmlns:junos="http://xml.juniper.net/junos/11.4R1/junos"> <output> Physical interface: ge-0/3/0, Enabled, Physical link is Down Link-level type: Ethernet, MTU: 1514, Speed: 1000mbps, Loopback: Disabled, Source filtering: Disabled, Flow control: Enabled, Auto-negotiation: Enabled, Remote fault: Online Device flags : Present Running Down Interface flags: Hardware-Down SNMP-Traps Internal: 0x4000 Link flags : None </output> </rpc-reply>
以下示例显示在命令中执行的等效操作模式CLI:
user@host> show interfaces ge-0/3/0 brief Physical interface: ge-0/3/0, Enabled, Physical link is Down Link-level type: Ethernet, MTU: 1514, Speed: 1000mbps, Loopback: Disabled, Source filtering: Disabled, Flow control: Enabled, Auto-negotiation: Enabled, Remote fault: Online Device flags : Present Running Down Interface flags: Hardware-Down SNMP-Traps Internal: 0x4000 Link flags : None
Junos XML 协议服务器返回的格式化 ASCII 文本与 CLI 输出相同,但输出中包含"<"(不标记)、">"(大于符号)和"&"(ampersand) 等字符的情况除外。Junos XML 协议服务器用分别为"<"、">"和"&" 的对应预定义实体参考替换这些字符。
如果 Junos XML API 未为客户端应用程序请求的输出类型定义响应标记元素,则 Junos XML 协议服务器将回复作为格式化的 ASCII 文本返回至标记元素中,即使请求了 XML 标记的输出也一 <output>
样。
有关标记 <output>
元素的信息,请参阅 Junos XML API 操作开发人员参考。
标记元素内的数据内容和格式会有所更改,因此客户端应用程序 <output>
不得依赖于它们。
JSON 格式
从 14.2 Junos OS版本开始,客户端应用程序可以 JSON 格式请求操作和配置信息。要请求 Junos XML 协议服务器使用 JSON 格式(而非使用 Junos XML 标记元素标记)返回操作信息,客户端应用程序在开放请求标记中包含 format="json"
属性。客户端应用程序将请求括在一个 <rpc>
标记元素中。
<rpc> <get-interface-information format="json"> <brief/> <interface-name>cbp0</interface-name> </get-interface-information> </rpc> ]]>]]>
当客户端应用程序在请求标记中包含属性时,Junos XML 协议服务器会使用 JSON 对回复 format="json"
进行格式化。
<rpc-reply xmlns:junos="http://xml.juniper.net/junos/16.1R1/junos"> { "interface-information" : [ { "attributes" : {"xmlns" : "http://xml.juniper.net/junos/16.1R1/junos-interface", "junos:style" : "brief" }, "physical-interface" : [ { "name" : [ { "data" : "cbp0" } ], "admin-status" : [ { "data" : "up", "attributes" : {"junos:format" : "Enabled"} } ], "oper-status" : [ { "data" : "up" } ], "if-type" : [ { "data" : "Ethernet" } ], "link-level-type" : [ { "data" : "Ethernet" } ], "mtu" : [ { "data" : "1514" } ], "speed" : [ { "data" : "Unspecified" } ], "clocking" : [ { "data" : "Unspecified" } ], "if-device-flags" : [ { "ifdf-present" : [ { "data" : [null] } ], "ifdf-running" : [ { "data" : [null] } ] } ], "ifd-specific-config-flags" : [ { "internal-flags" : [ { "data" : "0x0" } ] } ], "if-config-flags" : [ { "iff-snmp-traps" : [ { "data" : [null] } ] } ] } ] } ] } </rpc-reply>
从 Junos OS 版 17.3R1 开始,运行 Junos OS 支持以紧凑型 JSON 格式发射设备的操作状态,其中仅发出具有多个值的对象作为 JSON 阵列。要配置设备以发出紧凑的 JSON 格式,请配置 层级 compact
[edit system export-format state-data json]
的 语句。
user@host# set system export-format state-data json compact
以下示例执行 命令,然后以非紧凑、紧凑的 show system uptime
JSON 格式显示输出。
user@host> show system uptime | display json
非紧凑的 JSON 格式:
{ "system-uptime-information" : [ { "attributes" : {"xmlns" : "http://xml.juniper.net/junos/18.1R1/junos"}, "current-time" : [ { "date-time" : [ { "data" : "2018-05-15 13:43:46 PDT", "attributes" : {"junos:seconds" : "1526417026"} } ] } ], "time-source" : [ { "data" : " NTP CLOCK " } ], "system-booted-time" : [ { "date-time" : [ { "data" : "2018-05-15 10:57:02 PDT", "attributes" : {"junos:seconds" : "1526407022"} } ], "time-length" : [ { "data" : "02:46:44", "attributes" : {"junos:seconds" : "10004"} } ] } ], "protocols-started-time" : [ { "date-time" : [ { "data" : "2018-05-15 10:59:33 PDT", "attributes" : {"junos:seconds" : "1526407173"} } ], "time-length" : [ { "data" : "02:44:13", "attributes" : {"junos:seconds" : "9853"} } ] } ], "last-configured-time" : [ { "date-time" : [ { "data" : "2018-05-02 17:57:44 PDT", "attributes" : {"junos:seconds" : "1525309064"} } ], "time-length" : [ { "data" : "1w5d 19:46", "attributes" : {"junos:seconds" : "1107962"} } ], "user" : [ { "data" : "admin" } ] } ], "uptime-information" : [ { "date-time" : [ { "data" : "1:43PM", "attributes" : {"junos:seconds" : "1526417026"} } ], "up-time" : [ { "data" : "2:47", "attributes" : {"junos:seconds" : "10034"} } ], "active-user-count" : [ { "data" : "1", "attributes" : {"junos:format" : "1 user"} } ], "load-average-1" : [ { "data" : "0.49" } ], "load-average-5" : [ { "data" : "0.19" } ], "load-average-15" : [ { "data" : "0.10" } ] } ] } ] }
相同命令的紧凑型 JSON 格式是:
{ "system-uptime-information" : { "current-time" : { "date-time" : "2018-05-15 13:49:56 PDT" }, "time-source" : " NTP CLOCK ", "system-booted-time" : { "date-time" : "2018-05-15 10:57:02 PDT", "time-length" : "02:52:54" }, "protocols-started-time" : { "date-time" : "2018-05-15 10:59:33 PDT", "time-length" : "02:50:23" }, "last-configured-time" : { "date-time" : "2018-05-15 13:49:40 PDT", "time-length" : "00:00:16", "user" : "admin" }, "uptime-information" : { "date-time" : "1:49PM", "up-time" : "2:53", "active-user-count" : "1", "load-average-1" : "0.00", "load-average-5" : "0.06", "load-average-15" : "0.06" } } }