import serial
import time
# Serial port configuration
ser = serial.Serial("/dev/ttyUSB0", timeout=1)
print(f"Connecting to {ser.name}...")
# Strings for prompt detection
initPrompt = "Initializing Flash"
swPrompt = "switch:"
ynPrompt = " (y/n)?"
# Function to wait for specific prompt and send command
def wait_for_prompt_and_send(prompt, command):
while True:
response = ser.read_until(prompt.encode("utf-8")).decode("utf-8")
if prompt in response:
print(f"Found prompt: {prompt}")
ser.write(command)
ser.write(b"\n")
break
# Connect and send initial break commands
ser.write(b"\r")
print("Sent carriage return...")
time.sleep(0.5)
wait_for_prompt_and_send(initPrompt, b"\x03") # break command
# Initialize flash
wait_for_prompt_and_send(swPrompt, b"flash_init\n\n")
# Delete vlan.dat
wait_for_prompt_and_send(swPrompt, b"del flash:vlan.dat\n")
wait_for_prompt_and_send(ynPrompt, b"y\n")
# Delete config.text
wait_for_prompt_and_send(swPrompt, b"del flash:config.text\n")
wait_for_prompt_and_send(ynPrompt, b"y\n")`
ser.close()
I have to reset alot of old Cisco switches so I wanted to automate the process.
Instead of holding down the physical button on the switch, the script should issue a break command after the programm ran the initPrompt initializing flash. However, it doesn't do that. All I see on the python console is that it sent the carriage return and found the prompt: initializing flash. When I open the SecureCRT session and check the status of the startup, the switch goes through the normal boot so the break definitly didn't work.
Win 10 & Python 3.10
breakto do? it seems like the break would allow your script to continue to the nextwait_for_prompt_and_sendcallwhile Trueloop, allowing the function to returnbreakbut you are talking about sendingb"\x03"command. In that case I have no idea, but sounds like maybe that is not the right command sequence? Something specific to Cisco router anyway