PYEZ - 展示套装|匹配命令
PYEZ - Display set | Match command
我一直在尝试通过下面的程序 运行 下面的命令,但它们正在获取整个配置文件,而不是匹配命令的结果。 PYEZ不支持匹配功能吗?
显示配置 |展示套装 |匹配 RI-
显示配置 |展示套装 |匹配池-
显示配置 |展示套装 |匹配服务-
我的目的是获取匹配RI命令的set命令行,将set替换为delete并加载到设备上。
请帮忙。
尝试:
log.debug("collecting the router configuration")
now = datetime.datetime.now()
datets = str(now.year) + str(now.month) + str(now.day) + "_" + str(now.hour) + str(now.minute) + str(now.second)
log.debug("timestamp set to " + str(datets))
if (format == "cnf"):
cnf = dev.cli("show configuration", warning=False)
FileName = rtName + "." + datets + ".cnf"
log.debug("The configuration will be stored in filename as %s", FileName)
# saving the configuration into a CNF file
f = open(FileName, 'w+')
f.write(cnf)
f.close
return FileName
elif (format == "set"):
cnf = dev.cli("show configuration | display set | match pool-", warning=False)
FileName = rtName + "." + datets + ".txt"
log.debug("The configuration will be stored in filename as %s", FileName)
# saving the configuration into a Text file
f = open(FileName, 'w+')
f.write(cnf)
f.close
return FileName
else: # defaults to XML
cnf = dev.rpc.get_config()
FileName = rtName + "." + datets + ".xml"
log.warn("The configuration will be stored in filename as %s", FileName)
# saving the configuration into a XML file
f = open(FileName, 'w+')
f.write(etree.tostring(cnf))
f.close
return FileName
except Exception as e:
log.error("could not collect the router configuration via RPC")
log.error(e.message)
return None
# if the execution gets here, the return will be None
return FileName
正如警告所说,dev.cli()
方法仅用于调试目的。它不支持任何 | match
修饰符。这是因为它实际上并不是在 CLI 提示符下执行命令,而是通过 NETCONF 会话发送 <command>
RPC,并且 Junos 的限制是 <command>
RPC 不支持 | match
修饰符。
对于您的情况,我建议您使用 dev.rpc.get_config()
检索配置。您可以指定 filter_xml
参数来检索配置的子集。
http://junos-pyez.readthedocs.io/en/2.1.2/jnpr.junos.html#jnpr.junos.rpcmeta._RpcMetaExec.get_config
例如,要以集合格式检索所有 [edit routing-instances]
配置,您可以这样做:
>>> rsp = dev.rpc.get_config(filter_xml='<routing-instances/>', options={'format':'set'})
>>> print rsp.text
set routing-instances bar instance-type virtual-router
set routing-instances foo instance-type virtual-router
您只能使用 filter_xml
参数指定配置层次结构。您无法匹配特定的文本模式。但是,您始终可以下载配置层次结构,然后使用正常的 Python 字符串方法对响应进行字符串匹配。
--史黛西
我一直在尝试通过下面的程序 运行 下面的命令,但它们正在获取整个配置文件,而不是匹配命令的结果。 PYEZ不支持匹配功能吗?
显示配置 |展示套装 |匹配 RI- 显示配置 |展示套装 |匹配池- 显示配置 |展示套装 |匹配服务-
我的目的是获取匹配RI命令的set命令行,将set替换为delete并加载到设备上。
请帮忙。
尝试: log.debug("collecting the router configuration")
now = datetime.datetime.now()
datets = str(now.year) + str(now.month) + str(now.day) + "_" + str(now.hour) + str(now.minute) + str(now.second)
log.debug("timestamp set to " + str(datets))
if (format == "cnf"):
cnf = dev.cli("show configuration", warning=False)
FileName = rtName + "." + datets + ".cnf"
log.debug("The configuration will be stored in filename as %s", FileName)
# saving the configuration into a CNF file
f = open(FileName, 'w+')
f.write(cnf)
f.close
return FileName
elif (format == "set"):
cnf = dev.cli("show configuration | display set | match pool-", warning=False)
FileName = rtName + "." + datets + ".txt"
log.debug("The configuration will be stored in filename as %s", FileName)
# saving the configuration into a Text file
f = open(FileName, 'w+')
f.write(cnf)
f.close
return FileName
else: # defaults to XML
cnf = dev.rpc.get_config()
FileName = rtName + "." + datets + ".xml"
log.warn("The configuration will be stored in filename as %s", FileName)
# saving the configuration into a XML file
f = open(FileName, 'w+')
f.write(etree.tostring(cnf))
f.close
return FileName
except Exception as e:
log.error("could not collect the router configuration via RPC")
log.error(e.message)
return None
# if the execution gets here, the return will be None
return FileName
正如警告所说,dev.cli()
方法仅用于调试目的。它不支持任何 | match
修饰符。这是因为它实际上并不是在 CLI 提示符下执行命令,而是通过 NETCONF 会话发送 <command>
RPC,并且 Junos 的限制是 <command>
RPC 不支持 | match
修饰符。
对于您的情况,我建议您使用 dev.rpc.get_config()
检索配置。您可以指定 filter_xml
参数来检索配置的子集。
http://junos-pyez.readthedocs.io/en/2.1.2/jnpr.junos.html#jnpr.junos.rpcmeta._RpcMetaExec.get_config
例如,要以集合格式检索所有 [edit routing-instances]
配置,您可以这样做:
>>> rsp = dev.rpc.get_config(filter_xml='<routing-instances/>', options={'format':'set'})
>>> print rsp.text
set routing-instances bar instance-type virtual-router
set routing-instances foo instance-type virtual-router
您只能使用 filter_xml
参数指定配置层次结构。您无法匹配特定的文本模式。但是,您始终可以下载配置层次结构,然后使用正常的 Python 字符串方法对响应进行字符串匹配。
--史黛西