【问题标题】:expect garbage before prompt在提示之前期待垃圾
【发布时间】:2020-03-30 17:06:56
【问题描述】:

我尝试使用 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:~# 

这个问题的主要原因是什么?我该如何解决?

【问题讨论】:

  • 可能是转义的 EOF...
  • 顺便说一句,我会使用 SSH 身份验证而不是存储密码...
  • 如果我仔细观察,这些也可能是 ANSI 颜色代码...
  • @dan1st 我怎样才能删除它们?

标签: tcl expect garbage


【解决方案1】:

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

set env(TERM) "dumb"

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

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

【讨论】:

  • 我添加了这一行:set env(TERM) "dumb" 并且效果很好。谢谢。
猜你喜欢
  • 1970-01-01
  • 2012-01-19
  • 1970-01-01
  • 2014-06-25
  • 1970-01-01
  • 2019-03-23
  • 1970-01-01
  • 2013-07-10
  • 2016-07-08
相关资源
最近更新 更多