-
Notifications
You must be signed in to change notification settings - Fork 0
/
ec_update.py
86 lines (67 loc) · 2.43 KB
/
ec_update.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
#! /usr/bin/env python3.8
# -*- coding: utf-8 -*-
import getpass
import argparse
import threading
from os.path import join as path_join
from helpers import edgecore
from helpers import host
from helpers import auth
def main():
update_cmd = '''
copy tftp file\r
%s\r
2\r
%s\r
%s\r
configure\r
boot system opcode:%s\r
exit\r
reload\r
y\r
'''
parser = argparse.ArgumentParser(description='Update firmware')
parser.add_argument('switch_list',
type=str,
help='List of Edge-Core switches separated by space')
parser.add_argument('tftp_server',
type=str,
help='TFTP server where firmwares are stored')
parser.add_argument('tftp_dir',
type=str,
help='TFTP server firmware path')
parser.add_argument('firmware_name',
type=str,
help='New firmware to download from tftp')
parser.add_argument('-cf', '--credentials_file')
args = parser.parse_args()
edgecore.user, edgecore.password = auth.ReadCredentialsFromFile(args.credentials_file)
switches = host.RemoveDuplicatesFromList(args.switch_list.split(' '))
if not host.IsIpValid(switches):
sys.exit('One or more IP addresses are not valid')
up, down = host.SplitWorkingDeadHosts(switches)
print('Hosts in Up state: %s' % ', '.join(up))
print('Hosts in Down state: %s' % ', '.join(down))
for switch in up:
inactive_fw = ''
try:
inactive_fw = edgecore.InactiveFirmware(switch)
except FileNotFoundError:
print('No unused firmwares on device %s.' % switch)
continue
edgecore.ExecuteReadViaTelnet(switch, 'delete file name %s' % inactive_fw)
commands = update_cmd % (args.tftp_server,
path_join(args.tftp_dir, args.firmware_name),
args.firmware_name,
args.firmware_name)
thread_list = list()
for switch in up:
# thread_list.append(threading.Thread(target=edgecore.ExecuteReadViaTelnet, args=(switch, commands,)))
thread_list.append(threading.Thread(target=edgecore.ExecuteViaTelnet, args=(switch, commands,)))
for thread in thread_list:
thread.start()
for thread in thread_list:
thread.join()
print('End reached...')
if __name__ == '__main__':
main()