我有一个 shell 脚本,它通过 SSH 连接到同一主机(我的服务器)并运行在整个脚本中混合的各种命令。例如:
ssh -o ConnectTimeout=3 -o ConnectionAttempts=3 user@my-server "foocommand here"
mkdir -p /path/here
touch service.conf
...
ssh -o ConnectTimeout=3 -o ConnectionAttempts=3 user@my-server "barcommand here"
mkdir -p /other/path/here
touch /other/path/here/service.conf
...
ssh -o ConnectTimeout=3 -o ConnectionAttempts=3 user@my-server "darcommand here"
Run Code Online (Sandbox Code Playgroud)
等等。问题是每个 SSH 连接都打开并且握手需要一些时间,因为服务器my-server在地理上远离正在运行的脚本。
有没有办法加快这个过程并防止为每个必需的命令打开一个新的 SSH 连接?像 http 之类的东西可以让 SSH 保持活动状态吗?
是的,您可以设置 ssh 以保持与远程服务器的持久连接。这是通过ControlMaster,ControlPath和ControlPersist选项完成的。
示例配置(放置在 中$HOME/.ssh/config):
ControlMaster auto
ControlPath ~/.ssh/sockets/%C
ControlPersist 600
Run Code Online (Sandbox Code Playgroud)
设置ControlPath启用连接共享;当到远程主机的 ssh 连接打开时,到同一用户/主机的其他 ssh 连接将通过该连接多路复用。
设置ControlPersist允许连接在最后一个 ssh 会话退出后保持活动一段时间。
设置ControlMaster为auto允许 ssh 重用现有连接或在必要时创建新连接。
有关ssh_config(5)这些选项的详细说明,请参阅手册页。
| 归档时间: |
|
| 查看次数: |
1584 次 |
| 最近记录: |