如何解决使用“expect”时远程终端屏幕混乱的问题?(通常在调整大小之后)

use*_*107 2 shell ssh remote expect

使用expectssh 和密码自动登录远程,调整窗口大小时,stty size报告旧大小。vim导致诸如和 之类的命令less变得混乱。

use*_*107 7

经过长时间的搜索和测试,终于找到了expect问题的原因,默认情况下expect不会转发WINCH信号,这可以通过trap命令修复,如下所示

trap {
 #fetch rows and cols from controlling terminal
 #note  [] is tcl way of call and here the stty is expect's not system's which not support "stty rows" to query rows
 set rows [stty rows]
 set cols [stty columns]
 #send "echo size changed to $rows $cols\r"
 #according to the man page, the variable spawn_out(slave,name) is set to the name of the pty slave device
 stty rows $rows columns $cols < $spawn_out(slave,name)
} WINCH
Run Code Online (Sandbox Code Playgroud)

在我的预期文件的开头添加此内容后,一切正常。

感谢来自https://askubuntu.com/a/672919/1384831的 Anish Sneh

想回答如何解决终端屏幕混乱的问题?(通常在调整大小之后)但它受到保护,所以发布一个新问题。希望它能节省其他人的时间。

  • 很多人(至少我)没有意识到这是expect的问题,这个问题是如此热门,并且是谷歌“远程shell调整大小问题”的第一个搜索结果。我已经尝试了那里的所有答案,但没有任何运气,几乎放弃了。最近,在某些情况下,我直接使用`ssh`而不使用`expect`,最终意识到这是expect的问题。 (2认同)