[编辑:我正在运行 Python 2.7.3]
我是一名网络工程师,我一直在破解ncclient(网站上的版本很旧,这是我一直在使用的版本),以使其与 Brocade 的 NETCONF 实现一起工作。为了让它与我们的 Brocade 设备一起工作,我必须进行一些调整,但我必须分叉出包装并对源本身进行调整。这对我来说并不“干净”,所以我决定尝试“以正确的方式”做它并覆盖包中存在的一些东西*;具体三件事:
- 一个名为 build() 的“静态方法”,属于 HelloHandler 类,它本身是 SessionListener 的子类
- RPC 类的“._id”属性(最初的实现使用 uuid,而 Brocade 盒子不太喜欢这个,所以在我最初的调整中,我只是把它改成了一个永远不会改变的静态值)。
- 对构建 XML 过滤器属性的 util 函数的小调整
到目前为止,我在文件中有这段代码brcd_ncclient.py
:
#!/usr/bin/env python
# hack on XML element creation and create a subclass to override HelloHandler's
# build() method to format the XML in a way that the brocades actually like
from ncclient.xml_ import *
from ncclient.transport.session import HelloHandler
from ncclient.operations.rpc import RPC, RaiseMode
from ncclient.operations import util
# register brocade namespace and create functions to create proper xml for
# hello/capabilities exchange
BROCADE_1_0 = "http://brocade.com/ns/netconf/config/netiron-config/"
register_namespace('brcd', BROCADE_1_0)
brocade_new_ele = lambda tag, ns, attrs={}, **extra: ET.Element(qualify(tag, ns), attrs, **extra)
brocade_sub_ele = lambda parent, tag, ns, attrs={}, **extra: ET.SubElement(parent, qualify(tag, ns), attrs, **extra)
# subclass RPC to override self._id to change uuid-generated message-id's;
# Brocades seem to not be able to handle the really long id's
class BrcdRPC(RPC):
def __init__(self, session, async=False, timeout=30, raise_mode=RaiseMode.NONE):
self._id = "1"
return super(BrcdRPC, self).self._id
class BrcdHelloHandler(HelloHandler):
def __init__(self):
return super(BrcdHelloHandler, self).__init__()
@staticmethod
def build(capabilities):
hello = brocade_new_ele("hello", None, {'xmlns':"urn:ietf:params:xml:ns:netconf:base:1.0"})
caps = brocade_sub_ele(hello, "capabilities", None)
def fun(uri): brocade_sub_ele(caps, "capability", None).text = uri
map(fun, capabilities)
return to_xml(hello)
#return super(BrcdHelloHandler, self).build() ???
# since there's no classes I'm assuming I can just override the function itself
# in ncclient.operations.util?
def build_filter(spec, capcheck=None):
type = None
if isinstance(spec, tuple):
type, criteria = spec
# brocades want the netconf prefix on subtree filter attribute
rep = new_ele("filter", {'nc:type':type})
if type == "xpath":
rep.attrib["select"] = criteria
elif type == "subtree":
rep.append(to_ele(criteria))
else:
raise OperationError("Invalid filter type")
else:
rep = validated_element(spec, ("filter", qualify("filter")),
attrs=("type",))
# TODO set type var here, check if select attr present in case of xpath..
if type == "xpath" and capcheck is not None:
capcheck(":xpath")
return rep
然后在我的文件中netconftest.py
我有:
#!/usr/bin/env python
from ncclient import manager
from brcd_ncclient import *
manager.logging.basicConfig(filename='ncclient.log', level=manager.logging.DEBUG)
# brocade server capabilities advertising as 1.1 compliant when they're really not
# this will stop ncclient from attempting 1.1 chunked netconf message transactions
manager.CAPABILITIES = ['urn:ietf:params:netconf:capability:writeable-running:1.0', 'urn:ietf:params:netconf:base:1.0']
# BROCADE_1_0 is the namespace defined for netiron configs in brcd_ncclient
# this maps to the 'brcd' prefix used in xml elements, ie subtree filter criteria
with manager.connect(host='hostname_or_ip', username='username', password='password') as m:
# 'get' request with no filter - for brocades just shows 'show version' data
c = m.get()
print c
# 'get-config' request with 'mpls-config' filter - if no filter is
# supplied with 'get-config', brocade returns nothing
netironcfg = brocade_new_ele('netiron-config', BROCADE_1_0)
mplsconfig = brocade_sub_ele(netironcfg, 'mpls-config', BROCADE_1_0)
filterstr = to_xml(netironcfg)
c2 = m.get_config(source='running', filter=('subtree', filterstr))
print c2
# so far it only looks like the supported filters for 'get-config'
# operations are: 'interface-config', 'vlan-config' and 'mpls-config'
每当我运行我的netconftest.py
文件时,我都会收到超时错误,因为在日志文件中ncclient.log
我可以看到我的子类定义(即更改 XML 以进行 hello 交换的那个 - staticmethod build
)被忽略并且 Brocade 框不知道如何解释原始 ncclientHelloHandler.build()
方法正在生成的 XML**。我还可以在生成的日志文件中看到,我尝试覆盖的其他内容也被忽略了,例如消息 ID(静态值为 1)以及 XML 过滤器。
所以,我在这里有点不知所措。我确实从我的研究中找到了这个博客文章/模块,它似乎完全符合我的要求,但我真的希望能够通过手工而不是使用有人已经编写的模块作为不必自己解决这个问题的借口。
*有人可以向我解释这是否是“猴子补丁”并且实际上很糟糕吗?我在研究中看到猴子修补是不可取的,但是这个答案和这个答案让我很困惑。对我来说,我想要覆盖这些位将使我不必维护我自己的 ncclient 的整个分支。
**为了提供更多上下文,ncclient.transport.session.HelloHandler.build()
默认情况下生成的这个 XML,Brocade 框似乎不喜欢:
<?xml version='1.0' encoding='UTF-8'?>
<nc:hello xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
<nc:capabilities>
<nc:capability>urn:ietf:params:netconf:base:1.0</nc:capability>
<nc:capability>urn:ietf:params:netconf:capability:writeable-running:1.0</nc:capability>
</nc:capabilities>
</nc:hello>
我的重写build()
方法的目的是将上面的 XML 转换成这个(Brocade 确实喜欢:
<?xml version="1.0" encoding="UTF-8"?>
<hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<capabilities>
<capability>urn:ietf:params:netconf:base:1.0</capability>
<capability>urn:ietf:params:netconf:capability:writeable-running:1.0</capability>
</capabilities>
</hello>