简体   繁体   English

通过串行通信自动执行登录操作

[英]Automating log-in action through serial communication

i am trying to automate the log-in function for a device that i communicate with through serial .我正在尝试为我通过serial通信的设备自动登录 function 。 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).为了达到login:提示我必须在设备启动时按回车,然后在一段时间后login:提示将出现,一旦程序发现“登录:”字符串,它就会输入用户名(或者至少那是计划)。 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).输入正确的用户名后,将显示Password:提示,如果我输入正确的密码我成功登录到设备,如果我输入错误的密码我必须重新开始(这意味着再次输入用户名)。 Also if i fail to log-in in the first try the login: prompt changes to username: .另外,如果我第一次登录失败,请尝试将login:提示更改为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.出于某种原因,我看起来像输入首先发送\r\n命令而不是用户名,然后是命令。 Any idea how to resolve this?知道如何解决这个问题吗?

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.我不知道串行模块,但库可能会将“Enter”与登录信息一起发送。 That would result in "Enter" as user name.这将导致“Enter”作为用户名。

Quick searching brought up this answer: https://stackoverflow.com/a/12892221/4252584快速搜索找到了这个答案: 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.另一方面,我想知道为什么您在串行线路上没有事先“Enter”键就进入登录提示。 Are you sure, you need the "Enter" key on the line?你确定,你需要在线上的“Enter”键吗?

Add time.sleep(0.1) , before you send a command, like this:在发送命令之前添加time.sleep(0.1) ,如下所示:

time.sleep(0.1)
ser.write(b"root")
time.sleep(0.1)
ser.write('\r'.encode())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM