Junos Automation (Scripting)
Reply
Trusted Contributor
acooley
Posts: 117
Registered: ‎08-07-2010
0

example op script to execute existing commands and syntax

Hey Everyone!

Bit of a newb to junOS Scripting here, but I have a customer I'm working with 300 EX switches that I need to execute a request system archive slice alternate members-all to have it sync partitions in case of a failure of power or something that corrupts the primary boot partition. I don't know how to execute these kinds of commands just wonder if anyone has a script example I can reference. Would be something I would load on JunOS Space, and execute on all their devices to clean them up instead of 1 by 1.

-Adam
Recognized Expert
Mattia
Posts: 198
Registered: ‎03-17-2010
0

Re: example op script to execute existing commands and syntax

Hi, I've never used Junos Space so I don't know if you can use it; besides that you have a few options for that, some of them have been discussed in this post: http://forums.juniper.net/t5/Junos-Automation-Scripting/Script-to-log-into-numerous-switches-and-mak... Personally I prefer to use the Junos XML management protocol, which let you orchestrate the actions performed by a script from an external server (instead of a network element which may be also involved in the forwarding of traffic).. Furthermore it let you handle possibile exceptions easier, and if you already know Perl you wouldn't need to learn another language :-) It is documented here, in the guide you will find also some example scripts: http://www.juniper.net/support/xml/junoscript/index.html Cheers, mattia
.................................................................................
JNCIP-ENT, JNCIP-SEC, JNCIS-SP
(If this post helped you, please mark it as an "Accepted Solution"; kudos are also appreciated!)


Trusted Contributor
acooley
Posts: 117
Registered: ‎08-07-2010
0

Re: example op script to execute existing commands and syntax

So basically all space does is just loads junoscripts onto units, and invokes workflow to execute them over and over again accorss multiple managed elements. So really all I am trying to do is create a simple script to do a request system snapshot slice alternate via an rpc call, and i upload this script into space.

 

My first attempt at learning this was via a reboot call already invoked in junos, called <request-reboot> Script is below, if you can share what i might be missing from an understanding for how to invoke rpc calls via junosscript that would be great.

 

/*

* ----System Reboot Test----

*

*/

 

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";

 

jcs:invoke('request-reboot');

 

 

-Adam
Recognized Expert
Mattia
Posts: 198
Registered: ‎03-17-2010
0

Re: example op script to execute existing commands and syntax

Hi, you can use the following script in order to test the jcs:invoke function:

 

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";
match / {
    <op-script-results> {
        /* String example */
         var $command = <command> "show system uptime";
        var $results = jcs:invoke( $command );

        expr jcs:syslog( 145, $results  );
        
        
    }
}

 The results variable will contain the output of the operational command invoked, so in order to check that the script executed correctly, you can inspect the logs, typing "> show log messages | match cscript".

If this works, you can replace the "show system uptime" command with the proper one. You can also use the RPC argument  instead of the text (the jcs:invoke function is documented here).

 

 

.................................................................................
JNCIP-ENT, JNCIP-SEC, JNCIS-SP
(If this post helped you, please mark it as an "Accepted Solution"; kudos are also appreciated!)


Trusted Contributor
acooley
Posts: 117
Registered: ‎08-07-2010
0

Re: example op script to execute existing commands and syntax

Thanks Mattia

I just got it figured out! Well almost. i'm trying to invoke this on an EX switch, and my rpc call works, however it needs to do this accross a VC. When i check the RPC xml abstract for request system snapshot media internal all-members, I dont see it invoking anything for all the members, so I may have to rely on the <command> rpc call right?

 

-A

-Adam
Recognized Expert
Mattia
Posts: 198
Registered: ‎03-17-2010
0

Re: example op script to execute existing commands and syntax

Yes, if you can't find the API element corresponding to a given command, you can always use the tag. In order to see if the XML rpc is available for a command, just run the command followed by " | display xml", as explained here: http://forums.juniper.net/t5/Junos-and-Junosphere/XML-tag-format-equivalent-for-show-l2-learning-mac...
.................................................................................
JNCIP-ENT, JNCIP-SEC, JNCIS-SP
(If this post helped you, please mark it as an "Accepted Solution"; kudos are also appreciated!)


Visitor
dannyrose
Posts: 1
Registered: ‎08-07-2012
0

Re: example op script to execute existing commands and syntax

Hi

am also trying to run some basic cli commands on EX4200 amd M7/10/120/320 using scripts. This is so Junos Space can be scheduled to run the script/cli against large numbers of Junipers.

From your previous example, I have changed to 'clear interface statistics all' which is a weekly good housekeeping command we run. 

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";
match / {
<op-script-results> {
/* String example */
var $command = <command> "clear interface statistics all";
var $results = jcs:invoke( $command );

expr jcs:syslog( 145, $results );


}
}

 

however we also need to export the output of 'show interface extensive' for example. We tried your example of 'show system uptime' and the cli returned with no output. Would really appreciate your help. Also can we assume basic scripts like this will be ok against any Junos above 8.5 on MX/M or EX series?

thanks

Copyright© 1999-2013 Juniper Networks, Inc. All rights reserved.