在提示之前期待垃圾

expect garbage before prompt

我尝试使用 ssh 连接到我的路由器,以便自动从中提取一些日志。

我在下面开发了这段代码:

#!/usr/bin/expect -f

spawn ssh root@192.168.1.1
expect "Are you sure you want to"
send -- "yes\r"
expect "password"
send -- "root\r"
expect "\#"
send -- "ls\r"
expect "\#"

问题是我在输出日志中的提示之前预计会有垃圾。

spawn ssh root@192.168.1.1
The authenticity of host '192.168.1.1 (192.168.1.1)' can't be established.
RSA key fingerprint is SHA256:6aeE74qXMeQzg0SGJBZMIa0HFQ5HJrNqE5f3XZ6Irds.
Are you sure you want to continue connecting (yes/no)? yes
Failed to add the host to the list of known hosts (/home/amin/.ssh/known_hosts).

root@192.168.1.1's password: 


BusyBox v1.30.1 () built-in shell (ash)


OpenWrt Version: ALPHA
OpenWrt base: 19.07
------------------------------------
]0;root@openwrt: ~root@openwrt:~# ls
[0;0mnetwork[m
]0;root@openwrt: ~root@openwrt:~# 

造成此问题的主要原因是什么?我该如何解决?

问题是发出终端转义序列,可能控制终端使用的颜色。最简单的解决方法是在执行 spawn. 之前将终端类型(环境变量)设置为不支持颜色 的东西,也许这会成功:

set env(TERM) "dumb"

如果这不起作用(这完全取决于某人 .bashrc 中的内容),那么您可以使用第一个命令覆盖远程端的 PS1 环境变量 登录后

# etc for logging in
expect "# "
send "PS1='# '\r"
expect "# "
# Everything should be right from here on