pexpect / ptyprocess

Run a subprocess in a pseudo terminal
https://ptyprocess.readthedocs.io/en/latest/
Other
217 stars 71 forks source link

pty cannot read unicode color escapde #70

Closed demonguy closed 2 years ago

demonguy commented 2 years ago

My company has a ssh server, which would return QR code (by sending unicode and color escape), show in screenshot.

ssh xxxx@relay.office.com

6745260651f884d271e43d82d88331c

the intact output of ssh session is in the attachment ssh_output.log

However if i tried to use ptyprocess to read it , it just read the first 3 bytes and then hangs forever.

#!/usr/bin/env python
import ptyprocess

p = ptyprocess.PtyProcess.spawn(["/usr/bin/ssh", "chengyang@relay.office.com"])
s = p.read(1000)
print(s)  # this line return b'\x1b[c'
s = p.read(1000) # this line blocks forever

84b2acaced1385873c02b695b5b2376

Why would this happen and how can i solve it?

jquast commented 2 years ago

You need to reply to this sequence, "CSI Ps c Send Device Attributes (Primary DA)." https://gist.github.com/justinmk/a5102f9a0c1810437885a04a07ef0a91#file-xterm-control-sequences-txt-L356

Test it yourself in shell printf "\033[c", or python repl, print('\033[c'). My terminal replies "\033[?62;4c" which means "VT420 with Sixel graphics"

demonguy commented 2 years ago

That's new to me. Thanks