示例:生成自定义系统日志消息
Junos OS 提交脚本可以在提交操作期间生成自定义系统日志消息,以便在配置不符合自定义配置规则时向您发出提醒。提交过程不受生成系统日志消息的影响。此示例创建一个提交脚本,当设备配置中未包含特定语句时,该脚本将生成自定义系统日志消息。
要求
使用 Python 脚本时为 Junos OS 16.1R3 或更高版本。
概述和提交脚本
使用提交脚本,编写一条自定义系统日志消息,当语句未包含在[edit snmp community community-name authorization]层次结构级别时read-write显示该消息。
该脚本以 XSLT、SLAX 和 Python 格式显示。
XSLT 语法
<?xml version="1.0" standalone="yes"?>
<xsl:stylesheet version="1.0"
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">
<xsl:import href="../import/junos.xsl"/>
<xsl:template match="configuration">
<xsl:for-each select="snmp/community">
<xsl:if test="not(authorization) or (authorization != 'read-write')">
<xsl:variable name="community">
<xsl:call-template name="jcs:edit-path"/>
</xsl:variable>
<xsl:variable name="message" select="concat('SNMP community does not have read-write access: ', $community)"/>
<syslog>
<message>
<xsl:value-of select="$message"/>
</message>
</syslog>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
SLAX 语法
version 1.2;
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";
match configuration {
for-each (snmp/community) {
if ( not(authorization) or (authorization != "read-write")) {
var $community = call jcs:edit-path();
var $message = "SNMP community does not have read-write access: " _ $community;
<syslog> {
<message> $message;
}
}
}
}
Python 语法
from junos import Junos_Configuration
import jcs
def main():
root = Junos_Configuration
for element in root.xpath("./snmp/community"):
if element.find("authorization") is None or \
element.find("authorization").text != 'read-write':
jcs.syslog("172", "SNMP community does not have read-write access: "
+ element.find('name').text)
if __name__ == '__main__':
main()
配置
程序
分步过程
下载、启用并测试脚本。要测试提交脚本是否正确生成系统日志消息,请确保候选配置包含引发系统日志消息的条件。对于此示例,请确保 read-write 语句未包含在 [edit snmp community community-name authorization] 层次结构级别。
要测试本主题中的示例,请执行以下操作:
将脚本复制到文本文件中,将文件命名为 read-write.xsl、 read-write.lax 或 read-write.py ,然后将其复制到设备上的 /var/db/scripts/commit/ 目录。
注意:未签名的 Python 脚本必须由 root 用户或 Junos OS
super-user登录类中的用户拥有,并且只有文件所有者才能对文件具有写入权限。在配置模式下,在
[edit system scripts commit]层次结构级别配置file语句和脚本文件名。[edit] user@host# set system scripts commit file read-write.xsl
如果脚本是用 Python 编写的,请启用未签名的 Python 脚本的执行。
[edit] user@host# set system scripts language python
注意:将
language python3语句配置为使用 Python 3 执行 Python 脚本,或将语句配置language python为使用 Python 2.7 执行 Python 脚本。有关详细信息,请参阅 语言。(选答)要测试该条件,如果
read-write语句[edit snmp community community-name authorization]包含在每个社区的层次结构级别,请暂时删除现有 SNMP 社区的授权。[edit] user@host# delete snmp community community-name authorization read-write
发出以下命令以验证系统日志记录是否配置为写入文件(常用的文件名是 消息):
[edit] user@host# show system syslog
有关系统日志配置的信息,请参阅 系统日志资源管理器。
发出
commit命令以提交配置。user@host# commit
验证
验证脚本执行
目的
验证提交脚本生成的系统日志消息。
系统日志消息是在 Python、SLAX 和 XSLT 脚本的提交操作期间生成的,但它们仅在 Python 脚本的提交检查操作期间生成。这意味着您不能使用 commit check | display xml 或 commit check | display detail 配置模式命令验证 SLAX 和 XSLT 脚本的系统日志消息输出。
行动
提交操作完成后,检查系统日志文件。日志文件的默认目录是 /var/log/。发出 show log filename 操作模式命令,查看日志文件。例如,如果将消息记录到 消息 文件中,请发出以下命令:
user@host> show log messages | match cscript
提交脚本生成的系统日志条目的格式如下:
timestamp host-name cscript: message
read-write由于语句未包含在[edit snmp community community-name authorization]层次结构级别,因此提交脚本应在系统日志文件中生成“SNMP 社区没有读写访问权限”消息。
Jun 3 14:34:37 host-name cscript: SNMP community does not have read-write access: [edit snmp community community-name]