forked from coreyhines/Arista
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetEOS.py
More file actions
executable file
·173 lines (157 loc) · 6.21 KB
/
getEOS.py
File metadata and controls
executable file
·173 lines (157 loc) · 6.21 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env Python
import argparse
import wget
import sys
import re
import time
from urllib2 import urlopen
from subprocess import Popen, call, check_output
import os
from os.path import expanduser
images = {'veos':['vEOS-lab'],
'ceos':['cEOS'],
'vmdk':['EOS.vmdk'],
'all':['EOS','EOS-2GB', 'EOS-PDP', 'cEOS','vEOS-lab','EOS.vmdk'],
'eos':['EOS'],
'eos-2gb':['EOS-2GB'],
'eos-pdp':['EOS-PDP']}
outputFilename = []
def check_native_vpn():
"Function to check if there is a native Arista VPN client"
native_check = check_output(["scutil","--nc","list"]).split('\n')
for r1 in native_check:
if 'arista' in r1.lower():
return(r1[r1.find('"')+1:r1.rfind('"')])
else:
return(False)
def get_latest_rn(r_url):
#Function to grab the most current posted release notes and return filename
all_release_notes = {}
content = urlopen(r_url).read().split('\n')
for r1 in content:
if '-RN-v' in r1:
release_file = r1[r1.find('">E')+2:r1.find('</a>')]
ver = release_file[release_file.find('-v')+2:release_file.find('.pdf')]
all_release_notes[ver] = release_file
latest_ver = 0.0
for r1 in all_release_notes:
if float(r1) > latest_ver:
latest_ver = float(r1)
return(all_release_notes[str(latest_ver)])
def main(args,version):
urls = []
rn_url = "http://dist/release/EOS-" + version + "/final/doc/"
home = expanduser("~")
outputDir = home + "/Downloads/"
failed = False #Default value to False
native_vpn_disconnect = False #Variable to determine if vpn should be disconnected
vpn_reconnect_counter = 0 #Counter to prevent a forever loop
vpn_max_reconnect = 2000 #Max VPN reconnect tries to prevent forever loop
for image in images[args.package]:
if image == "cEOS":
ext = ".tar.xz"
elif image == "EOS.vmdk":
ext = ""
else:
ext = ".swi"
urls.append("http://dist/release/EOS-" + version + "/final/images/" + image + ext)
if image == "EOS.vmdk":
image = "EOS"
ext = ".vmdk"
outputFilename.append(outputDir + image + "-" + version + ext)
#Check to see if native OSX vpn is configured
vpn_name = check_native_vpn()
if vpn_name:
vpncheck = check_output(["scutil", "--nc", "status", vpn_name])
vpncheckStr = vpncheck.split("\n")
#Check to see if native vpn is connected
if vpncheckStr[0] == "Connected":
print("VPN Connection is up...\n")
print ("Downloading EOS:" + version + " To: " + outputDir + "\n")
try:
for url, filename in map(None, urls, outputFilename):
file = wget.download(url, filename)
print("\nFile downloaded to " + filename)
if args.releaseNotes:
release_note = get_latest_rn(rn_url)
if release_note:
file = wget.download(rn_url + release_note, outputDir+release_note)
print("\nFile downloaded to " + outputDir+release_note)
except:
failed = True
file = None #Added in so it can be used to evaluate later on
else:
print("VPN Connection is down...\n")
print("Attempting to connect to {0}".format(vpn_name))
check_output(["scutil", "--nc", "start", vpn_name])
while True:
vpncheck = check_output(["scutil", "--nc", "status", vpn_name])
vpncheckStr = vpncheck.split("\n")
vpncheckResult = vpncheckStr[0].strip()
if vpncheckResult == "Connected":
if not args.stayConnected:
native_vpn_disconnect = True
break
elif vpn_reconnect_counter >= vpn_max_reconnect:
failed = True
break
else:
vpn_reconnect_counter += 1
#Check to make sure that the VPN tunnel didn't hit reconnect max tries
if not failed:
print ("Downloading EOS: " + version + " To: " + outputDir + "\n")
try:
for url, filename in map(None, urls, outputFilename):
file = wget.download(url, filename)
print("\nFile downloaded to " + filename)
if args.releaseNotes:
release_note = get_latest_rn(rn_url)
if release_note:
file = wget.download(rn_url + release_note, outputDir+release_note)
print("\nFile downloaded to " + outputDir+release_note)
except:
failed = True
file = None #Added in so it can be used to evaluate later on
else:
print("Unable to start VPN connection:\nEither you are not connected to the internet or you don't have the right VPN name")
if file:
for filename, urls in map(None, outputFilename, urls):
filesize = os.stat(filename)[6]
if filesize < 1024:
with open(filename) as myfile:
content = myfile.read()
match = re.search(r'<p>(.*).</p>', content)
reason = match.group(1)
os.remove(filename)
print ("\n\nThere was an error downloading: " + url + "\n")
print ("\t" + reason + "\n")
#Check to see if script opened VPN tunnel, if so disconnect from VPN
if native_vpn_disconnect:
print('\nDisconnecting VPN connection')
check_output(["scutil","--nc","stop",vpn_name])
print('You have been disconnected from {0}\n'.format(vpn_name))
else:
print('\nHave a great day!\n')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
"version", type=str, help="EOS image version", default=None)
parser.add_argument(
"-p", "--package", type=str, default="eos", choices=["eos", "eos-2gb", "vmdk", "veos", "ceos", "pdp", "all"],
help="specify which EOS packaging", required=False)
parser.add_argument(
"-s", "--stayConnected", type=bool, default=False,
help="Overrides the default behavior of disconnecting the VPN if getEOS.py made the VPN connection.", required=False)
parser.add_argument(
"-r","--releaseNotes", type=bool, default=False,
help="Downloads the Current release notes for this version release.", required=False
)
args = parser.parse_args()
version = args.version.upper()
if not re.match(r"\d\.\d{1,2}\.\d{1,2}.\d{0,2}[FM]?$", version):
parser.print_usage()
parser.exit()
#Adding in redundancy check in case no version supplied.
while not version:
version = raw_input("Enter EOS Version: ").strip()
main(args,version)