示例:使用操作脚本导入文件
此示例中的 op 脚本 使用 Junos XML 协议 file-get 操作从远程服务器读取文件的内容。
要求
此示例使用运行 Junos OS 的设备。
概述和操作脚本
Junos XML 协议 file-get 操作读取文件的内容。使用 file-get 命令的基本语法如下:
<rpc> <file-get> <filename>value</filename> <encoding>value</encoding> </file-get> </rpc>
以下标记元素与命令一起使用 file-get 。
encoding-(必需)指定使用的编码类型。您可以使用ASCII、base64或raw编码。filename-(必需)此标记中包含要导入的文件的完整或相对路径和文件名。使用相对路径时,如果file-get在本地执行操作,则指定的路径将相对于 /var/tmp/ 目录。如果在连接句柄的上下文中远程执行操作,则路径将相对于用户的主目录。
使用 ASCII 编码时, file-get 操作会将导入文件中的任何控制字符转换为 Unicode 字符“SECTION SIGN”(U+00A7)。
XSLT 语法
以下示例脚本连接到远程设备并读取指定文件的内容。脚本采用三个参数:远程设备的 IP 地址或主机名、文件名和文件编码。变量 arguments 在脚本的全局级别进行声明,以便在命令行界面 (CLI) 中显示参数名称和说明。
该脚本会声明该fileget变量,其中包含该操作的file-get远程过程调用 (RPC)。命令行参数定义和encoding标记元素的值filename。如果缺少必需参数myhost,则脚本会发出错误并停止执行。否则,脚本会提示输入连接到远程设备的用户名和密码。
如果与远程设备的连接成功,则脚本将在连接句柄的上下文中执行 RPC。操作的 file-get 输出是函数的结果 jcs:execute() ,存储在变量中 out 。如果操作遇到错误,脚本将错误打印到 CLI。 file-get 如果操作成功,则文件的内容将存储在变量中 out ,该变量将打印到 CLI。然后,与远程主机的连接将关闭。
<?xml version="1.0" standalone="yes"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:junos="http://xml.juniper.net/junos/*/junos"
xmlns:xnm="http://xml.juniper.net/xnm/1.1/xnm"
xmlns:jcs="http://xml.juniper.net/junos/commit-scripts/1.0" version="1.0">
<xsl:import href="../import/junos.xsl"/>
<xsl:variable name="arguments">
<argument>
<name>myhost</name>
<description>IP address or hostname of the remote host</description>
</argument>
<argument>
<name>filename</name>
<description>name of file</description>
</argument>
<argument>
<name>encoding</name>
<description>ascii, base64, or raw</description>
</argument>
</xsl:variable>
<xsl:param name="myhost"/>
<xsl:param name="filename"/>
<xsl:param name="encoding"/>
<xsl:template match="/">
<op-script-results>
<xsl:variable name="fileget">
<file-get>
<filename>
<xsl:value-of select="$filename"/>
</filename>
<encoding>
<xsl:value-of select="$encoding"/>
</encoding>
</file-get>
</xsl:variable>
<xsl:choose>
<xsl:when test="$myhost = ''">
<xnm:error>
<message>missing mandatory argument 'myhost'</message>
</xnm:error>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="username" select="jcs:get-input('Enter username: ')"/>
<xsl:variable name="pw" select="jcs:get-secret('Enter password: ')"/>
<xsl:variable name="connect" select="jcs:open($myhost, $username, $pw)"/>
<xsl:choose>
<xsl:when test="$connect">
<output>Connected to host. Reading file...
</output>
<xsl:variable name="out" select="jcs:execute($connect, $fileget)"/>
<xsl:choose>
<xsl:when test="$out//xnm:error">
<xsl:copy-of select="$out//xnm:error"/>
</xsl:when>
<xsl:otherwise>
<output>
<xsl:value-of select="concat('File contents: ', $out)"/>
</output>
</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="jcs:close($connect)"/>
</xsl:when>
<xsl:otherwise>
<output>No connection to host.</output>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</op-script-results>
</xsl:template>
</xsl:stylesheet>
SLAX 语法
version 1.0;
ns junos = "http://xml.juniper.net/junos/*/junos";
ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
import "../import/junos.xsl";
var $arguments = {
<argument> {
<name> "myhost";
<description> "IP address or hostname of the remote host";
}
<argument> {
<name> "filename";
<description> "name of file";
}
<argument> {
<name> "encoding";
<description> "ascii, base64, or raw";
}
}
param $myhost;
param $filename;
param $encoding;
match / {
<op-script-results> {
var $fileget = {
<file-get> {
<filename>$filename;
<encoding>$encoding;
}
}
if ($myhost = '') {
<xnm:error> {
<message> "missing mandatory argument 'myhost'";
}
}
else {
var $username = jcs:get-input("Enter username: ");
var $pw = jcs:get-secret("Enter password: ");
var $connect = jcs:open($myhost, $username, $pw);
if ($connect) {
<output> "Connected to host. Reading file... \n";
var $out = jcs:execute($connect, $fileget);
if ($out//xnm:error) {
copy-of $out//xnm:error;
}
else {
<output> "File contents: " _ $out;
}
expr jcs:close($connect);
}
else {
<output> "No connection to host.";
}
}
}
}
配置
程序
逐步过程
要下载、启用和测试脚本:
将 XSLT 或 SLAX 脚本复制到文本文件中,根据需要将文件命名为 import.xsl 或 import.slax ,并将其复制到设备上的 /var/db/script/op/ 目录中。
在配置模式下,在
file层次结构级别包括语句[edit system scripts op],并根据需要 将 import.xsl 或 import.slax 包含在内。[edit system scripts op] user@host# set file import.(slax | xsl)
commit and-quit发出命令以提交配置并返回到操作模式。[edit] user@host# commit and-quit
通过发出
op import操作模式命令来执行操作脚本,并包括任何必要的参数。
验证
验证脚本参数
目的
验证参数名称和说明是否显示在 CLI 中。
行动
发出 op import ? 操作模式命令。CLI 根据脚本中的全局 arguments 变量中的定义,列出了脚本参数的可能补全。
user@host> op import ? Possible completions: <[Enter]> Execute this command <name> Argument name detail Display detailed output encoding ascii, base64, or raw filename name of file myhost IP address or hostname of the remote host | Pipe through a command
验证操作脚本执行
目的
验证脚本是否按预期运行。
行动
发出 op import myhost host encoding encoding filename file 操作模式命令,并在出现提示时包括适当的用户名和密码。如果脚本执行成功,将显示请求文件的内容。例如:
root@host> op import myhost router1 encoding ascii filename /var/db/scripts/op/test.slax Enter username: root Enter password: Connected to host. Reading file... File contents: version 1.0; ns junos = "http://xml.juniper.net/junos/*/junos"; ns xnm = "http://xml.juniper.net/xnm/1.1/xnm"; ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0"; import "../import/junos.xsl"; ...
如果未能在命令行参数中提供远程设备的 IP 地址或主机名,则脚本会发出错误并停止执行。
root@host> op import error: missing mandatory argument 'myhost'
此外,如果指定的路径或文件不存在,则脚本会发出错误。
root@host> op import myhost router1 encoding ascii filename /var/db/scripts/op/test1.slax Enter username: root Enter password: Connected to host. Reading file... File contents: Failed to open file (/var/db/scripts/op/test1.slax): No such file or directory