You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Golang的go-netconf库恢复Juniper接口配置报错求助

问题:Netconf恢复接口配置报语法错误

使用环境

使用github.com/Juniper/go-netconf/netconf库操作Junos设备。

获取接口配置的代码

rpcGetInterfaceConfig = "<get-configuration><configuration><interfaces><interface><name>%s</name></interface></interfaces></configuration></get-configuration>"
command := fmt.Sprintf(rpcGetInterfaceConfig, switchInterface)
reply, err := j.Session.Exec(netconf.RawMethod(command))

获取到的配置内容

<configuration xmlns="http://xml.juniper.net/xnm/1.1/xnm" junos:changed-seconds="1677335789" junos:changed-localtime="2023-03-01 04:43:09 UTC">
    <junos:comment># Placeholder for QFX platform config.</junos:comment>
    <interfaces>
        <interface>
            <name>et-1/0/1</name>
            <native-vlan-id>101</native-vlan-id>
            <mtu>9192</mtu>
            <unit>
                <name>0</name>
                <family>
                    <ethernet-switching>
                        <interface-mode>trunk</interface-mode>
                        <vlan>
                            <members>vlan101</members>
                            <members>vlan102</members>
                            <members>vlan103</members>
                        </vlan>
                    </ethernet-switching>
                </family>
            </unit>``
        </interface>
    </interfaces>
</configuration>

注:获取到的配置中有多余的``符号,建议清理后再使用

恢复配置的代码

rpcApplyConfig = "<load-configuration format=\"xml\"><configuration>%s</configuration></load-configuration>"
command := fmt.Sprintf(rpcApplyConfig, config)
reply, err := j.Session.Exec(netconf.RawMethod(command))

报错信息

<load-configuration-results>
 <rpc-error>
 <error-type>protocol</error-type>
 <error-tag>operation-failed</error-tag>
 <error-severity>error</error-severity>
 <error-message>syntax error, expecting &lt;/configuration&gt;</error-message>
 <error-info>
 <bad-element>configuration</bad-element>
 </error-info>
 </rpc-error>
 <rpc-error>
 <error-type>protocol</error-type>
 <error-tag>operation-failed</error-tag>
 <error-severity>error</error-severity>
 <error-message>syntax error, expecting &lt;/configuration&gt;</error-message>
 <error-info>
 <bad-element>configuration</bad-element>
 </error-info>
 </rpc-error>
 <load-error-count>2</load-error-count>
</load-configuration-results>

问题原因及解决办法

原因

恢复配置的RPC模板中已经包含<configuration>标签,但传入的config变量是从reply.Data获取到的完整配置,本身就包含<configuration>根节点。最终生成的XML会出现嵌套的<configuration>结构,违反Junos Netconf的语法要求,导致报错。

解决办法

有两种可行方案:

方案1:修改RPC模板,去掉内部的<configuration>标签

直接将获取到的完整配置传入模板,不需要额外嵌套:

// 修改模板
rpcApplyConfig = "<load-configuration format=\"xml\">%s</load-configuration>"
command := fmt.Sprintf(rpcApplyConfig, config)
reply, err := j.Session.Exec(netconf.RawMethod(command))

方案2:提取配置中的<interfaces>部分,再传入模板

如果只需要恢复接口配置,可以从reply.Data中提取<interfaces>节点的内容,再填充到模板的<configuration>中:

// 示例:用XML解析提取interfaces部分(需引入encoding/xml包)
type Configuration struct {
    Interfaces xml.Element `xml:"interfaces"`
}
var conf Configuration
err := xml.Unmarshal([]byte(reply.Data), &conf)
if err != nil {
    // 处理解析错误
}
// 序列化interfaces部分
interfacesXml, err := xml.Marshal(conf.Interfaces)
if err != nil {
    // 处理序列化错误
}
// 使用原模板传入interfaces内容
rpcApplyConfig = "<load-configuration format=\"xml\"><configuration>%s</configuration></load-configuration>"
command := fmt.Sprintf(rpcApplyConfig, string(interfacesXml))
reply, err := j.Session.Exec(netconf.RawMethod(command))

另外,务必清理获取到的配置中多余的``符号,避免引入额外语法错误。

内容的提问来源于stack exchange,提问作者Евгений Горохов

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 13:59:56