1

i am trying to automate the log-in function for a device that i communicate with through serial. In order to reach the login: prompt i got to press enter while the device boots and then after sometime the login: prompt will show up, once it the program spots the 'login:' string it enter the username(or at least that's the plan). After entering the correct username the Password: prompt will show up, if i enter the correct password i successfully log-in to the device, if i enter the wrong password i have to start over(which means to enter the username again). Also if i fail to log-in in the first try the login: prompt changes to username:.

I have made this till now, but

import serial
import re
from time import sleep
import time

ser = serial.Serial('COM3', timeout=1)
ser.baudrate = 115200

def auto_login():
    while True:
        output = ser.read(10000).decode('utf-8', 'ignore')
        testmode_command = ser.write("\r\n".encode())
        print(output)
        if "1 : press [Enter] for test mode / [Esc+Enter] for plain Linux" in output:
            ser.write(testmode_command)
        if " login:" in output:
            break



def login_repeat():
    login = b"root \r\n"
    output = ser.read(10000).decode('utf-8', 'ignore')
    print(output)
    if " login:" in output:
        ser.write(login)
    if "Username:" in output:
        ser.write(login)

def pass_word():
    password = b"p \r\n"
    time.sleep(0.1)
    output = ser.read(10000).decode('utf-8', 'ignore')
    print(output)
    if "Password:" in output:
        ser.write(password)

The result i am getting is :

Login incorrect
Username: 
root 

System starting up, please try later

Login incorrect
Username: 
root 

For some reason i looks like the enter is sent first the \r\n command instead of the username and then the command. Any idea how to resolve this?

Rozakos
  • 608
  • 2
  • 6
  • 20

2 Answers2

2

Add time.sleep(0.1), before you send a command, like this :

time.sleep(0.1)
ser.write(b"root")
time.sleep(0.1)
ser.write('\r'.encode())
Deezer
  • 135
  • 9
1

Just as a hunch, are you sure, you have no buffering issues. I don't know the serial module but it might be possible that the library sends the "Enter" together with the login information. That would result in "Enter" as user name.

Quick searching brought up this answer: https://stackoverflow.com/a/12892221/4252584

You might try to explicitly flush the buffers.

On the other hand I am wondering why you get to the login prompt without prior "Enter" key on the serial line. Are you sure, you need the "Enter" key on the line?

DwightKendall
  • 194
  • 1
  • 3