Fetch remote Junos script at startup

I was in need to fetch an operational Junos script from a URL automatically at startup. Junos has such capabilities for a long time and it is based on the idea of downloading the script while an operator configures the script in Junos via CLI:

[edit]
mwiget@pe1# edit system scripts op file say-hello.py

[edit system scripts op file say-hello.py]
mwiget@pe1# set source https://gist.githubusercontent.com/mwiget/5f77416cce02e5e3eaa9bae17fb98289/raw/73af60c8170156b2e951793d954670a057f5fa40/say-hello.py

[edit system scripts op file say-hello.py]
mwiget@pe1# set refresh
refreshing 'say-hello.py' from 'https://gist.githubusercontent.com/mwiget/5f77416cce02e5e3eaa9bae17fb98289/raw/73af60c8170156b2e951793d954670a057f5fa40/say-hello.py'
/var/home/mwiget/...transferring.file.........100% of  617  B 1115 kBps

[edit system scripts op file say-hello.py]
mwiget@pe1# commit and-quit
commit complete
Exiting configuration mode

mwiget@pe1> show configuration system scripts op file say-hello.py
source https://gist.githubusercontent.com/mwiget/5f77416cce02e5e3eaa9bae17fb98289/raw/73af60c8170156b2e951793d954670a057f5fa40/say-hello.py;

mwiget@pe1>

The refresh command isn’t saved in the configuration, instead it executes right away. See Junos TechLibrary at
Example: Configuring and Refreshing from the Master Source for a Script

There is also an operational command in Junos to update a script on request. Using this method makes the need to store the URL in the configuration obsolete, though might still be handy for documentation purpose:

mwiget@pe1> request system scripts refresh-from op file say-hello.py url https://gist.githubusercontent.com/mwiget/5f77416cce02e5e3eaa9bae17fb98289/raw/73af60c8170156b2e951793d954670a057f5fa40/say-hello.py
refreshing 'say-hello.py' from 'https://gist.githubusercontent.com/mwiget/5f77416cce02e5e3eaa9bae17fb98289/raw/73af60c8170156b2e951793d954670a057f5fa40/say-hello.py'
/var/home/mwiget/...transferring.file.........100% of  550  B 4365 kBps

mwiget@pe1>

So, how can we fetch the script automatically at startup? One way is using an event script that triggers the very same command automatically right after the system is up and running:

mwiget@pe1> show configuration event-options
policy fetch-my-scripts {
    events jtask_task_begin;
    then {
        execute-commands {
            commands {
                "request system scripts refresh-from op file say-hello.py url https://gist.githubusercontent.com/mwiget/2a2a1f657871d5db088efc2706776c72/raw/05a6c0b72f7230777829a65f1ae3631a98dd51b5/say-hello.py";
            }
        }
    }
}

I initially tried the syslog event SYSTEM_OPERATIONAL, but that wouldn’t trigger the command, maybe because the event trigger system is not operational at the time the event is sent. While looking thru the system logs, I found jtask_task_begin to fit the bill just nicely:

mwiget@pe1> help syslog jtask_task_begin
Name:          JTASK_TASK_BEGIN
Message:       Commencing routing updates version <process-version>, built <date> by <builder>
Help:          process started
Description:   The process started.
Type:          Event: This message reports an event, not an error
Severity:      notice
Facility:      LOG_DAEMON

Execute remote op script in CLI

There is a cool way to have an op script executed right away from the CLI, but it must be enabled for Python:

mwiget@pe1> op url https://gist.githubusercontent.com/mwiget/5f77416cce02e5e3eaa9bae17fb98289/raw/73af60c8170156b2e951793d954670a057f5fa40/say-hello.py
warning: [edit system scripts op allow-url-for-python] not enabled
error: The remote op script execution not allowed

mwiget@pe1> configure
Entering configuration mode

[edit]
mwiget@pe1# set system scripts op allow-url-for-python

[edit]
mwiget@pe1# commit and-quit
commit complete
Exiting configuration mode

mwiget@pe1> op url https://gist.githubusercontent.com/mwiget/5f77416cce02e5e3eaa9bae17fb98289/raw/73af60c8170156b2e951793d954670a057f5fa40/say-hello.py
Hi! I'm from GitHubGist.

mwiget@pe1>

The script I used in these examples can be found here:

https://gist.github.com/mwiget/5f77416cce02e5e3eaa9bae17fb98289#file-say-hello-py

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: