Enable an Op Script and Define a Script Alias
Store Op Scripts
Operation (op) scripts are stored on a device’s hard disk in the
/var/db/scripts/op directory or on the
flash drive in the /config/scripts/op
directory. Only users in the super-user login class
can access and edit files in these directories. For information
about setting the storage location for scripts, see Store and Enable Junos Automation Scripts and Store Scripts in Flash Memory.
To prevent the execution of unauthorized Python code on Junos devices,
unsigned Python scripts must meet certain requirements before you
can execute the scripts on a device. Unsigned Python scripts must be
owned by either root or a user in the super-user
login class, and only the file owner can have write permission for
the file. For detailed information about the requirements for
executing Python automation scripts, see Requirements for Executing Python Automation Scripts on Junos Devices.
If the device has dual Routing Engines and you want to enable an
op script to execute on both Routing Engines, you can copy
the script to the /var/db/scripts/op or
/config/scripts/op directory on
both Routing Engines, or you can issue the commit
synchronize scripts command to synchronize
the configuration and copy the scripts to the other Routing
Engine as part of the commit operation.
Enable Op Scripts
You must enable an op script before you can execute it. To enable an op
script, include the
file filename statement
at the [edit system scripts op] hierarchy level,
and specify the filename of the op script. Only users in the
super-user login class can enable op
scripts.
[edit system scripts op] user@host# set file filename
SLAX and Python scripts must include the .slax or .py filename extension, respectively, in both the actual script name and the filename in the configuration. XSLT scripts do not require a filename extension, but we strongly recommend that you append the .xsl extension. In either case, the configured filename must exactly match the filename of the script in the directory. For example, if the XSLT script filename is script1.xsl, then you must include script1.xsl in the configuration. Likewise, if the XSLT script filename is script1, then you must include script1 in the configuration.
Optionally, you can define an alias for an op script. To define the
alias, include the
command
statement at the [edit system scripts op
file filename] hierarchy
level.
[edit system scripts op file filename] user@host# set command filename-alias
By default, you cannot execute unsigned Python scripts on Junos devices.
To enable the execution of unsigned Python automation scripts that
meet the requirements outlined in Requirements for Executing Python Automation Scripts on Junos Devices, you must configure the language python or
language python3 statement at the
[edit system scripts] hierarchy level.
[edit system scripts] user@host# set language (python | python3)
To determine which op scripts are currently enabled on the device,
display the files configured at the [edit system scripts
op] hierarchy level.
user@host> show configuration system scripts op
To ensure that the enabled files are on the device, list the contents of the /var/run/scripts/op/ directory.
user@host> file list /var/run/scripts/op
To execute the script, you can specify either the op script filename or the alias.
user@host> op (filename | filename-alias)
Example: Enabling an Op Script
To enable an op script:
Place the script in the appropriate directory:
/var/db/scripts/op directory on the hard disk
/config/scripts/op directory on the flash drive
If you store scripts on the flash drive, configure the
load-scripts-from-flashstatement.[edit system scripts] user@host# set load-scripts-from-flash
For unsigned Python scripts, ensure that the following requirements are met:
File owner is either root or a user in the
super-userlogin class.Only the file owner has write permission for the file.
The
language pythonorlanguage python3statement is configured at the[edit system scripts]hierarchy level.[edit system scripts] user@host# set language (python | python3)
Enable the script.
[edit system scripts] user@host# set op file filename
For example:
[edit system scripts] user@host# set op file ospf-neighbors.py
(Optional) Configure an alias for the op script.
[edit system scripts] user@host# set op file filename command filename-alias
Commit the configuration.
[edit] user@host# commit