我在Ubuntu PC的脚本中使用expect连接到一个远程OMAP3板上,该板具有Linux ARM BSP。剧本演得很完美。我们现在正在OMAP4板上迁移,该板具有相似但不同的Linux BSP。当我调用我的脚本时,我在我的Ubuntu控制台中接收到垃圾字符,而当我连接到OMAP3时没有接收到。
脚本如下:

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"

这是连接到OMAP3板时的输出(即无错误):
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]#

下面是连接到OMAP4板时的输出(即出现垃圾字符):
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

脚本运行正常,但我收到了^[[53;22R和;22R等垃圾字符。这可能是我的OMAP4板上的tty设置吗?还有什么可能导致这种情况?谢谢。

最佳答案

最后,我在这里找到了一个可行的解决方案:
http://www.commandlinefu.com/commands/view/6141/remove-color-codes-special-characters-with-sed#comment
所以我换了线

) | expect


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

它正按预期工作。

10-08 02:09