이 페이지 내용
예: 사용자 지정 시스템 로그 메시지 생성
Junos OS 커밋 스크립트는 커밋 작업 중에 사용자 지정 시스템 로그 메시지를 생성하여 구성이 사용자 지정 구성 규칙을 준수하지 않을 때 경고할 수 있습니다. 커밋 프로세스는 시스템 로그 메시지 생성에 영향을 받지 않습니다. 이 예는 디바이스 구성에 특정 문이 포함되지 않은 경우 사용자 지정 시스템 로그 메시지를 생성하는 커밋 스크립트를 생성합니다.
요구 사항
Python 스크립트를 사용하는 경우 Junos OS 릴리스 16.1R3 이상.
개요 및 커밋 스크립트
커밋 스크립트를 사용하여 문이 계층 수준에 포함되지 않을 때 read-write
나타나는 사용자 지정 시스템 로그 메시지를 작성합니다 [edit snmp community community-name authorization]
.
스크립트는 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; } } } }
파이썬 구문
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()
구성
절차
단계별 절차
스크립트를 다운로드하고, 사용하도록 설정하고, 테스트합니다. 커밋 스크립트가 시스템 로그 메시지를 올바르게 생성하는지 테스트하려면 후보 구성에 시스템 로그 메시지를 유도하는 조건이 포함되어 있는지 확인하십시오. 이 예시에서는 문이 계층 수준에 포함되어 [edit snmp community community-name authorization]
있지 않은지 read-write
확인합니다.
이 항목의 예제를 테스트하려면 다음을 수행합니다.
스크립트를 텍스트 파일에 복사하고 파일 이름을 read-write.xsl, read-write.slax 또는 read-write.py 적절하게 지정한 다음 디바이스의 /var/db/scripts/commit/ 디렉터리에 복사합니다.
메모:서명되지 않은 Python 스크립트는 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
메모:Python 3을
language python3
사용하여 Python 스크립트를 실행하도록 문을 구성하거나 Python 2.7을 사용하여 Python 스크립트를 실행하도록 문을 구성합니다language python
. 자세한 내용은 언어를 참조하세요.(선택 사항) 조건을 테스트하기 위해 문이 모든 커뮤니티의 계층 수준에 포함되어
[edit snmp community community-name authorization]
있는 경우read-write
기존 SNMP 커뮤니티에 대한 권한 부여를 일시적으로 삭제합니다.[edit] user@host# delete snmp community community-name authorization read-write
다음 명령을 실행하여 시스템 로깅이 파일에 쓰도록 구성되었는지 확인합니다(일반적으로 사용되는 파일 이름은 messages).
[edit] user@host# show system syslog
시스템 로그 구성에 대한 자세한 내용은 시스템 로그 탐색기를 참조하세요.
commit
명령을 실행하여 구성을 커밋합니다.user@host# commit
확인
스크립트 실행 확인
목적
커밋 스크립트에서 생성된 시스템 로그 메시지를 확인합니다.
시스템 로그 메시지는 Python, SLAX 및 XSLT 스크립트에 대한 커밋 작업 중에 생성되지만 Python 스크립트에 대한 커밋 확인 작업 중에만 생성됩니다. 즉, 또는 commit check | display detail
구성 모드 명령을 사용하여 commit check | display xml
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 community does not have read-write access" 메시지를 생성해야 합니다.
Jun 3 14:34:37 host-name cscript: SNMP community does not have read-write access: [edit snmp community community-name]