Search code examples
linuxbasharmexpecttty

Receiving junk charaters while using expect


I was using expect in scripting from an Ubuntu PC to connect on a remote OMAP3 board having a Linux ARM BSP. The script was working flawlessly. We are now migrating on an OMAP4 board having a similar but different Linux BSP. When I call my script, I receive junk character in my Ubuntu console that I do not receive when I connect to the OMAP3.

The script looks like this:

remote_exec()
{
  (
cat <<EOF
    #strace 4
    set timeout -1
    spawn telnet $address
    expect "login:"
    send "$username\r"
    expect "Password:"
    send "$password\r"
EOF
    while [ "$1" ]
    do
        echo 'expect "]# "'
        echo 'send "'$(echo "$1" | sed 's/[$]/\\$/g' | sed 's/["]/\\"/g')'\r"'
        shift
    done
   ) | expect
}

remote_exec \
    "pwd" \
     "echo joie"

Here is the output when I connect to my OMAP3 board (i.e. no error):

test@sts11:~/install/HW400$ ./test-expect.sh 172.19.50.97
spawn telnet 172.19.50.97
Trying 172.19.50.97...
Connected to 172.19.50.97.
Escape character is '^]'.

SBC-97 login: root
Password: 
[root@SBC-97 /root]# pwd
/root
[root@SBC-97 /root]# echo joie
joie
[root@SBC-97 /root]# 

Here is the output when I connect to my OMAP4 board (i.e. junk characters appeared):

test@sts11:~/install/HW400$ ./test-expect.sh 172.19.50.62
spawn telnet 172.19.50.62
Trying 172.19.50.62...
Connected to 172.19.50.62.
Escape character is '^]'.

SBC-62 login: root
[root@SBC-62 /home]# p^[[53;22Rwd
/home
[root@SBC-62 /home]# e^[[53;22Rcho joie
joie
[root@SBC-62 /home]# test@sts11:~/install/HW400$ ;22R;22R;22R

The script runs properly, but I receive junk characters such as ^[[53;22R and ;22R. Is this could be a tty settings on my OMAP4 board? What else could cause this? Thanks.


Solution

  • Finally, I have found a working solution here:

    http://www.commandlinefu.com/commands/view/6141/remove-color-codes-special-characters-with-sed#comment

    So I replace the line

    ) | expect
    

    by

    ) | expect | sed -r "s:\x1B\[[0-9;]*[mK]::g"'
    

    and it is working as expected.