-
Notifications
You must be signed in to change notification settings - Fork 0
/
pushZoning.bash
executable file
·101 lines (85 loc) · 3.23 KB
/
pushZoning.bash
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
#!/usr/bin/env bash
# pushZoning.bash
# Version 1.0
# Author: Chip Copper
#
# This script pushes aliases, zones, and configs to a switch from
# local files. It can be used to restore configurations to a switch
# as a form of restore operation or can serve to duplicate zoning on
# another fabric.
echo "PUSH zoning information"
read -p "Username: " USERNAME
read -p "Password: " -s PASSWORD
echo ""
read -p "IP Address: " IPADDRESS
PROTO='http'
NEWPROTO="X"
read -p "Secure http (y/n) [n]: " NEWPROTO
if [[ "$NEWPROTO" == "y" ]] || [[ "$NEWPROTO" == "Y" ]];
then
PROTO='https'
fi
# Testing defaults
#USERNAME='admin'
#PASSWORD='Password'
#IPADDRESS='192.168.4.100'
NEWCONFIG="Y"
read -p "Activate config (y/n) [y]: " NEWPROTO
if [[ "$NEWPROTO" == "n" ]] || [[ "$NEWPROTO" == "N" ]];
then
NEWCONFIG="N"
fi
URI='running/brocade-zone/defined-configuration'
URI2='running/brocade-zone/effective-configuration'
# Establish a session with the switch and keep the credentials
export SANSESSION=$(curl -s -I -X POST -u "$USERNAME:$PASSWORD" $PROTO://$IPADDRESS/rest/login | grep "Authorization" | tr -d "\r\n")
# Get the current configuration checksum
export CHECKSUM=$(curl -s -H "$SANSESSION" -H "Content-Type: application/yang-data+json" -H "Accept: application/yang-data+json" $PROTO://$IPADDRESS/rest/$URI2/checksum \
| jq '.Response."effective-configuration" | {checksum: .checksum}')
# Push aliases
grep -q "null$" aliases.json
if (( $? )) ;
then
curl -s -X POST -H "$SANSESSION" -H "Content-Type: application/yang-data+json" -H "Accept: application/yang-data+json" --data @aliases.json $PROTO://$IPADDRESS/rest/$URI/alias
echo "Aliases pushed"
else
echo "No aliases to push."
fi
# Push zones
# Push zones
grep -q "null$" zones.json
if (( $? )) ;
then
curl -s -X POST -H "$SANSESSION" -H "Content-Type: application/yang-data+json" -H "Accept: application/yang-data+json" --data @zones.json $PROTO://$IPADDRESS/rest/$URI/zone
echo "Zones pushed"
else
echo "No zones to push."
fi
# Push configs
grep -q "null$" cfgs.json
if (( $? ));
then
curl -s -X POST -H "$SANSESSION" -H "Content-Type: application/yang-data+json" -H "Accept: application/yang-data+json" --data @cfgs.json $PROTO://$IPADDRESS/rest/$URI/cfg
echo "Configs pushed"
else
echo "No configs to push."
fi
# Push active config
if [[ "$NEWCONFIG" == "Y" ]];
then
grep -q "null$" active.data
if (( $? ));
then
export CFGNAME=$(cat active.data)
curl -s -X PATCH -H "$SANSESSION" -H "Content-Type: application/yang-data+json" -H "Accept: application/yang-data+json" --data "$CHECKSUM" $PROTO://$IPADDRESS/rest/$URI2/cfg-name/$CFGNAME
echo "Defined configuration saved and config activated"
else
curl -s -X PATCH -H "$SANSESSION" -H "Content-Type: application/yang-data+json" -H "Accept: application/yang-data+json" --data "$CHECKSUM" $PROTO://$IPADDRESS/rest/$URI2/cfg-action/1
echo "No active config at source, defined configuration saved"
fi
else
curl -s -X PATCH -H "$SANSESSION" -H "Content-Type: application/yang-data+json" -H "Accept: application/yang-data+json" --data "$CHECKSUM" $PROTO://$IPADDRESS/rest/$URI2/cfg-action/1
echo "No active config at source, defined configuration saved"
fi
# End the session.
curl -s -X POST -H "$SANSESSION" $PROTO://$IPADDRESS/rest/logout