如何从运行多个命令的shell脚本共享/保持SSH连接
发布时间:2020-12-15 18:32:48 所属栏目:安全 来源:网络整理
导读:我有一个 shell脚本,通过SSH连接到同一个主机(我的服务器),并在整个脚本中运行各种命令.例如: ssh -o ConnectTimeout=3 -o ConnectionAttempts=3 user@my-server "foocommand here"mkdir -p /path/heretouch service.conf...ssh -o ConnectTimeout=3 -o Con
我有一个
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" 等等.问题是每个SSH连接都打开,握手需要一些时间,因为服务器my-server在地理位置上远离正在运行的脚本. 有没有办法加快此过程并阻止为每个必需的命令打开新的SSH连接?像http这样的东西能保持活着吗?
是的,您可以设置ssh以保持与远程服务器的持久连接.这是通过ControlMaster,ControlPath和ControlPersist选项完成的.
示例配置(放在$HOME / .ssh / config中): ControlMaster auto ControlPath ~/.ssh/sockets/%C ControlPersist 600 设置ControlPath可以实现连接共享;当打开与远程主机的ssh连接时,将通过该连接多路复用到同一用户/主机的其他ssh连接. 设置ControlPersist允许连接在最后一个ssh会话退出后的一段时间内保持活动状态. 将ControlMaster设置为auto会允许ssh重用现有连接或在必要时创建新连接. 有关这些选项的详细说明,请参见ssh_config(5)手册页. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |