-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathns_mod_basics.py
118 lines (106 loc) · 4.36 KB
/
ns_mod_basics.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
import json, requests, base64, sys, os, re
requests.urllib3.disable_warnings()
def getAuthCookie(connectiontype,nitroNSIP,nitroUser,nitroPass):
url = '%s://%s/nitro/v1/config/login' % (connectiontype, nitroNSIP)
headers = {'Content-type': 'application/vnd.com.citrix.netscaler.login+json'}
json_string = {
"login":{
"username":nitroUser,
"password":nitroPass,
}
}
payload = json.dumps(json_string)
try:
response = requests.post(url, data=payload, headers=headers, verify=False, timeout=1.0)
response.raise_for_status()
except requests.exceptions.RequestException as e:
print(e)
sys.exit(1)
except requests.exceptions.HTTPError as err:
print(err)
sys.exit(1)
cookie = response.cookies['NITRO_AUTH_TOKEN']
nitroCookie = 'NITRO_AUTH_TOKEN=%s' % cookie
print ("Creating Authentication Token: %s" % response.reason)
return nitroCookie
def logOut(connectiontype,nitroNSIP,authToken):
url = '%s://%s/nitro/v1/config/logout' % (connectiontype, nitroNSIP)
headers = {'Content-type': 'application/vnd.com.citrix.netscaler.logout+json','Cookie': authToken}
json_string = {
"logout":{}
}
payload = json.dumps(json_string)
response = requests.post(url, data=payload, headers=headers, verify=False)
print ("\nLogging Out: %s" % response.reason)
def saveNsConfig(connectiontype,nitroNSIP,authToken):
url = '%s://%s/nitro/v1/config/nsconfig?action=save' % (connectiontype, nitroNSIP)
headers = {'Content-type': 'application/json','Cookie': authToken}
json_string = {
"nsconfig":{}
}
payload = json.dumps(json_string)
response = requests.post(url, data=payload, headers=headers, verify=False)
print ("\nSaving Netscaler Configuration: %s" % response.reason)
#### File handling ####
def sendFile(connectiontype,nitroNSIP,authToken,nscert,localcert,nscertpath):
url = '%s://%s/nitro/v1/config/systemfile' % (connectiontype, nitroNSIP)
headers = {'Content-type': 'application/vnd.com.citrix.netscaler.systemfile+json','Cookie': authToken}
f = open(localcert, 'r')
filecontent = base64.b64encode(f.read())
json_string = {
"systemfile": {
"filename": nscert,
"filelocation": nscertpath,
"filecontent":filecontent,
"fileencoding": "BASE64",}
}
payload = json.dumps(json_string)
response = requests.post(url, data=payload, headers=headers, verify=False)
print ("UPLOADING %s: %s") % (nscert, response.reason)
def enable_nsfeature(connectiontype,nitroNSIP,authToken,feature):
url = '%s://%s/nitro/v1/config/nsfeature?action=enable' % (connectiontype, nitroNSIP)
headers = {'Content-type': 'application/json','Cookie': authToken}
json_string = {
"nsfeature": {
"feature": feature,
}
}
payload = json.dumps(json_string)
response = requests.post(url, data=payload, headers=headers, verify=False)
print ("\nEnabling nsfeature %s: %s" % (feature, response.reason))
print ("Response: %s" % response.text)
#### dns,ntp related tasks ####
def addNtpServer(connectiontype,nitroNSIP,authToken,ntp_serverip):
url = '%s://%s/nitro/v1/config/ntpserver' % (connectiontype, nitroNSIP)
headers = {'Content-type': 'application/json','Cookie': authToken}
json_string = {
"ntpserver": {
"serverip": ntp_serverip,
# "servername": servername,
# "minpoll" : minpoll,
# "maxpoll" : maxpoll,
# "autokey" : autokey,
# "key" : key,
}
}
payload = json.dumps(json_string)
response = requests.post(url, data=payload, headers=headers, verify=False)
print ("\nAdding NTP Server: %s" % response.reason)
print ("Response: %s" % response.text)
def addDnsNameServer(connectiontype,nitroNSIP,authToken,dns_serverip,dnsvservername,dns_local,dns_state,dns_type,dnsprofilename):
url = '%s://%s/nitro/v1/config/dnsnameserver' % (connectiontype, nitroNSIP)
headers = {'Content-type': 'application/json','Cookie': authToken}
json_string = {
"dnsnameserver": {
"ip": dns_serverip,
#"dnsvservername": dnsvservername,
#"local": dns_local,
#"state": dns_state,
#"type": dns_type,
#"dnsprofilename": dnsprofilename,
}
}
payload = json.dumps(json_string)
response = requests.post(url, data=payload, headers=headers, verify=False)
print ("\nAdding DNS Server: %s" % response.reason)
print ("Response: %s" % response.text)