0

I am writing a simple script to check the ownership of a file. It is all working as expected but I am getting some random (or not) characters in the output.

here is the script:

import pexpect

p=pexpect.spawn('ssh root@%s'% addr)
p.sendline("ls -l /etc/gshadow")
p.expect("root@system:~#")
print("CMD %s:\n" %p.before.decode())

and here is the output

CMD ls -l /etc/gshadow
lrwxrwxrwx    1 root     root            15 Jun 22 08:46 /etc/gshadow -> /home/+/gshadow
:

^[[64;5Ruser@system:~/home$ 4;5R

Why do I get these characters surrounding the prompt and how can I prevent that?

Chillie
  • 1,356
  • 13
  • 16
daalgoo
  • 45
  • 4

1 Answers1

0

As per pexpect docs:

After each call to expect() the before and after properties will be set to the text printed by child application. The before property will contain all text up to the expected string pattern.

So the characters you see represent the raw strings that are sent to the terminal and that you would usually not see when working directly with a terminal. This includes instructions for color, cursor positioning, control characters etc.

You can find some of the characters in this wikipedia article: ANSI_escape_code.

Preventing them completely would probably involve finding/writing a parser for those. So maybe altering your pattern would be an easier solution?

Here is a similar SO question that involves pexpect and escapes.

This answer also provides a method to deal with control characters.

Chillie
  • 1,356
  • 13
  • 16