-
Notifications
You must be signed in to change notification settings - Fork 4
/
cpe-hostname.slax
168 lines (137 loc) · 5.63 KB
/
cpe-hostname.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* ----------------------------------------------------------------------------
* Author : Arnoud Vermeer (adapted from Suresh Palguna Krishnan)
* Version : 1.0
* Last Modified : 2019-01-30
* Platform : EX2300-C-12T
* Release : last tested on 18.1
* Description :
*
* This file contains a script to set the hostname of the switch to CPE-$SERIAL as part of the ZTP process.
*
* The ZTP process installs the appropriate software version and installs a base configuration file identified int eh DHCP parameters.
* The base configuration file should contain the following configuration
* The switch will download the script from the URL and run the script after the ZTP process is completed
* system { delete: autoinstallation; }
* event-options {
* generate-event { ztp-lic time-interval 180; }
* policy ztp-lic {
* events ztp-lic;
* then {
* execute-commands {
* commands {
* "op url http://192.168.0.1/cpe-hostname.slax";
* }
* }
* }
* }
* }
*
*
* ----------------------------------------------------------------------------
* Copyright (c) 2011-2012 Juniper Networks. All Rights Reserved.
*
* YOU MUST ACCEPT THE TERMS OF THIS DISCLAIMER TO USE THIS SOFTWARE
*
* JUNIPER IS WILLING TO MAKE THE INCLUDED SCRIPTING SOFTWARE AVAILABLE TO YOU
* ONLY UPON THE CONDITION THAT YOU ACCEPT ALL OF THE TERMS CONTAINED IN THIS
* DISCLAIMER. PLEASE READ THE TERMS AND CONDITIONS OF THIS DISCLAIMER
* CAREFULLY.
*
* THE SOFTWARE CONTAINED IN THIS FILE IS PROVIDED "AS IS." JUNIPER MAKES NO
* WARRANTIES OF ANY KIND WHATSOEVER WITH RESPECT TO SOFTWARE. ALL EXPRESS OR
* IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY WARRANTY
* OF NON-INFRINGEMENT OR WARRANTY OF MERCHANTABILITY OR FITNESS FOR A
* PARTICULAR PURPOSE, ARE HEREBY DISCLAIMED AND EXCLUDED TO THE EXTENT
* ALLOWED BY APPLICABLE LAW.
*
* IN NO EVENT WILL JUNIPER BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
* FOR DIRECT, SPECIAL, INDIRECT, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES
* HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY ARISING OUT OF THE
* USE OF OR INABILITY TO USE THE SOFTWARE, EVEN IF JUNIPER HAS BEEN ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGES.
*/
version 1.0;
/* Junos standard namespaces */
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";
/* EXSLT extensions and app specific namespaces */
ns exsl extension = "http://exslt.org/common";
ns func extension = "http://exslt.org/functions";
ns ztp = "http://xml.juniper.net/junos/ztp";
import "/usr/libdata/cscript/import/junos.xsl";
match / {
<op-script-results> {
/* Open a connection to the device */
var $jnx = jcs:open();
/*Get serial number on current device */
var $serial = ztp:serial-number( $jnx );
var $hostname = "CPE-" _ $serial;
/*** Set the hostname and remove the event-options ***/
var $load-config-rpc = <load-configuration action = "replace" format = "xml"> {
<configuration> {
<system> {
<host-name> $hostname;
}
<event-options delete="delete">;
}
}
expr ztp:load-config( $jnx, $load-config-rpc );
expr ztp:save-rescue( $jnx );
expr ztp:end-script( $jnx );
}
}
/* ------------------------------------------------- */
/* this function is used get serial number */
/* ------------------------------------------------- */
<func:function name="ztp:serial-number">
{
param $jnx;
var $cmd = <get-chassis-inventory>;
var $sn = jcs:execute( $jnx, $cmd )//chassis/serial-number;
<func:result select="$sn">;
}
/* --------------------------------------------------------------------- */
/* this function is used save the configuration to the rescue partition */
/* --------------------------------------------------------------------- */
<func:function name="ztp:save-rescue">
{
param $jnx;
var $cmd = <request-save-rescue-configuration>;
var $save-rsp = jcs:execute( $jnx, $cmd );
<func:result select="/null">;
}
/* ----------------------------------------------------------------------------- */
/* this function is used to load/commit changes to the configuration; no locking */
/* ----------------------------------------------------------------------------- */
<func:function name="ztp:load-config">
{
param $jnx;
param $load-cmd;
var $load-rsp = jcs:execute( $jnx, $load-cmd );
if( $load-rsp//self::xnm:error ) {
call syslog-messages( $header = "Unable to load configuration", $messages = $load-rsp//self::xnm:error/message );
<func:result select="$load-rsp//self::xnm:error">;
}
else {
var $commit-rsp = jcs:execute( $jnx, "commit-configuration" );
if( $commit-rsp//self::xnm:error ) {
call syslog-messages( $header = "unable to commit configuration", $messages = $commit-rsp//self::xnm:error/message );
<func:result select="exsl:node-set($commit-rsp//self::xnm:error)">;
}
else {
<func:result select="/null">;
}
}
}
/* ------------------------------------------------- */
/* this function is used to terminate script properly */
/* ------------------------------------------------- */
<func:function name="ztp:end-script">
{
param $jnx;
expr jcs:close( $jnx );
<xsl:message terminate="yes">;
<func:result select="true()">;
}