Blog Viewer

Scripting How-To: Run a command on multiple remote targets

By Erdem posted 08-14-2015 15:31

  

Overview

Run a command on multiple remote targets. This applies to SLAX version 1.0 and higher.

Description

 

This script runs a command on a set of remote hosts. The output from all targets is displayed. Note that this uses the "jcs:execute()" function, so the user must set up ssh-agent and keys as needed to allow unprompted login.

Source Code

GitHub Links

The source code below is also available from GitHub at the following locations:

 

Example Configuration

1 system {
2     scripts {
3         op {
4             file run-on-all.slax {
5                 description "Run a command on multiple routers";
6             }
7         }
8     }
9 }

Example Output

 
001 user@cli> op run-on-all target "iad ord mia" command "show version"    

002 [processing target iad] 

003    [connecting] 

004    [executing] 

005    [closing] 

006 [processing target ord] 

007    [connecting] 

008    [executing] 

009    [closing] 

010 [processing target mia] 

011    [connecting] 

012    [executing] 

013    [closing] 

014 [begin iad] 

015 Hostname: iad 

016 Model: m7i 

017 JUNOS Base OS boot [9.5I20090330_0030_danai-20] 

018 JUNOS Base OS Software Suite [9.5I20090330_0030_danai-20] 

019 JUNOS Kernel Software Suite [9.5I20090330_0030_danai-20] 

020 JUNOS Crypto Software Suite [9.5I20090330_0030_danai-20] 

021 JUNOS Packet Forwarding Engine Support (M/T Common) [9.5I20090330_0030_danai-20] 

022 JUNOS Packet Forwarding Engine Support (M7i/M10i) [9.5I20090330_0030_danai-20] 

023 JUNOS Online Documentation [9.5I20090330_0030_danai-20] 

024 JUNOS Voice Services Container package [9.5I20090330_0030_danai-20] 

025 JUNOS Services AACL Container package [9.5I20090330_0030_danai-20] 

026 JUNOS Services LL-PDF Container package [9.5I20090330_0030_danai-20] 

027 JUNOS Services Stateful Firewall [9.5I20090330_0030_danai-20] 

028 JUNOS AppId Services [9.5I20090330_0030_danai-20] 

029 JUNOS IDP Services [9.5I20090330_0030_danai-20] 

030 JUNOS Routing Software Suite [9.5I20090330_0030_danai-20] 

031 [end iad] 

032 [begin ord] 

033 Hostname: ord 

034 Model: m7i 

035 JUNOS Base OS boot [9.5I20090330_0030_danai-20] 

036 JUNOS Base OS Software Suite [9.5I20090330_0030_danai-20] 

037 JUNOS Kernel Software Suite [9.5I20090330_0030_danai-20] 

038 JUNOS Crypto Software Suite [9.5I20090330_0030_danai-20] 

039 JUNOS Packet Forwarding Engine Support (M/T Common) [9.5I20090330_0030_danai-20] 

040 JUNOS Packet Forwarding Engine Support (M7i/M10i) [9.5I20090330_0030_danai-20] 

041 JUNOS Online Documentation [9.5I20090330_0030_danai-20] 

042 JUNOS Voice Services Container package [9.5I20090330_0030_danai-20] 

043 JUNOS Services AACL Container package [9.5I20090330_0030_danai-20] 

044 JUNOS Services LL-PDF Container package [9.5I20090330_0030_danai-20] 

045 JUNOS Services Stateful Firewall [9.5I20090330_0030_danai-20] 

046 JUNOS AppId Services [9.5I20090330_0030_danai-20] 

047 JUNOS IDP Services [9.5I20090330_0030_danai-20] 

048 JUNOS Routing Software Suite [9.5I20090330_0030_danai-20] 

049 [end ord] 

050 [begin mia] 

051 Hostname: mia 

052 Model: m20 

053 JUNOS Base OS boot [9.5I20090330_0030_danai-20] 

054 JUNOS Base OS Software Suite [9.5I20090330_0030_danai-20] 

055 JUNOS Kernel Software Suite [9.5I20090330_0030_danai-20] 

056 JUNOS Crypto Software Suite [9.5I20090330_0030_danai-20] 

057 JUNOS Packet Forwarding Engine Support (M/T Common) [9.5I20090330_0030_danai-20] 

058 JUNOS Packet Forwarding Engine Support (M20/M40) [9.5I20090330_0030_danai-20] 

059 JUNOS Online Documentation [9.5I20090330_0030_danai-20] 

060 JUNOS Voice Services Container package [9.5I20090330_0030_danai-20] 

061 JUNOS Services AACL Container package [9.5I20090330_0030_danai-20] 

062 JUNOS Services LL-PDF Container package [9.5I20090330_0030_danai-20] 

063 JUNOS Services Stateful Firewall [9.5I20090330_0030_danai-20] 

064 JUNOS AppId Services [9.5I20090330_0030_danai-20] 

065 JUNOS IDP Services [9.5I20090330_0030_danai-20] 

066 JUNOS Routing Software Suite [9.5I20090330_0030_danai-20] 

