Linux 的世界有着许多的未知宝藏,今天要说的就是其中一个。
看下面的脚本:
#!/usr/bin/expect -f
set user <username>
set host <REMOTE_IP>
set password <PASSWORD>
set timeout -1
spawn ssh $user@$host
expect "*assword:*"
send "$password\r"
interact
expect eof
遇到一个小问题,之前直接使用 terminal 登录 ssh,使用 tmux 的时候,是可以随着窗口的缩放而适应大小的。改用 expect 自动登录 ssh之后,tmux 的适应窗口大小的能力消失了。在这里找到一个问答,解决这个问题。简而言之,expect 自己来捕获 sigwinch 信号,并传递给 children。
修改之后的脚本:
#!/usr/bin/expect -f
# 捕获 WINCH 信号,并传递给 Spawned 出来的 Children,
# 这样 ssh 的 TTY 就能随着窗口 resize 而适应。
trap {
set rows [stty rows]
set cols [stty columns]
stty rows $rows columns $cols < $spawn_out(slave,name)
} WINCH
set user <username>
set host <REMOTE_IP>
set password <PASSWORD>
set timeout -1
spawn ssh $user@$host
expect "*assword:*"
send "$password\r"
interact
expect eof