-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection.py
More file actions
109 lines (77 loc) · 2.97 KB
/
Copy pathconnection.py
File metadata and controls
109 lines (77 loc) · 2.97 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
"""
Connects to a Cisco router through a serial connection and performs a factory reset.
Logs in using credentials from environment variables and enters enable mode.
Partially clears the router configuration, but does not perform a complete factory reset.
"""
import serial
import time
import os
from dotenv import load_dotenv
# Load the router login credentials from the .env file
load_dotenv()
username = os.getenv("ROUTER_USERNAME")
password = os.getenv("ROUTER_PASSWORD")
enable_password = os.getenv("ROUTER_ENABLE_PASSWORD")
# Open a serial connection to the router
ser = serial.Serial(port="/dev/ttyUSB1", baudrate=9600, timeout=5)
def read_output():
"""
Reads and returns available output from the serial connection.
Waits 10 seconds for the router to respond, then reads all available
bytes from the serial buffer and decodes them as UTF-8.
Returns:
str: The decoded output from the serial port.
"""
time.sleep(10)
return ser.read(ser.in_waiting).decode("utf-8", errors="ignore")
# Send an initial newline to wake up the router and read the response
ser.write(b"\r\n")
output = read_output()
print(f"1: {repr(output)}")
# Handle the login prompt
if "Username" in output:
ser.write(f"{username}\r\n".encode())
output = read_output()
print(f"2: {repr(output)}")
if "Password" in output:
ser.write(f"{password}\r\n".encode())
output = read_output()
print(f"3: {repr(output)}")
# Enter enable mode after login
if ">" in output:
print("Logged in!")
ser.write(b"enable\r\n")
output = read_output()
print(f"4: {repr(output)}")
if "Password" in output:
ser.write(f"{enable_password}\r\n".encode())
output = read_output()
print(f"5: {repr(output)}")
if "#" in output:
print("Enable mode activated!")
# Perform the factory reset when enable mode is active
if "#" in output:
print("Enable mode activated!")
# Delete the startup configuration
ser.write(b"write erase\r\n")
output = read_output()
print(f"Write erase: {repr(output)}")
if "confirm" in output.lower():
ser.write(b"\r\n")
output = read_output()
print(f"Confirmed: {repr(output)}")
# Delete the VLAN database file
ser.write(b"delete /force flash:vlan.dat\r\n")
output = read_output()
print(f"Delete vlan.dat: {repr(output)}")
# Reload the router to apply the factory reset
ser.write(b"reload\r\n")
output = read_output()
print(f"Reload: {repr(output)}")
if "confirm" in output.lower() or "save" in output.lower():
ser.write(b"no\r\n")
output = read_output()
ser.write(b"\r\n")
print("Router restarting...")
print("Factory reset complete!")
ser.close()