In my company we use expect
to automate tasks, when ssh
'ing into other systems. All our legacy systems run ISO-8859-1 character encoding, while our desktop systems run UTF-8 encoding. Which of course poses certain challenges when ssh
'ing from one of our desktops into our legacy systems.
This is easily solved by removing the SendEnv
line in /etc/ssh/ssh_config
and setting the Gnome terminal character encoding to ISO-8859-1 just before starting the ssh
session. This works fine when doing it manually from the command prompt or from a bash
script. But when doing it from inside an expect
script it fails. It seems like the character encoding either isn't handled at all or is handled incorrectly from within expect
, which results in mangled characters when we enter special characters.
Bare bones bash
script that works:
#!/bin/bash
ssh user@server
Bare bones expect
script that produces the error:
#!/usr/bin/expect --
spawn ssh user@server
interact
The Gnome terminal character encoding has been manually set correctly before executing the two scripts. These two scripts should work identically, by ssh
'ing into the server and letting the user enter the password. But the bash
script handles character encoding correctly, while the expect
script produces mangled special characters.
I assume I'm missing something obvious, but I can't figure out what it is I'm missing.
Edit: We have already tried luit
, which doesn't help. It only results in differently mangled characters.
expect
is written, as you know, in thetcl
language, and you can look here for information on how it handles internationalisation. Internally, tcl converts characters to utf-8, but assumes all input and output uses the system encoding, namely the locale of your terminal.To override this for a particular data stream you can use
fconfigure
on the channel id to specify the encoding to use. The spawn command exposes the created channel by setting the variablespawn_id
. (Beware, there is a common mistake where you sometimes need to declare this variable as global, e.g. in a procedure).For your simple example, you need to tell tcl the encoding of the ssh i/o stream and it should do the necessary conversions for you, without you needing to change the gnome terminal locale in any way.