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()
initPrompt
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)