指定 NETCONF 会话中的操作信息请求的输出格式
在 NETCONF 会话中,为了请求有关 Junos 设备的信息,客户端应用程序会发出 <rpc> 包含 Junos XML 请求标记元素的元素。要要求 NETCONF 服务器以特定格式返回输出,客户端应用程序在打开操作请求标记中包含可选 format 属性。应用程序可以请求以可扩展标记语言 (XML) 标记格式、JavaScript 对象符号 (JSON) 或格式化 ASCII 文本的输出。语法如下:
<rpc>
<operational-request format="(ascii | json | json-minified | text | xml | xml-minified)">
<!-- tag elements for options -->
</operational-request>
</rpc>
表 1 介绍了可用格式。最小化格式可移除计算机处理所需的字符,例如空格、选项卡和新线路。细化格式减少了数据的大小,因此可以降低传输成本以及数据交付和处理时间。
format属性值 |
说明 |
|---|---|
ascii |
格式化 ASCII 文本 |
json |
JavaScript 对象符号 (JSON) |
json-minified |
JSON 格式,删除了不必要的空格、选项卡和新线路 |
text |
格式化 ASCII 文本 |
xml |
Junos XML 标记格式 |
xml-minified |
Junos XML 标记格式,删除了不必要的空格、选项卡和新线路 |
XML 格式
默认情况下,NETCONF 服务器将以 XML 格式返回操作信息。如果属性 format 设置为 xml 或 format 如果属性被省略,服务器将以 XML 返回响应。以下示例请求 ge-0/3/0 接口的信息并省略 format 属性。
<rpc>
<get-interface-information>
<brief/>
<interface-name>ge-0/3/0</interface-name>
</get-interface-information>
</rpc>
]]>]]>
NETCONF 服务器以 XML 格式返回信息,与 CLI 中显示的输出相同,当您将 | display xml 过滤器附加到操作模式命令时。
<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"
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>
]]>]]>
操作命令 RPC 还支持以最小化格式返回 XML 输出,从而省略不必要的空格、选项卡和新线路。要在支持的版本中请求最小化 XML 输出,请将属性包含 format="xml-minified" 在开口请求标记中。例如:
<rpc>
<get-interface-information format="xml-minified">
<brief/>
<interface-name>ge-0/3/0</interface-name>
</get-interface-information>
</rpc>
]]>]]>
NETCONF 服务器以最小化 XML 格式返回信息。
<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:junos="http://xml.juniper.net/junos/21.1R1/junos"> <interface-information xmlns="http://xml.juniper.net/junos/21.1R1/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>]]>]]>
JSON 格式
从 Junos OS 14.2 版开始,您可以显示 JSON 格式的操作和配置数据。要请求 NETCONF 服务器以 JSON 格式返回操作信息,客户端应用程序在打开的操作请求标记中包含 format="json" 属性。
<rpc>
<get-interface-information format="json">
<brief/>
<interface-name>cbp0</interface-name>
</get-interface-information>
</rpc>
]]>]]>
当客户端应用程序在请求标记中包含 format="json" 属性时,NETCONF 服务器会使用 JSON 格式化回复。
<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"
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 格式,请在[edit system export-format state-data json]层次结构级别配置compact语句。
操作命令 RPC 还支持以最小化格式返回 JSON 输出,从而省略不必要的空格、选项卡和新线路。要在支持的版本中请求最小化 JSON 输出,请将属性包含 format="json-minified" 在开口请求标记中。例如:
<rpc>
<get-interface-information format="json-minified">
<brief/>
<interface-name>cbp0</interface-name>
</get-interface-information>
</rpc>
]]>]]>
NETCONF 服务器以最小化 JSON 格式返回信息。
<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:junos="http://xml.juniper.net/junos/21.1R1/junos">
{"interface-information":[{"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":"9192"}],"speed":[{"data":"Unspecified"}],"clocking":[{"data":"Unspecified"}],"if-device-flags":[{"ifdf-present":[{"data":[null]}],"ifdf-running":[{"data":[null]}]}],"ifd-specific-config-flags":[{}],"if-config-flags":[{"iff-snmp-traps":[{"data":[null]}]}]}]}]}</rpc-reply>
]]>]]>
ASCII 格式
要要求 NETCONF 服务器将操作信息作为格式化的 ASCII 文本返回,而不是将其标记为 Junos XML 标记元素,客户端应用程序在开场请求标记中包含 format="text" 或 format="ascii" 属性。
<rpc>
<get-interface-information format="(text | ascii)">
<brief/>
<interface-name>ge-0/3/0</interface-name>
</get-interface-information>
</rpc>
]]>]]>
当客户端应用程序在请求标记中包含 format="text" 或 format="ascii" 属性时,NETCONF 服务器会将回复格式化为 ASCII 文本,并将其括在标记元素中 <output> 。这些 format="text" 和 format="ascii" 属性产生相同的输出。
<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"
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
NETCONF 服务器返回的格式化 ASCII 文本与 CLI 输出相同,但输出包含不允许的字符(如“<”(小于标牌)、“>”(大于标志)和“&”(安瑟桑)的情况除外。NETCONF 服务器以“<”、“>”和“&”等效的预定义实体参考替换这些字符。
如果 Junos XML API 未针对客户端应用程序请求的输出类型定义响应标记元素,则 NETCONF 服务器将返回回复,其格式化 ASCII 文本包含在标记元素中 <output> ,即使请求 XML 标记的输出也是如此。
标记元素中 <output> 的数据内容和格式可能会发生变化,因此客户端应用程序不能依赖于这些内容和格式。