使用 Junos PyEZ 管理 Junos 设备上的救援配置
Junos PyEZ jnpr.junos.utils.config.Config 实用程序使您能够管理 Junos 设备上的救援配置。救援配置允许您定义已知工作配置或具有随时可恢复的已知状态的配置。如果您的路由器或交换机配置和备份配置文件在修复后损坏,则需要恢复为已知配置或作为最后手段时,您可使用救援配置。
如何管理救援配置
该 jnpr.junos.utils.config.Config 实用程序使您能够在 Junos 设备上保存、检索、加载和删除救援配置。创建该类实 Config 例后,您可使用此 rescue() 方法来管理救援配置。通过为所需操作设置 rescue() 方法 action 参数,指定在救援配置上执行的操作。有效的操作值包括 "save"、 "get"、 "reload"和 "delete".以下示例说明了每个 rescue() 方法操作的方法调用。
保存救援配置
创建救援配置时,设备将最近提交的配置保存为援救配置。要将活动配置保存为救援配置,请在方法论证列表中rescue()指定action="save"。此操作覆盖任何现有救援配置。例如:
from jnpr.junos import Device
from jnpr.junos.utils.config import Config
with Device(host='dc1a.example.com') as dev:
cu = Config(dev)
cu.rescue(action='save')
检索救援配置
要检索现有救援配置,请指定 action="get"和选择性地指定格式为 "json"或 "text" "xml"。如果不指定格式,默认格式为文本。如果设备没有现有的救援配置,该 rescue() 方法将返回 None。
以下示例检索并打印救援配置(如果存在)。
from jnpr.junos import Device
from jnpr.junos.utils.config import Config
from lxml import etree
with Device(host='dc1a.example.com') as dev:
cu = Config(dev)
rescue = cu.rescue(action='get', format='xml')
if rescue is None:
print ('No existing rescue configuration.')
else:
print (etree.tostring(rescue, encoding='unicode'))
负载并提交救援配置
要将现有救援配置加载到候选配置中,请指定 action="reload"。如果没有救援配置,负载操作将返回 False。加载救援配置后,您必须提交配置才能使其成为设备上的活动配置。
以下示例会尝试加载救援配置,如果存在,则提交该配置使其成为活动配置。
from jnpr.junos import Device
from jnpr.junos.utils.config import Config
with Device(host='dc1a.example.com') as dev:
with Config(dev, mode='exclusive') as cu:
rescue = cu.rescue(action='reload')
if rescue is False:
print ('No existing rescue configuration.')
else:
cu.pdiff()
cu.commit()
删除救援配置
要删除现有救援配置,请指定 action="delete"。
from jnpr.junos import Device
from jnpr.junos.utils.config import Config
with Device(host='dc1a.example.com') as dev:
cu = Config(dev)
cu.rescue(action='delete')
示例:使用 Junos PyEZ 保存救援配置
此示例使用 Junos PyEZ jnpr.junos.utils.config.Config 实用程序在 Junos 设备上保存救援配置(如果不存在)。
要求
此示例使用以下硬件和软件组件:
运行 Python 3.5 或更高版本以及 Junos PyEZ 版本 2.0 或更高版本的配置管理服务器
启用 NETCONF 的 Junos 设备,以及经适当权限配置的用户帐户
为服务器和 Junos 设备上的相应用户配置的 SSH 公钥/私有密钥对
概述
此示例展示了一个 Python 应用程序,该应用程序使用 Junos PyEZ Config 实用程序在指定设备上保存救援配置。救援配置允许您定义已知工作配置或具有随时可恢复的已知状态的配置。创建救援配置时,设备将最近提交的配置保存为援救配置。
Python 应用程序导入 Device 该类,该类负责处理与 Junos 设备的连接; Config 该类用于在目标设备上执行救援配置操作;并且该模块要求出现例外 jnpr.junos.exception 情况,其中包含管理 Junos 设备时遇到的例外情况。为目标设备创建 Device 实例后, open() 该方法会与设备建立连接和 NETCONF 会话。
应用程序首先确定目标设备上是否存在现有救援配置。如果存在救援配置,则打印为标准输出。如果没有现有救援配置,应用程序会指示设备创建一个。该 rescue() 方法 action 参数设置为 "get" 检索现有救援配置,并在 "save" 不存在时创建救援配置。
执行救援配置操作后,应用程序会调用 close() 终止 NETCONF 会话和连接的方法。该应用程序包括用于处理例外情况的代码,例如 ConnectError 连接到设备时发生的错误。该应用程序还包括处理可能发生的任何其他例外情况的代码。
配置
创建 Junos PyEZ 应用程序
逐步过程
要创建使用 Junos PyEZ 来保存救援配置的 Python 应用程序,如果 Junos 设备上不存在此配置:
导入任何所需模块、类和对象。
from jnpr.junos import Device from jnpr.junos.utils.config import Config from jnpr.junos.exception import ConnectError
包括任何必需的变量,此示例中包含受管理设备的主机名。
host = 'dc1a.example.com'
main()创建功能定义和功能调用,并将剩余语句放入定义中。def main(): if __name__ == "__main__": main()
创建该类的
Device实例,并提供该特定连接所需的主机名和任何参数。dev = Device(host=host)
打开连接并与设备建立 NETCONF 会话。
# open a connection with the device and start a NETCONF session try: dev.open() except ConnectError as err: print ("Cannot connect to device: {0}".format(err)) return
创建实用程序实
Config例。# Create an instance of Config cu = Config(dev)
打印现有救援配置或在不存在时保存一个。
# Print existing rescue configuration or save one if none exists try: rescue = cu.rescue(action='get', format='text') if rescue is None: print ('No existing rescue configuration.') print ('Saving rescue configuration.') cu.rescue(action='save') else: print ('Rescue configuration found:') print (rescue) except Exception as err: print (err)
结束 NETCONF 会话并与设备关闭连接。
# End the NETCONF session and close the connection dev.close()
结果
在配置管理服务器上,查看已完成的应用程序。如果应用程序未显示预期的代码,请重复此示例中的说明以更正应用程序。
from jnpr.junos import Device
from jnpr.junos.utils.config import Config
from jnpr.junos.exception import ConnectError
host = 'dc1a.example.com'
def main():
dev = Device(host=host)
# open a connection with the device and start a NETCONF session
try:
dev.open()
except ConnectError as err:
print ("Cannot connect to device: {0}".format(err))
return
# Create an instance of Config
cu = Config(dev)
# Print existing rescue configuration or save one if none exists
try:
rescue = cu.rescue(action='get', format='text')
if rescue is None:
print ('No existing rescue configuration.')
print ('Saving rescue configuration.')
cu.rescue(action='save')
else:
print ('Rescue configuration found:')
print (rescue)
except Exception as err:
print (err)
# End the NETCONF session and close the connection
dev.close()
if __name__ == "__main__":
main()
执行 Junos PyEZ 代码
执行应用程序
要执行 Junos PyEZ 代码:
-
在配置管理服务器上执行应用程序。
user@server:~$ python3 junos-pyez-config-rescue-create.py No existing rescue configuration. Saving rescue configuration.
在此示例中,目标设备没有现有救援配置,因此设备可以保存一个。如果第二次执行应用程序,它将输出在初始执行过程中保存的救援配置。