Automation

 View Only
last person joined: 6 days ago 

Ask questions and share experiences about Apstra, Paragon, and all things network automation.
  • 1.  Creating a script in Junos Space for clearing storage on EX 2300-C's

    Posted 03-14-2023 10:37

    Hello!

    I have been a network technician with my company working under engineers for the last 2 years supporting a Juniper environment. I know my way around the CLI, but none of us are familiar with writing scripts for use in jspace.

    I would greatly appreciate some pointers on where to get started on learning how to create my own scripts. I'd like to begin with a simple one that runs the <request system storage cleanup> command only.

    I have a few hundred 2300's that I need to run this on for software updates and being able to whip a script up for it would be waaaaaaaaaaaaaaaaaaaaaaaaaay better than ssh'ing into every one and copy pasting the command - I did it the last time we updated and it was monotonous to say the least.

    Thank you very much if you have the time!



    ------------------------------
    TJ MUNGER
    ------------------------------


  • 2.  RE: Creating a script in Junos Space for clearing storage on EX 2300-C's

     
    Posted 02-18-2024 12:38
    Edited by asharp 02-21-2024 17:46

    Something like the following should work.  Not tested it extensively, but was working with Space 23.1R1.4 and a couple of SRX that had been discovered.

    I should point out that this is a local SLAX script, that means that this script does not need to be staged on any devices.  Instead Junos Space itself will launch the script, and internally it will make connections to the target device(s) via the connection, and collect and parse the RPC replies that it receives from each of the devices etc.

    So long as the target devices understand the RPC's that are requested then all is good.

    I like this approach since it avoids having to stage scripts etc., it also means that you can do fancy rendering of the output that is generated because the results window in Junos Space supports HTML.  I'll make another post with an example of a SLAX script that I use to monitor SRX Cluster health, and the output while all generated by a SLAX script, the results itself includes graphs, sortable columns and colors etc.

    You can also do some more things via this approach, for example use SLAX to SSH into some device/server and execute commands, make REST API calls back to Junos Space to go and perform some other task like create a queue to handle long running tasks, or schedule a task to be performed, apply CLI configlets etc. 

    Anyway, back to the point of this reply....

    version 1.0;
    
    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";
    
    /* @ISLOCAL = "true" */
    /* @EXECUTIONTYPE = "GROUPEDEXECUTION" */
    /* @GROUPBYDEVICE="TRUE" */
    /* @CONTEXT = "/device" */
    /* @NAME = "System Storage Cleanup" */
    /* @DESCRIPTION = "Clean up temporary files and rotate logs" */
    /* @CONFIRMATION = "Are you sure that you want to clean up system storage?" */
    
    match / {
        <op-script-results> {
            var $connection = jcs:open();
            var $rpc = <request-system-storage-cleanup>;
            var $results = jcs:execute($connection,$rpc);
            expr jcs:close($connection);
            <output> {
                uexpr "request system storage cleanup:\tList of files to delete:\n";
                uexpr jcs:printf("%20s %20s %40s","Size", "Date", "Name\n");
                for-each ($results/file-list/file) {
                    uexpr jcs:printf("%20s %20s %40s", size, date, file-name _ "\n");
                }
            }
        }
    }
    

    It isn't formatted very well, so it will just dump out the results for each device that it has been executed against.  e.g.

    request system storage cleanup:	List of files to delete:
                    Size                 Date                                    Name
                     135         Feb 18 18:03   /cf/var/log/default-log-messages.0.gz
                     135         Feb 18 18:03   /cf/var/log/interactive-commands.0.gz
                     319         Feb 18 18:03               /cf/var/log/messages.0.gz
                      27         Feb 18 18:00                   /cf/var/log/wtmp.0.gz

    I guess using printf the columns can be arranged more effectively, but for a quick and dirty script this is as good a starting point for you to start learning as any.

    It has been a very very long time since I last wrote any SLAX for Junos Space, so I have to admit I've not used a lot of these annotations in a while and am probably quite rusty.  But hopefully the following will help explain the script in some detail.

    /* @ISLOCAL = "true" */ - This instructs Junos Space to execute this script locally on the Junos Space server using JUICE.
    /* @EXECUTIONTYPE = "GROUPEDEXECUTION" */ - This allows the script to be executed when multiple devices have been selected from the Device Management view.
    /* @GROUPBYDEVICE="TRUE" */ - This allows the script to be executed against each selected device as its own execution.  e.g. if you select 10 devices, then the script will be launched 10 times once for each device at the same time.  Results are shown on a per device basis.
    /* @CONTEXT = "/device" */ - This defines the context under which the script can be executed, for example only when selecting devices from the device management view.
    /* @NAME = "System Storage Cleanup" */ - The name to call this script.
    /* @DESCRIPTION = "Clean up temporary files and rotate logs" */ - A description for the script.
    /* @CONFIRMATION = "Are you sure that you want to clean up system storage?" */ - Display a confirmation dialog asking if the use wants to proceed or not.
    Hope this helps.



    ------------------------------
    Andy Sharp
    ------------------------------