在 Op Scripts 中声明和使用命令行参数
调用脚本时,Junos OS 操作脚本可接受命令行参数。您可以在操作脚本或配置中的语句中包含声明,以便用户在 CLI 中请求对操作脚本的上下文相关帮助时,可查看可能的参数列表。脚本还必须包含处理这些参数的任何必要声明和代码。以下部分详细介绍了如何定义参数和帮助文本,以及如何在操作脚本中使用参数。
声明 Op 脚本命令行参数
定义在 CLI 中使用环境敏感型帮助时将显示的预期操作脚本参数列表的方法有两种方式:
-
在操作脚本中包括声明
-
在 Junos OS 配置中包含语句
脚本生成 和 配置生成的 参数对操作的影响相同。以下部分介绍如何使用不同方法定义操作脚本参数并在 CLI 中显示:
如何定义操作脚本中的参数
您可以在 Python、SLAX 或 XSLT op 脚本中直接声明操作脚本的预期命令行参数。
要在 Python 操作脚本中声明命令行参数:
- 声明一本名为 的
arguments
全球字典。 - 对于每个参数,定义一个名称值对,用于映射到论点名称和论点帮助文本。
Python 语法
# Define arguments dictionary arguments = {'name1': 'description1', 'name2': 'description2'} if __name__ == '__main__': ...
要在 CLI 中显示参数,Python 脚本必须包括该 if __name__ == '__main__':
语句。
要在 SLAX 或 XSLT 操作脚本中声明命令行参数:
- 声明名为
arguments
的全球变量。 - 对于每个参数,定义一个
<argument>
元素。 - 在每个
<argument>
元素中:<name>
使用参数名称定义元素。- 可选定义
<description>
为该论点提供帮助文本的元素。
XSLT 语法
<xsl:variable name="arguments"> <argument> <name>name</name> <description>descriptive-text</description> </argument> </xsl:variable>
SLAX 语法
var $arguments = { <argument> { <name> "name"; <description> "descriptive-text"; } }
如何定义 Junos OS 配置中的参数
您可以在 Junos OS 配置中声明操作脚本的预期命令行参数,作为直接在操作脚本中声明参数的替代项。
要在配置中声明命令行参数:
- 导航到
arguments
给定脚本的[edit system scripts op file filename]
层次结构级别上的语句。 - 配置参数名称。
- 可选配置
description
语句以提供该论点的帮助文本。
例如:
[edit system scripts op op file file filename] arguments { argument-name { description descriptive-text; } }
如何在上下文敏感型帮助中显示参数
在操作脚本或配置中声明参数后,您可以使用 CLI 的上下文敏感帮助列出操作脚本参数。如果包含可选的论点说明,CLI 将显示带有参数名称的帮助文本。
user@host> op filename ? Possible completions: argument-name description argument-name description
您还可以创建操作脚本的隐藏参数,方法是不在操作脚本或配置中包含参数声明。您通常会在脚本中使用该参数,但是当您为该操作脚本请求环境敏感的帮助时,CLI 不会显示论点或帮助文本。
如果您在 Junos OS 配置中配置命令行参数,并在操作脚本中直接声明参数,则仍然可用在脚本中声明的参数,但是在发出 op filename ?
命令时,CLI 不会在其中Possible completions
列出这些参数。这是因为管理 (mgd) 进程首先检查配置以进行参数,从而填充列表。只有在配置中未找到参数时,mgd 进程才会检查脚本进行参数处理。因此,如果在配置中声明参数,则脚本中声明的任何参数将隐藏在 CLI 中。
有关为操作脚本配置帮助文本的详细信息,请参阅 配置“帮助文本 for Op Scripts”。
在 Op Scripts 中使用命令行参数
您可使用 op filename
命令执行本地操作脚本。要将命令行参数传递至脚本,请在执行脚本时包括每个参数名称和值。
user@host> op filename argument-name argument-value
如果指定脚本无法识别的参数,则脚本会忽略该参数。
以下部分讨论如何使用传递到 Python、SLAX 和 XSLT 操作脚本的命令行参数:
如何在 Python Op Scripts 中使用参数
Python 操作脚本可使用标准命令行解析库来处理和使用命令行参数。例如,您可以使用 Python argparse
库轻松定义所需和可选参数,指定默认值,并处理脚本中的参数。
为了让用户更轻松地使用标准 Python 库解析命令行参数,我们修改了将参数传递到 Python 操作脚本的方式。从 Junos OS 版本 21.2R1 和 Junos OS Evolved 版本 21.2R1 开始,当设备将命令行参数传递到 Python op 脚本时,它将单个连字符 (-) 前缀为单个字符的论证名称,并将两个连字符 (--) 前缀为多字符论证名称。在早期版本中,设备会将单个连字符 (-) 前缀至所有参数名称。您应确保操作脚本正确处理特定版本的参数。
以下示例使用模块 argparse
处理脚本参数。这些示例定义了全局 arguments
字典,词典密钥用于定义解析器的预期参数。我们提供了两个示例脚本,用于适当处理指定版本中的参数。
Python 语法(Junos OS 版本 21.2R1 或更高版本)
# Junos OS Release 21.2R1 and later import argparse arguments = {'arg1': 'description1', 'arg2': 'description2', 's': 'short option'} def main(): parser = argparse.ArgumentParser(description='This is a demo script.') # Define the arguments accepted by parser # which use the key names defined in the arguments dictionary for key in arguments: if len(key) == 1: parser.add_argument(('-' + key), required=True, help=arguments[key]) else: parser.add_argument(('--' + key), required=True, help=arguments[key]) args = parser.parse_args() # Extract the value print (args.arg1) print (args.arg2) print (args.s) if __name__ == '__main__': main()
Python 语法(Junos OS 版本 21.1 和更低版本)
# Junos OS Release 21.1 and earlier import argparse arguments = {'arg1': 'description1', 'arg2': 'description2', 's': 'short option'} def main(): parser = argparse.ArgumentParser(description='This is a demo script.') # Define the arguments accepted by parser # which use the key names defined in the arguments dictionary for key in arguments: parser.add_argument(('-' + key), required=True, help=arguments[key]) args = parser.parse_args() # Extract the value print (args.arg1) print (args.arg2) print (args.s) if __name__ == '__main__': main()
如何在 SLAX 和 XSLT 操作脚本中使用参数
要在 SLAX 或 XSLT 操作脚本中使用命令行参数,必须:
- 为每个参数包含参数声明
- 确保参数名称与脚本中的
arguments
可变声明或arguments
Junos OS 配置中的语句中定义的名称相同。
XSLT 语法
<xsl:param name="name"/>
SLAX 语法
param $name;
op 脚本将每个脚本参数的值分配给相应的参数,然后可在整个脚本中参考。
示例:在 XSLT 操作脚本中声明参数
声明指定和 interface
protocol
。执行脚本,将 ge-0/2/0.0 接口和 inet
协议指定为参数的值。
以下示例说明如何在 XSLT 脚本或配置中声明参数:
在 Op Script 中声明参数 (脚本1)
<xsl:variable name="arguments"> <argument> <name>interface</name> <description>Name of interface to display</description> </argument> <argument> <name>protocol</name> <description>Protocol to display (inet, inet6)</description> </argument> </xsl:variable>
在配置中声明参数
[edit system scripts op] file script1 { arguments { interface { description "Name of interface to display"; } protocol { description "Protocol to display (inet, inet6)"; } } }
除了在脚本或配置中声明参数之外,您还必须声明脚本中的相应参数,以便参考脚本参数并访问其值。
声明参数
<xsl:param name="interface"/> <xsl:param name="protocol"/>
在执行脚本时提供参数名称和值。例如:
执行脚本
user@host> op script1 interface ge-0/2/0.0 protocol inet
示例:在 Python Op Scripts 中声明和使用参数
声明在 Python 操作脚本中命名 interface
和 p
命名的两个参数。执行脚本,将 ge-0/2/0.0 接口和 inet
协议指定为参数的值。根据您的版本选择适当的论证处理语句。脚本使用与 Junos OS 版本 21.2R1 和更高版本兼容的语句,并注释了用于处理旧版本中的参数的语句。
在 Op Script 中声明参数(script1.py)
from jnpr.junos import Device import argparse # Define arguments dictionary arguments = {'interface': 'Name of interface to display', 'p': 'Protocol to display (inet, inet6)'} def main(): parser = argparse.ArgumentParser() # Argument handling for Junos OS Release 21.2R1 or later for key in arguments: if len(key) == 1: parser.add_argument(('-' + key), required=True, help=arguments[key]) else: parser.add_argument(('--' + key), required=True, help=arguments[key]) # Argument handling for Junos OS Release 21.1 and earlier #for key in arguments: # parser.add_argument(('-' + key), required=True, help=arguments[key]) args = parser.parse_args() try: with Device() as dev: res = dev.rpc.get_interface_information( interface_name=args.interface, terse=True, normalize=True) print (args.interface + " status: " + res.findtext("logical-interface/oper-status")) for elem in res.xpath("//address-family \ [normalize-space(address-family-name)=$protocol]", protocol=args.p): if (elem.find("interface-address/ifa-local") is not None): print ("inet address: " + elem.find("interface-address/ifa-local").text) except Exception as err: print (err) if __name__ == '__main__': main()
或者,您不能将字典包含 arguments
在 Python 操作脚本中,而是可以将参数与 SLAX 和 XSLT 脚本完全一样包含在配置中。
要在 CLI 的上下文敏感型帮助中查看操作脚本参数,请发出 op filename ?
命令。
显示参数
user@host> op script1.py ? Possible completions: <[Enter]> Execute this command <name> Argument name detail Display detailed output interface Name of interface to display invoke-debugger Invoke script in debugger mode p Protocol to display (inet, inet6) | Pipe through a command
在执行脚本时提供参数名称和值。例如:
执行脚本
user@host> op script1.py interface ge-0/2/0.0 p inet ge-0/2/0.0 status: up inet address 198.51.100.1/24