0
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

5
  • 1
    what are you expecting the break to do? it seems like the break would allow your script to continue to the next wait_for_prompt_and_send call Commented Aug 16, 2023 at 10:19
  • the break is to stop the boot process so it goes into the password recovery mode where the switch: prompt shows up. Kinda like pushing the button Commented Aug 16, 2023 at 11:09
  • 1
    how does the break stop the boot process? in this code it breaks out of the while True loop, allowing the function to return Commented Aug 16, 2023 at 11:12
  • Does it not? Thats how I learned it to, insert the b"\x03". In that case would there be another command to force the switch to go into the switch: menu instead of the login menu? Commented Aug 16, 2023 at 11:15
  • I see, I was talking about the Python break but you are talking about sending b"\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 Commented Aug 16, 2023 at 11:22

2 Answers 2

0

Looking at the docs, you could try the send_break(duration=0.25) parameter and maybe play around a bit with the duration value.

Sign up to request clarification or add additional context in comments.

Comments

0

Your timeout appears to be too short to detect the Initializing Flash prompt

ser = serial.Serial("/dev/ttyUSB0", timeout=1)

1-second barely gives the router enough time to start the init sequence... you need to make it longer, such as 15-seconds

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.