-
Notifications
You must be signed in to change notification settings - Fork 0
/
dhcp-bindings-to-static.slax
200 lines (172 loc) · 7.13 KB
/
dhcp-bindings-to-static.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* Author: Ken Cope
* Version: 1.2
* Last Modified: 29 Sep 2021
* Platform: EX
* Release: 17.1 and above
* License: Public Domain
*
* Description: This script looks for dynamic DHCP security bindings and creates
* a static binding with the specified group name (default BYPASS) for a specified
* VLAN (default Voice). It will only create a static binding if the state is fully
* bound (BOUND), and the interface is defined as an access type, trunks are ignored.
* If any bindings are found that can be converted to static, a configuration
* is created and commmited.
*
* The script takes the following arguments:
* vlan: VLAN name to be scanned
* group: Name of the DHCP security group for overriding defaults
*
* Caveats:
* - on Junos 18.4R3, static override bindings dont seem to take effect until the DHCP lease
* expires or the switch is restarted
*
* Revisions:
* 1.2 - Add option to restart DHCP service
* 1.1 - Fix access interface scanning, add syslog
* 1.0 - Initial revision
*
*/
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";
param $group = 'BYPASS';
param $vlan = 'Voice';
param $dhcp-restart = 'False';
var $arguments = {
<argument> {
<name> "group";
<description> "Name of dhcp-security group to use";
}
<argument> {
<name> "vlan";
<description> "Name of VLAN to create bindings for";
}
<argument> {
<name> "dhcp-restart";
<description> "Restart DHCP daemon after change? Set this to anything except False to restart";
}
}
match / {
<op-script-results> {
var $usage = "This script adds all of the dynamic DHCP snooping " _
"bindings to a static bypass list " _
"and commits the configuration.";
var $temp = jcs:output($usage);
expr jcs:output( "Querying DHCP Seurity Bindings:" );
var $cmd = <rpc> {
<get-dhcp-security-binding>;
}
var $dhcps = jcs:invoke($cmd);
/* Determine if the ports are access ports from the configuration, ignoring trunk ports */
var $get-config-rpc = <get-configuration database="committed" inherit="inherit"> {
<configuration> {
<interfaces>;
}
}
var $configuration = jcs:invoke( $get-config-rpc );
/* match on any interface that is tagged as an access port, trim any whitespace */
var $access_interfaces = $configuration/interfaces/interface[unit/family/ethernet-switching/interface-mode = 'access']/name[normalize-space()];
/* DEBUG list access interfaces */
/*
for-each ($access_interfaces) {
expr jcs:output(concat("ACCESS INTERFACE:", .));
}
<output> "Access interfaces......";
copy-of $access_interfaces;
*/
/* DEBUG code for access port matching loop */
/*
for-each ($dhcps//dhcp-security-entry[vlan-name = $vlan]) {
var $port = jcs:split("[[.period.]]", ./intf-name);
expr jcs:output(concat("IFL:", $port));
if ($access_interfaces[. = $port]){
expr jcs:output(concat("MATCHED ACCESS INTERFACE:", $port));
}
}
*/
/* Display what IP addresses we will create a static bypass for */
for-each ($dhcps//dhcp-security-entry[vlan-name = $vlan][state = 'BOUND']) {
var $port = jcs:split("[[.period.]]", ./intf-name);
var $ip_address = ./ip-address;
if ($ip_address) {
if ($access_interfaces[. = $port]){
expr jcs:output( concat('Adding ', $ip_address, ' to dhcp-security static bypass to group ', $group, ' for interface ', $port));
<output> concat('Added ', $ip_address, ' to dhcp-security static bypass to group ', $group, ' for interface ', $port);
expr jcs:syslog( "change.info", concat('Added ', $ip_address, ' to dhcp-security static bypass to group ', $group, ' for interface ', $port));
}
}
}
/* Check to see if there are any entries and exit if not */
var $ips = $dhcps//dhcp-security-entry[vlan-name = $vlan][state = 'BOUND']/ip-address/text();
if ( jcs:empty($ips)) {
<output> concat("Device has no dynamic DHCP bindings currently on VLAN ", $vlan,". No changes will be made.");
}
else {
var $config-changes = {
<configuration> {
<vlans> {
for-each ($dhcps//dhcp-security-entry[vlan-name = $vlan][state = 'BOUND']) {
/* only create config section for access ports, get ifd from ifl */
var $port = jcs:split("[[.period.]]", ./intf-name);
if ($access_interfaces[. = $port]){
<vlan> {
<name> ./vlan-name;
<forwarding-options>{
<dhcp-security>{
<group> {
<name> $group;
<interface> {
<name> ./intf-name;
<static-ip> {
<name> ./ip-address;
<mac> ./hw-address;
}
}
}
}
}
}
}
}
}
}
}
var $load-action = "merge";
var $options := {
<commit-options> {
<log> "added static DHCP security bindings";
}
}
var $conn_handle = jcs:open();
var $results := {
call jcs:load-configuration( $action=$load-action,
$commit-options=$options,
$configuration=$config-changes, $connection=$conn_handle);
}
var $close-results = jcs:close($conn_handle);
if ($results//xnm:error) {
for-each ($results//xnm:error) {
<output> message;
}
}
if ($results//xnm:warning) {
for-each ($results//xnm:warning) {
<output> message;
}
}
/* restart the dhcpd daemon */
if ($dhcp-restart != "False") {
expr jcs:output("Restarting DHCPD daemon");
var $dhcpd_restart_rpc = <restart-daemon> {
<daemon-name> 'dhcp-service';
<gracefully>;
}
var $result = jcs:invoke( $dhcpd_restart_rpc );
<output> $result;
}
}
}
}