expect

How to get non-latin output from expect script


I'm trying to write a script to test telnet connection. I use CP866 encoding in qnx 6-like OS and need to have the whole cyrillic expect's output. My expect version is 5.33, tcl is 8.3.5

There is no problem with the latin output at all, but I get question marks instead of cyrillic one. What did I do wrong? Should expect display non-latin output in general? btw, it works. I can open telnet session, just output is not what I want

My code is:

#!/bin/sh

expect -c '
set timeout 5
spawn telnet 127.0.0.1
fconfigure $spawn_id -encoding cp866
expect {
    -re "Имя пользователя:|Username:" {                                  
        send -- "root\r"            
        exp_continue
    }
    -re "Пароль:|Password:" {
        send -- "12345678\r"                 
        exp_continue
    }
    -re "Выполнен вход|Signed in" {                        
        puts "-> Pass"
        exit 0
    }
    timeout {                                        
        puts "-> Fail"
        exit 1
    }
}
'

And my output:

??? ????????????: root
??????:
???????? ???? ? ???????: 17:28:49 02.10.2024 ?? /dev/ttyp0

UPD: Later I also faced another problem, maybe this will be useful for others: I have pure expect scripts too (they are called from shell by expect -f ). And fconfigure && exporting vars didn't help. Solution: I just saved my .exp files in cp866 encoding - that worked. (It didn't work with the original problem with an expect inside shell script btw)


Solution

  • Your issue is a bit challenging.

    Let's make sure that all is configured for the encoding:

    #!/bin/sh
    
    # env var
    export LANG=ru_RU.CP866
    export LC_CTYPE=ru_RU.CP866
    
    # config terminal
    echo -ne '\033%8' 
    
    expect -c '
    set timeout 5
    
    # config all std'
    fconfigure stdout -encoding cp866
    fconfigure stderr -encoding cp866
    fconfigure stdin -encoding cp866
    
    ...
    

    If all of this not working, check manually if there's a general wider problem, e.g.:
    echo -e "\u041F\u0440\u0438\u0432\u0435\u0442"
    And consider using newer Tcl.