-
Notifications
You must be signed in to change notification settings - Fork 0
/
Script_Initialize_Switch.py
127 lines (97 loc) · 3.6 KB
/
Script_Initialize_Switch.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
119
120
121
122
123
124
125
126
127
import serial
import time
import sys
from time import sleep
from serial.serialutil import Timeout
#Change the COM port to the one you are using.
com_port = "COM-TO-USE"
def send_to_console(ser: serial.Serial, command: str, wait_time: float = 1):
"""
Function to send the commands to the serial port
Args:
ser: Connecting to serial port
command: string, The command that will be sent to the serial port
wait_time: float, wait time after executing the command
"""
command_to_send = command + "\r"
ser.write(command_to_send.encode('utf-8'))
sleep(wait_time)
print(ser.read(ser.inWaiting()).decode('utf-8'), end="")
def configure_switch():
print("Configure VLAN1")
ser = serial.Serial(com_port, timeout=1)
print(f"Connecting to port {ser.name}...")
#Switch to configuration mode
send_to_console(ser, "enable")
send_to_console(ser, "conf t")
# Configure VLAN1
send_to_console(ser, "interface vlan 1")
send_to_console(ser, "ip address 192.168.10.10 255.255.255.0")
send_to_console(ser, "no shutdown")
send_to_console(ser, "exit")
# Close serial connexion
ser.close()
def tftp_transfer():
print("Download .tar file")
ser = serial.Serial(com_port, timeout=1)
print(f"Connecting to port {ser.name}...")
#Switch to configuration mode
send_to_console(ser, "end")
send_to_console(ser, "enable")
#Get the new tar file via TFTP and extract it to the flash directory
send_to_console(ser, "archive tar /xtract tftp://192.168.10.12/c1000-universalk9-tar.152-7.E6.tar flash: \r\n")
# Close serial connexion
ser.close()
def boot_system():
print("Boot the new firmware")
ser = serial.Serial(com_port, timeout=1)
print(f"Connecting to port {ser.name}...")
#Switch to configuration mode
send_to_console(ser, "end")
send_to_console(ser, "enable")
send_to_console(ser, "conf t")
#Boot the new system firmware
send_to_console(ser, "boot system flash:/c1000-universalk9-mz.152-7.E6/c1000-universalk9-mz.152-7.E6.bin")
send_to_console(ser, "exit")
send_to_console(ser, "reload")
send_to_console(ser, "yes")
send_to_console(ser, "\r\n")
# Close serial connexion
ser.close()
def delete_directory():
print("Delete old firmware files and folders")
ser = serial.Serial(com_port, timeout=1)
print(f"Connecting to port {ser.name}...")
#Switch to enable mode
send_to_console(ser, "\r\n")
send_to_console(ser, "enable")
#Delete the folders and files of the old firmwares
send_to_console(ser, "delete /force /recursive flash:/c1000-universalk9-mz.152-7.E4")
send_to_console(ser, "delete /force flash:/c1000-universalk9-tar.152-7.E4.tar")
send_to_console(ser, "delete /force flash:/c1000-universalk9-mz.152-7.E6.bin")
send_to_console(ser, "delete /force flash:/config.text")
send_to_console(ser, "delete /force flash:/private-config.text")
send_to_console(ser, "exit")
#Reboot the switch
send_to_console(ser, "reload")
# Close serial connexion
ser.close()
def timer(seconds):
for remaining in range(seconds, 0, -1):
sys.stdout.write("\r")
sys.stdout.write("{:2d} seconds remaining".format(remaining))
sys.stdout.flush()
time.sleep(1)
sys.stdout.write("\rDone !\n")
#Call Configure_switch function
configure_switch()
#Call tftp_transfer function
tftp_transfer()
#Timer with 480 seconds to complete the TFTP transfer
timer(480)
#Call boot_system function
boot_system()
#Timer with 75 seconds to reboot the system
timer(75)
#Call delete_directory function
delete_directory()