-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsample_ztp_script.py
145 lines (100 loc) · 4.85 KB
/
sample_ztp_script.py
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
#!/usr/bin/env python
import sys
sys.path.append("/pkg/bin/")
from ztp_helper import ZtpHelpers
import json, tempfile
from pprint import pprint
class ZtpFunctions(ZtpHelpers):
def set_root_user(self):
# The hash for the password in config below translates to "lab"
# So root-user credentials will be netops/lab
config = """ !
username netops
group root-lr
group cisco-support
secret 5 $1$7kTu$zjrgqbgW08vEXsYzUycXw1
!
end"""
with tempfile.NamedTemporaryFile(delete=True) as f:
f.write("%s" % config)
f.flush()
f.seek(0)
result = self.xrapply(f.name)
if result["status"] == "error":
self.syslogger.info("Failed to apply root user to system %s"+json.dumps(result))
return result
if __name__ == "__main__":
# Create an Object of the child class, syslog parameters are optional.
# If nothing is specified, then logging will happen to local log rotated file.
ztp_script = ZtpFunctions(syslog_file="/root/ztp_python.log", syslog_server="11.11.11.2", syslog_port=514)
print "\n###### Debugs enabled ######\n"
# Enable verbose debugging to stdout/console. By default it is off
ztp_script.toggle_debug(1)
# Change context to XR VRF in the linux shell when needed. Depends on when user changes config to create network namespace.
print "\n###### Change context to user specified VRF ######\n"
ztp_script.set_vrf("global-vrf")
# Use the child class methods
print "\n###### Using Child class method, setting the root user ######\n"
ztp_script.set_root_user()
# Disable debugs
print "\n###### Debugs Disabled ######\n"
ztp_script.toggle_debug(0)
# Show commands using Parent class helper method: xrcmd
print "\n###### Executing a show command ######\n"
pprint(ztp_script.xrcmd({"exec_cmd" : "show running-config"}))
# Config apply with file using Parent class helper method: xrapply
print "\n###### Apply valid configuration using a file ######\n"
config = """ !
hostname customer
!
cdp
!
end"""
with tempfile.NamedTemporaryFile(delete=True) as f:
f.write("%s" % config)
f.flush()
f.seek(0)
print ztp_script.xrapply(f.name)
# Config apply with string using Parent class helper method: xrapply_string
print "\n###### Apply valid configuration using a string ######\n"
out = ztp_script.xrapply_string("hostname customer2")
pprint(out)
# xrapply with atomic option enabled on a successful case
with tempfile.NamedTemporaryFile(delete=True) as f:
f.write("%s" % config)
f.flush()
f.seek(0)
print ztp_script.xrapply(f.name, atomic=True)
# Send syslogs to the preset syslog destinations (see above)
print "\n###### Sending Syslogs to Server/file ######\n"
ztp_script.syslogger.info("Hostname updated")
ztp_script.syslogger.info(ztp_script.xrcmd({"exec_cmd" : "show running-config hostname"}))
# Error handling
print "\n###### Apply invalid configuration using a string ######\n"
pprint(ztp_script.xrapply_string("hostnaime customer2"))
# xrapply with atomic option enabled on a failure case
config_negative = """key chain Bundle-Ether29-pri
no macsec
macsec
key f9d3f4f01a50517afb0000000000000000000000000000000000000000000000
key-string password 95157010155425f56085d070c751c1f5a4e077777777777777777777777774054435155545b5d515108074d41080e54515650045c5f07545e05004753010f5c53 cryptographic-algorithm aes-128-cmac
"""
with tempfile.NamedTemporaryFile(delete=True) as f:
f.write("%s" % config_negative)
f.flush()
f.seek(0)
print ztp_script.xrapply(f.name, atomic=True)
# HTTP file download
print "\n###### Downloading file from HTTP server ######\n"
print ztp_script.download_file(file_url='http://1.2.29.64/sj21-ddd-01-07/ztp.script',
destination_folder='/disk0:/ztp/tmp')
# HTTPS file download with certificate validation
print "\n###### Downloading file from HTTPS server ######\n"
print ztp_script.download_file(file_url='https://1.2.29.64:5000/get-report',
destination_folder='/disk0:/ztp/tmp',
ca_cert='/disk0:/ztp/tmp/cacert.pem')
# HTTPS file download without certificate validation
print "\n###### Downloading file from HTTPS server ######\n"
print ztp_script.download_file(file_url='https://1.2.29.64:5000/get-report',
destination_folder='/disk0:/ztp/tmp',
validate_server=False)