clarkwang/sexpect

expect -re can't match specific character ~ $ cygwin on windows 10

rpi4iot opened this issue · 2 comments

expect -re can't match specific character ~ $
cygwin on windows 10

updated example/ssh.sh
expect -re ':~ \$ $'  try to match prompt before run any command
but failed with  'ret=207'
expect -ex 'raspberrypi:'   works
expect -re 'raspberrypi:'   works
expect -ex 'raspberrypi:~'   don't work
expect -re 'raspberrypi:~'   don't work
cygcheck (cygwin) 3.1.2  X86-64
Windows 10 Professional Ver 10.0 Build 18363   X86-64

root@DESKTOP-XYZ ~/sexpect-master/examples
$ bash -x ./ssh.sh -p raspberry pi@192.168.1.19

+ sexpect expect -nocase -re 'raspberrypi:~'

Linux raspberrypi 4.19.75+ #1270 Tue Sep 24 18:38:54 BST 2019 armv6l

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

while true; do
    sexpect expect -nocase -re 'raspberrypi:~'
	ret=$?
	if [[ $ret == 0 ]]; then
	    break
    elif sexpect chkerr -errno $ret -is eof; then
        sexpect wait
        exit
    elif sexpect chkerr -errno $ret -is timeout; then
        sexpect close
        sexpect wait
        fatal "timeout waiting for password prompt"
    else
        fatal "unknown error: $ret"
    fi
done

sexpect send -enter 'uptime'

sexpect set -idle 5
sexpect interact

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sat Jan 25 04:40:09 2020 from 192.168.1.8

pi@raspberrypi:~ $ + ret=207
+ [[ 207 == 0 ]]
+ sexpect chkerr -errno 207 -is eof
+ sexpect chkerr -errno 207 -is timeout
+ sexpect close
+ sexpect wait
+ fatal 'timeout waiting for password prompt'
+ echo '!! timeout waiting for password prompt'
!! timeout waiting for password prompt
+ exit 1


$ cygcheck.exe -V
cygcheck (cygwin) 3.1.2
System Checker for Cygwin
Copyright (C) 1998 - 2019 Cygwin Authors

$ cygcheck.exe -s
Cygwin Configuration Diagnostics
Current System Time: Sat Jan 25 04:36:19 2020

Windows 10 Professional Ver 10.0 Build 18363

It seems the ansi sequence impact the regex match

-re 'pi@raspberrypi: ~' works (note: space between : and ~)

pi@raspberrypi:~ $ echo $PS1
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w \$\[\033[00m\]
pi@raspberrypi:~ $

b'\r\n\x1b]0;pi@raspberrypi: ~\x07\x1b[01;32mpi@raspberrypi\x1b[00m:\x1b[01;34m~ $\x1b[00m \r\n\x1b]0;pi@raspberrypi: ~\x07\x1b[01;32mpi@raspberrypi\x1b[00m:\x1b[01;34m~ $\x1b[00m '

You're right. Sometimes $PS1 has parts of it colorized. You need to take that into consideration when writing the regex pattern. Usually I would change PS1 after successful login to make things easier.