#!/usr/bin/env python3
import serial
import time

# Configuration
port = "/dev/ttyUSB0"
baudrate = 115200
login_prompt = "ts7600-4a9f81 login:"
password_prompt = "Password:"
success_prompt = "root@ts7600-4a9f81:~#"
commands = ["uname -a"]

# Open the serial port
ser = serial.Serial(port, baudrate, timeout=1)

def send_command(command, prompt):
    ser.write(command.encode('utf-8') + b'\n')
    ser.flush()
    while True:
        response = ser.readline().decode('utf-8')
        print(response, end='')
        if prompt in response:
            break

try:
    # Wait for the "login:" prompt
    send_command('', login_prompt)

    # Enter the username
    ser.write("root\n".encode('utf-8'))

    # Wait for the "Password:" prompt
    send_command('', password_prompt)

    # Enter the password
    ser.write("asd123\n".encode('utf-8'))

    # Check for successful login
    response = ser.readline().decode('utf-8')
    if success_prompt not in response:
        print("Login failed.")
        ser.close()
    else:
        print("Login successful.")

        # Execute commands
        for command in commands:
            send_command(command, success_prompt)

    # Close the serial port
    ser.close()

except serial.SerialException as e:
    print("Error: ", e)

