-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathgit_download.py
More file actions
133 lines (116 loc) · 5.27 KB
/
git_download.py
File metadata and controls
133 lines (116 loc) · 5.27 KB
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
import httplib
import signal
import sys
# This function Changes the VR
def vr_change(vrid):
f = open("/proc/self/ns_id", "w")
f.write(vrid)
f.close()
def signal_handler(signal, frame):
vr_change('0')
print ("\n")
exit(0)
# This function downloads github pages
def pagegit(URL_location):
c = httplib.HTTPSConnection("raw.githubusercontent.com")
c.request("GET", "{0}".format(URL_location))
response = c.getresponse()
# print response.status, response.reason
return response.read()
# This function checkes to see if DNS is configured on the switch and downloads the main github page with the correct VR
def internet_check():
main_page_data = 'False'
url = '/extremenetworks/ExtremeScripting/master/EXOS/Python/README.md'
dns = exsh.clicmd('show configuration "nettools" | include "configure dns-client add name-server"', True)
if 'VR-Default' in dns:
try:
vr_change('2')
main_page_data = pagegit(url)
print ("\nInternet Connection found on VR-Default")
except:
main_page_data = 'False'
if 'VR-Mgmt' in dns and main_page_data == 'False':
try:
vr_change('0')
main_page_data = pagegit(url)
print ("\nInternet Connection found on VR-Mgmt")
except:
main_page_data = 'False'
return main_page_data
def main():
signal.signal(signal.SIGINT, signal_handler)
try:
from httplib import HTTPSConnection
except ImportError:
print ("HTTPS ImportError: This script is only supported on EXOS 21.X and above.")
exit(0)
# Checks for internet access and downloads/organizes the script table into a list of dictionaries
main_page_data = internet_check().split('\n')
if main_page_data[0] != 'False':
github = []
for line in main_page_data:
if '|[' in line:
line = line.split('|')
path = line[1].split('(')
name = path[0][1:-1]
detail = line[2].split('(')
#Check for space to omit script
if detail[0][:1] != ' ':
github.append({'name': name, 'path': path[1][:-1], 'Description': detail[0]})
# This part prints the scripts names and descriptions collected so you can select the script to download
n = 0
print ('Script:{0}Description:'.format(' '*29))
print ('-'*48)
for line in github:
n += 1
if n >= 10:
print ('{0}: {1} {2} {3}'.format(n, line['name'], (' ' * (31 - len(line['name']))), line['Description']))
else:
print (' {0}: {1} {2} {3}'.format(n, line['name'], (' ' * (31 - len(line['name']))), line['Description']))
print ("To quit press <q> then <enter>.")
# Collects user input and formats input for downloading the script readme file to get .py file name
input = raw_input("What script # would you like to download? ")
try:
input = int(input)
except:
pass
quit = ['q', 'quit', 'exit', 'goaway']
if input != '' and 1 <= input <= len(github) and path != 'NA':
userinput = int(input) - 1
# This uses the user input[index] to go to the script page to get the .py file name.
url = "/extremenetworks/ExtremeScripting/master/EXOS/Python/{0}/README.md".format(github[userinput]['path'])
script_page_data = pagegit(url)
script_page_data = script_page_data.split('\n')
for line in script_page_data:
if '.py)' in line and '.py]' in line:
line = line.split('(')
script_name = line[1][:-1]
# Uses the script name found above to download the .py file to a variable.
script_url = "/extremenetworks/ExtremeScripting/master/EXOS/Python/{0}/{1}".format(github[userinput]['path'], script_name)
source = pagegit(script_url)
# Writes the variable data to the switch with the script name
f = open( "/usr/local/cfg/{0}".format(script_name), 'w')
f.write(source)
f.close()
print ("\nBy downloading this file you agree to the License")
print ("Terms in the readme, available at the URL below.\n")
print ("https://github.com/extremenetworks/ExtremeScripting/blob/master/EXOS/Python/{0}/README.md\n".format(github[userinput]['path']))
print ("{0} was downloaded to /usr/local/cfg/.\n".format(script_name))
main()
if input.lower() in quit:
# Revert back to normal VR namespace
vr_change('0')
exit(0)
else:
print ("Wrong input please select valid number.")
else:
print ("DNS is misconfigured or no internet access was found on VR-Default or VR-Mgmt\n")
print ("Configure DNS with the command 'configure dns-client add name-server <DNS IP> vr <vr name>'")
# Revert back to normal VR namespace
vr_change('0')
if __name__ == '__main__':
try:
main()
except SystemExit:
# catch SystemExit to prevent EXOS shell from exiting to the login prompt
vr_change('0')