1

I am having issues with pexpect detecting "$" of the Linux command prompt. To give a better picture of what I am trying to accomplish:

Example Command Line prompt that I want to trigger my pexpect:

testcomp@testcomp-desktop:~$

Example of my pexpect code to detect it:

child.expect(['$',pexpect.TIMEOUT])

What results from this after testing it is that it chooses pexpect.TIMEOUT even though the prompt is there. I have also tried a couple of different variations of "$" like "\$" and "~$"

4
  • @Asocia if you can't expect it then is there any way to verify that the command prompt is printed? Commented Jun 12, 2020 at 21:20
  • First: $ matches the end of a line when parsed as a regex; it doesn't match a dollar sign. [$] should work; whether \$ does depends on what kind of string you put it in. Commented Jun 12, 2020 at 22:26
  • Second: We really need a minimal reproducible example to know if the prompt should be seen. Please extend the code in your question to be a runnable example that should be able to match a prompt (maybe instantiate child by spawning something like bash -l -i) Commented Jun 12, 2020 at 22:27
  • @Charles Duffy, you were right the [$] expression does work. Thanks! Commented Jun 15, 2020 at 13:54

1 Answer 1

1

FYI this is the code that should work if you want to do a console connection to a Linux machine (I was using minicom). What was described in the comments above was to use '[$]' to detect the Linux Command Line Prompt and it seemed to work well.

child = pexpect.spawn("minicom -b 115200 -o -D /dev/ttyUSB0") check = child.expect(["login",'[$]']) if(check == 0): child.sendline(username) child.sendline(password) else: child.sendline("\r") 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.