-
Notifications
You must be signed in to change notification settings - Fork 0
/
virl-renew.py
80 lines (74 loc) · 2.6 KB
/
virl-renew.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
# -*- coding: utf-8 -*-
import json
import time
import re
import logging
from gzip import GzipFile
import urllib2
import cookielib
if __name__ == "__main__":
url_base = 'http://10.10.20.160'
username = 'uwmadmin'
password = 'password'
#login
login_url = url_base+'/login/'
#s = requests.session()
cj = cookielib.LWPCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders = [
("User-agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.5) Gecko/20031107 Debian/1.5-3"),
("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"),
("Referer", login_url),
]
#for session id token
http = opener.open(login_url)
content = http.read()
http.close()
m = re.search('name="csrf_token" type="hidden" value="([^"]+)"', content)
csrf_token = m.group(1)
time.sleep(5)
opener.addheaders = [
("User-agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.5) Gecko/20031107 Debian/1.5-3"),
("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"),
("Referer", login_url),
('Content-Type', 'application/x-www-form-urlencoded'),
]
params = 'csrf_token={0}&username={1}&password={2}&submit=Login'.format(csrf_token, username, password)
http = opener.open(login_url, params)
content = http.read()
http.close()
time.sleep(2)
#get license status
status_url = url_base + '/admin/salt/'
for retry in xrange(3):
try:
http = opener.open(status_url)
content = http.read()
http.close()
except Exception,e:
continue
renew = False
if content.find('Failed to collect current salt')>-1: #license expired more than 7 days
renew = True
else:
m = re.search('<dt>Allowed Cisco node count:</dt><dd>([0-9]+)', content)
licenses = int(m.group(1))
if licenses>5:
print('VIRL license accquired already, no renew required')
renew = False
else:
renew = True
#renew license page
if renew:
#renew license page
print('Renew license {0} times...'.format(retry+1))
renew_url = url_base + '/admin/salt/?renew=1'
try:
content = opener.open(renew_url)
except Exception, e:
pass
else:
print('wait extra 30 seconds for VIRL fully renewal')
time.sleep(30)
break
time.sleep(10)