067 [end mia] 

068   

069 user@cli> op run-on-all target "iad ord mia" command "show route 10.5.20.2" 

070 [processing target iad] 

071    [connecting] 

072    [executing] 

073    [closing] 

074 [processing target ord] 

075    [connecting] 

076    [executing] 

077    [closing] 

078 [processing target mia] 

079    [connecting] 

080    [executing] 

081    [closing] 

082 [begin iad] 

083   

084 inet.0: 29 destinations, 47 routes (28 active, 0 holddown, 1 hidden) 

085 + = Active Route, - = Last Active, * = Both 

086   

087 10.5.20.0/24       *[OSPF/10] 2w6d 04:16:26, metric 2 

088                     > to 10.5.16.2 via fe-0/0/2.0 

089 [end iad] 

090 [begin ord] 

091   

092 inet.0: 29 destinations, 40 routes (28 active, 0 holddown, 1 hidden) 

093 + = Active Route, - = Last Active, * = Both 

094   

095 10.5.20.0/24       *[BGP/170] 1w4d 21:20:04, MED 2, localpref 100 

096                       AS path: 68 I 

097                     > to 10.5.19.1 via fe-0/0/0.0 

098                     [BGP/170] 1w4d 21:20:08, MED 2, localpref 100 

099                       AS path: 68 I 

100                     > to 10.5.18.1 via fe-0/0/1.0 

101 [end ord] 

102 [begin mia] 

103   

104 inet.0: 25 destinations, 25 routes (24 active, 0 holddown, 1 hidden) 

105 + = Active Route, - = Last Active, * = Both 

106   

107 10.5.20.2/32       *[Local/0] 10w0d 00:45:49 

108                       Local via fe-2/0/0.0 

109 [end mia]           

110   

111 user@cli> 

SLAX Script Contents
01 version 1.0; 

02   

03 ns junos = "http://xml.juniper.net/junos/*/junos"; 

04 ns xnm = "http://xml.juniper.net/xnm/1.1/xnm"; 

05 ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0"; 

06 ns ext = "http://xmlsoft.org/XSLT/namespace"; 

07 ns exsl = "http://exslt.org/math"; 

08   

09 import "../import/junos.xsl"; 

10   

11 /* 

12  * This script runs a command on a set of remote hosts.  The 

13  * output from all targets is displayed. 

14  * 

15  * Note that this uses the "jcs:execute()" function, so the 

16  * user must set up ssh-agent and keys as needed to allow 

17  * unprompted login. 

18  */ 

19   

20 var $arguments = { 

21     <argument> { 

22     <name> "target"; 

23     <description> "Target of this command"; 

24     } 

25     <argument> { 

26     <name> "command"; 

27     <description> "Command to execute"; 

28     } 

29 } 

30   

31 param $target; 

32 param $command; 

33 param $debug = 0;       /* Debug level */ 

34   

35 match / { 

36     <op-script-results> { 

37     <out> { 

38         if (not($target)) { 

39         expr jcs:output("missing argument: target"); 

40         } else if (not($command)) { 

41         expr jcs:output("missing argument: command"); 

42         } 

43         call main; 

44     } 

45     } 

46 } 

47   

48 template main 

49 { 

50     var $rpc = { 

51     if (contains($command, " ")) { 

52         <command> $command; 

53     } else { 

54         <xsl:element name=$command>; 

55     } 

56     } 

57     var $targets = jcs:split(" ", $target); 

58   

59     for-each ($targets) { 

60     var $targ = .; 

61   

62     expr jcs:output("[processing target ", $targ, "]"); 

63     expr jcs:output("   [connecting]"); 

64     var $conn = jcs:open(.); 

65     if ($conn) { 

66         expr jcs:output("   [executing]"); 

67         var $result = jcs:execute($conn, $rpc); 

68   

69         copy-of $result; 

70   

71         expr jcs:output("   [closing]"); 

72         expr jcs:close($conn); 

73     } else { 

74         expr jcs:output("   [connection failed]"); 

75     } 

76     } 

77 } 
XML Script Contents
01 <?xml version="1.0"?>
02 <script>
03   <title>run-on-all.slax</title>
04   <author>phil.shafer</author>
05   <synopsis>
06     Run a command on multiple remote targets
07   </synopsis>
08   <coe>op</coe>
09   <type>display</type>
10  
11   <description>
12 This script runs a command on a set of remote hosts.  The
13 output from all targets is displayed.
14  
15 Note that this uses the "jcs:execute()" function, so the
16 user must set up ssh-agent and keys as needed to allow
17 unprompted login.
18  
19   </description>
20  
21 <example>
22   <title>Sample session</title>
23   <config>example-1.conf</config>
24   <output>example-1.output</output>
25 </example>
26  
27   <xhtml:script xmlns:xhtml="http://www.w3.org/1999/xhtml"
28                 src="../../../../../web/leaf.js"
29             type="text/javascript"/>
30 </script>

#opscript
#How-To
#ScriptingHow-To
#Slax

Permalink