aeruder / expect

expect (tcl)
Other
23 stars 5 forks source link

Error transporting binary data over ssh/telnet (even shell) in expect script #1

Open fangxlmr opened 3 years ago

fangxlmr commented 3 years ago

It seems like no one is maintaining this repo any more, but I still wanna post the issue here since it confuse me a lot and closely releated to expect command. Assuming I'd like to fetch data from remote with user/password auth automating in expect script, like the following: ps: I use sh to refer ssh/telnet connection

$ expect -v
expect vesion 5.45.4
$ cat expect.sh
#!/usr/bin/expect -f

spawn -noecho sh -c ./download.sh
interact
$ cat download.sh
#!/bin/sh

sh -c 'cat /bin/ls' # cat a binary file
$ ./expect.sh > a.out
$ file a.out
a.out: ELF 64-bit LSB shared object, x86-64, version1 (SYSV), too many program (2304)

As the last command said, captured command ls (i.e. a.out) has broken locally. And you can find if you actually run the script, the two files are different in bytesize (let alone checksum).

I though it might be related to traslation and encoding issue, so I change the expect.sh script to this and it still not working

$ cat expect.sh
#!/usr/bin/expect -f

spawn -noecho sh -c ./download.sh
fconfigure $spawn_id -translation binary -encoding binary
fconfigure stdin -translation binary -encoding binary
fconfigure stdout -translation binary -encoding binary
interact

Any body has any idea?

clarkwang commented 3 years ago

xref: https://stackoverflow.com/questions/65436528/

douniwan5788 commented 3 years ago
  1. Use stty raw to disable all translations
  2. export LC_ALL=en_US.ISO-8859-1 to avoid encoding problems with expect
fangxlmr commented 3 years ago

xref: https://stackoverflow.com/questions/65436528/

@clarkwang Thanks. Indeed, the question on Stackoverflow is posted by me as well.

fangxlmr commented 3 years ago

@douniwan5788 Yes! That does work! Thanks!

One more question: Since I wanna keep the 'newline' symbol as whatever it is, how to avoid \r\n translation?

This might happen when cat a binary data. Binary data might contain byte 0a (i.e. \n) so that expect converts it to 0d0a (i.e. \r\n) resulting in broken binary as well.

fangxlmr commented 3 years ago
spawn "telnet xxx"
fconfigure $spawn_id -translation crlf

focnfigure command seems solve the translation issue.