-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbounce.slax
142 lines (121 loc) · 5.62 KB
/
bounce.slax
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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";
/*
bounce interface script for Junos ver0.2
Practice excercise in SLAX by Martin White-Rose (Twitter: @aUsefulcom)
based _heavily_ on script used in Juniper Education IJAUT course
When copied to /var/db/scripts/op/ and added to the op script configuration,
This script will open config, disable the given interface, commit wait 10 (default) secs, then re-open config, enable the interface and commit
V0.2 - Added checking for uncommitted changes.
call this script as:
op bounce interface <interface name> wait <secs>
*/
/* input arguments and set descriptions for help*/
param $interface;
param $wait;
var $arguments = {
<argument> {
<name> "interface";
<description> "Script will disable this interface, wait 10 (default) secs and re-enable";
}
<argument> {
<name> "wait";
<description> "Number of seconds to wait before re-enabling interface (default 10)";
}
}
/* Getting the interface information to make sure it is not already Admin Down */
var $rpc = <get-interface-information> {
<interface-name> $interface;
}
var $int-status = jcs:invoke($rpc);
/* Getting the config with changed markers to confirm if any uncommited candidate exists */
var $config-rpc = <get-configuration changed="changed" database="candidate" format="xml">;
var $config-state = jcs:invoke($config-rpc);
/* Main script */
match / {
<op-script-results> {
/* Test to see if they have set the interface variable and exit if not */
if (jcs:empty($interface)) {
<xnm:error> {
<message> "No Interface specified - please use 'op bounce interface <interface-name> wait <secs>'";
}
} else if ($int-status/physical-interface/admin-status == "down") {
/*Test if the interface is already disabled*/
<xnm:error> {
<message> "Warning - Interface "_ $interface _"is currently Admin Down - Aborting.";
}
} else if ($config-state//*[@junos:changed="changed"]) {
/* Test to see if candidate config contains uncommitted changes */
<xnm:error> {
<message> "Warning - Uncommitted Candidate Configuration changes detected - Aborting.";
}
} else {
/* IF the tests passed - go ahead an bounce the interface */
var $mgd = jcs:open();
if (not($mgd)) {
<xnm:error> {
<message> "Cannot open MGD connection.";
}
}
else {
/*If the connection worked, setup config to disable the given interface*/
var $disable-config = {
<configuration> {
<interfaces> {
<interface> {
<name> $interface;
<disable>;
}
}
}
}
/*end of the config variable */
expr jcs:output("Connected to MGD.");
/* Used the MGD connection to load AND commit the config */
var $first-config := {
call jcs:load-configuration($connection=$mgd, $configuration=$disable-config);
}
/*Check commit worked, print output and then sleep for 10 Seconds or wait time from user */
if ($first-config//xnm:error) {
<xnm:error> {
<message> "Could not load and commit configuration to disable Interface. Aborted.";
}
} else if (jcs:empty($wait)) {
expr jcs:output ("Interface ", $interface, " disabled. Waiting 10 Seconds before re-enabling.");
expr jcs:sleep(10,0);
} else {
expr jcs:output ("Interface ", $interface, " disabled. Waiting ", $wait, " Seconds before re-enabling.");
expr jcs:sleep($wait,0);
}
/* Get the config setup to re-enable the Interface by deleting disable */
var $enable-config = {
<configuration> {
<interfaces> {
<interface> {
<name> $interface;
<disable delete="delete">;
}
}
}
}
expr jcs:output("Re-enabling Interface ", $interface);
var $second-config := {
call jcs:load-configuration($connection=$mgd, $configuration=$enable-config);
}
/* Check the 2nd commit worked, print confirmation and end */
if ($second-config//xnm:error) {
<xnm:error> {
<message> "Could not load and commit configuration to re-enable Interface - STILL DISABLED, script aborted.";
}
} else {
expr jcs:output("Interface ", $interface, " Re-enabled. Script by @aUsefulcom");
}
/*Close down the MGD connection*/
expr jcs:close($mgd);
}
}
}
}