Description

If you want to execute CLI commands in reference to the occurrence of some events, you can use this article to achieve that scenario.

Some sample scenarios can be like scheduled reboots, taking logs at specified time, etc.

Solution

Configuration:

root# show event-options
policy p {
    events [ sshd_login_failed login_failed login_pam_authentication_error ]; < <Message for ssh login failure.<br />     then {
        execute-commands { 
            commands {
                "request system reboot";
            }
        }
    }
}

Messages log when this command will be executed due ssh failure:

Dec 17 12:56:03 1400-2 file[7735]: UI_LOGIN_EVENT: User 'root' login, class 'super-user' [7735], ssh-connection '', client-mode 'junoscript'
Dec 17 12:56:03 1400-2 file[7735]: UI_CMDLINE_READ_LINE: User 'root', command 'rpc command request system reboot '
Dec 17 12:56:03 1400-2 file[7735]: UI_JUNOSCRIPT_CMD: User 'root' used JUNOScript client to run command 'request-reboot' <<Automatic Reboot
Dec 17 12:56:03 1400-2 file[7735]: UI_REBOOT_EVENT: System rebooted by 'root'
Dec 17 12:56:03 1400-2 file[7735]: UI_CHILD_START: Starting child '/sbin/shutdown'</pre>

We can also generate events instead of using system generated events to trigger command execution.

root@1400-1# show event-options
generate-event {
    test time-of-day "15:35:00 +0000";
}
policy p {
     events test;
     then {
        execute-commands {
            commands {
                "show version";
                "show system uptime";
            }
            output-filename logs.txt;
            destination d1;
            output-format text;
         }
      }
}
destinations {
    d1 {
        archive-sites {
            /cf/var/tmp/;
        }
    }
}

This configuration will collect output of above given commands with the file name 1400-1_logs.txt.  This file will be saved in the  /cf/var/tmp/ directory.

Output after execution of this event is as follows:

root@1400-1> show version

Hostname: 1400-1
Model: srx1400
JUNOS Software Release [12.1R4.7]

root@1400-1> show system uptime

Current time: 2013-01-01 15:35:00 UTC
System booted: 2012-12-28 08:02:00 UTC (4d 07:33 ago)
Protocols started: 2013-01-01 12:31:15 UTC (03:03:45 ago)
Last configured: 2013-01-01 15:33:41 UTC (00:01:19 ago) by root
3:35PM up 4 days, 7:33, 3 users, load averages: 0.14, 0.09, 0.03

Messages log related to execution of above event is as follows:

Jan 1 15:35:03 1400-1 mgd[11241]: UI_JUNOSCRIPT_CMD: User 'root' used JUNOScript client to run command 'get-chassis-inventory'
Jan 1 15:35:03 1400-1 mgd[11241]: UI_JUNOSCRIPT_CMD: User 'root' used JUNOScript client to run command 'get-chassis-inventory'
Jan 1 15:35:03 1400-1 mgd[11241]: UI_JUNOSCRIPT_CMD: User 'root' used JUNOScript client to run command 'get-interface-information

For more details, refer to the technical documentation on Configuring an Event Policy to Execute Operational Mode Commands.

Modification History

2020-07-18: Article reviewed for accuracy; Minor non-technical changes needed.

Related Information