示例:使用操作脚本导出文件
此示例中的 op 脚本 使用 Junos XML 协议 file-put 操作写入远程服务器和本地设备上的文件。
要求
此示例使用运行 Junos OS 的设备。
概述和操作脚本
Junos XML 协议 file-put 操作会创建一个文件,并将指定的内容写入该文件。使用 file-put 命令的基本语法如下:
<rpc> <file-put> <delete-if-exist /> <encoding>value</encoding> <filename>value</filename> <permission>value</permission> <file-contents>file</file-contents> </file-put> </rpc>
以下标记元素与命令一起使用 file-put 。这些标记可以按任意顺序放置,但除 file-contents。标记 file-contents 元素必须是列表中的最后一个标记。
delete-if-exist-(可选)如果包含,则任何现有文件将被覆盖。如果省略标记,则当遇到现有文件时,将返回错误。encoding-(必需)指定使用的编码类型。您可以使用ASCII或base64编码。filename-(必需)此标记中包含要创建的文件的完整或相对路径和文件名。使用相对路径时,指定的路径是相对于用户主目录的。如果指定的目录不存在,系统将返回“目录不存在”错误。permission-(可选)在远程服务器上设置文件的 UNIX 权限。例如,要为用户应用读/写访问权限和对其他人的读取访问权限,应将权限值设置为 0644。有关 UNIX 权限的完整说明,请参阅chmod命令。file-contents—(必需)要导出的 ASCII 或 base64 编码文件内容。这必须是列表中的最后一个标记。
XSLT 语法
以下示例脚本执行 Junos XML API 请求,并将结果导出到远程设备上的文件和本地设备上的文件。脚本采用三个参数:远程设备的 IP 地址或主机名、文件名和文件编码。变量 arguments 在脚本的全局级别进行声明,以便在命令行界面 (CLI) 中显示参数名称和说明。
该脚本会调用本地设备上的 Junos XML API <get-software-information> 请求,并将结果存储在变量中result。该脚本会声明该fileput变量,其中包含该操作的file-put远程过程调用 (RPC)。命令行参数定义和encoding标记元素的值filename。如果缺少必需参数myhost,则脚本会发出错误并停止执行。否则,脚本会提示输入连接到远程设备的用户名和密码。
如果与远程设备的连接成功,则脚本将在连接句柄的上下文中执行 RPC。操作的 file-put 输出是函数的结果 jcs:execute() ,存储在变量中 out 。如果操作遇到错误,脚本将错误打印到 CLI。 file-put 如果操作成功,则标记元素指定 file-contents 的内容将导出到远程设备上的指定文件。然后,与远程主机的连接将关闭。该脚本还会将内容导出到本地设备上的相同文件。
示例脚本包括可选标记元素 permission 和 delete-if-exist 操作 file-put 。通过包括标记 delete-if-exist ,脚本将覆盖远程和本地主机上任何同名的现有文件。在此示例中, permission 标记设置为 0644。
<?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 destination file</description>
</argument>
<argument>
<name>encoding</name>
<description>ascii or base64</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="rpc">
<get-software-information/>
</xsl:variable>
<xsl:variable name="result" select="jcs:invoke($rpc)"/>
<xsl:variable name="fileput">
<file-put>
<filename>
<xsl:value-of select="$filename"/>
</filename>
<encoding>
<xsl:value-of select="$encoding"/>
</encoding>
<permission>0644</permission>
<delete-if-exist/>
<file-contents>
<xsl:value-of select="$result"/>
</file-contents>
</file-put>
</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. Exporting file... </output>
<xsl:variable name="out" select="jcs:execute($connect, $fileput)"/>
<xsl:choose>
<xsl:when test="$out//xnm:error">
<xsl:copy-of select="($out//xnm:error)"/>
</xsl:when>
<xsl:otherwise>
<output>
<xsl:value-of select="$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>
<xsl:variable name="local-out" select="jcs:invoke($fileput)"/>
<output>
<xsl:value-of select="concat('Saving file on local host\n', $local-out)"/>
</output>
</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 destination file";
}
<argument> {
<name> "encoding";
<description> "ascii or base64";
}
}
param $myhost;
param $filename;
param $encoding;
match / {
<op-script-results> {
var $rpc = <get-software-information>;
var $result = jcs:invoke($rpc);
var $fileput = {
<file-put> {
<filename>$filename;
<encoding>$encoding;
<permission>'0644';
<delete-if-exist>;
<file-contents>$result;
}
}
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. Exporting file... \n";
var $out = jcs:execute($connect, $fileput);
if ($out//xnm:error) {
copy-of ($out//xnm:error);
}
else {
<output> $out;
}
expr jcs:close($connect);
}
else {
<output> "No connection to host.";
}
}
var $local-out = jcs:invoke($fileput);
<output> "Saving file on local host\n" _ $local-out;
}
}
配置
程序
逐步过程
要下载、启用和测试脚本:
将 XSLT 或 SLAX 脚本复制到文本文件中,根据需要将文件命名 为 export.xsl 或 export.slax ,并将其复制到设备上的 /var/db/script/op/ 目录中。
在配置模式下,在
file层次结构级别包括语句[edit system scripts op],并根据需要 导出.xsl 或 export.slax 。[edit system scripts op] user@host# set file export.(slax | xsl)
commit and-quit发出命令。[edit] user@host# commit and-quit
通过发出
op export操作模式命令来执行操作脚本,并包括任何必要的参数。
验证
验证操作脚本参数
目的
验证参数名称和说明是否显示在 CLI 中。
行动
发出 op exort ? 操作模式命令。CLI 根据脚本中的全局 arguments 变量中的定义,列出了脚本参数的可能补全。
user@host> op export ? Possible completions: <[Enter]> Execute this command <name> Argument name detail Display detailed output encoding ascii or base64 filename name of destination file myhost IP address or hostname of the remote host | Pipe through a command
验证操作脚本执行
目的
验证脚本是否按预期运行。
行动
发出 op export myhost host encoding encoding filename file 操作模式命令,并在出现提示时包括适当的用户名和密码。如果脚本执行成功,RPC 请求的结果 <get-software-information> 将写入远程设备和本地设备上的文件。例如:
root@host> op export myhost router1 encoding ascii filename /var/log/host-version.txt Enter username: root Enter password: Connected to host. Exporting file... /var/log/host-version.txt Saving file on local host /var/log/host-version.txt
如果未能在命令行参数中提供远程设备的 IP 地址或主机名,则脚本会发出错误并停止执行。
root@host> op export error: missing mandatory argument 'myhost'
如果省略操作的delete-if-existfile-put子标记,并且指定的文件已存在,则脚本报告错误。
root@host> op export myhost router1 encoding ascii filename /var/log/host-version.txt Enter username: root Enter password: Connected to host. Exporting file... Destination file exists Saving file on local host Destination file exists
如果执行脚本并包括远程或本地主机上都不存在的目录路径,则脚本将报告错误。
root@host> op export myhost router1 encoding ascii filename /var/test/host-version.txt Enter username: root Enter password: Connected to host. Exporting file... Destination directory does not exist: /var/test Saving file on local host Destination directory does not exist: /var/test