在 Shell 脚本中设置和使用 SSH ControlMaster 会话 [英] Setup and use SSH ControlMaster Session in a Shell Script

查看:56
本文介绍了在 Shell 脚本中设置和使用 SSH ControlMaster 会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个脚本,其中包含需要在远程服务器上运行的几组命令,并在其间处理结果.目前这是通过为每组命令运行 ssh 来实现的,但是这需要每次建立新连接并进行身份验证,这很慢.

I'm writing a script which has several sets of commands that it needs to run on a remote server, with processing of results in between. Currently this is achieved by running ssh for each set of commands, however this requires a new connection to be made and authenticated each time, which is slow.

我最近阅读了 SSH 中的 ControlMaster 选项,这似乎正是我所需要的,即通过单个 SSH 连接运行单独 SSH 会话的能力.

I recently read about the ControlMaster option in SSH, which seems like exactly what I need, namely the ability to run separate SSH sessions through a single SSH connection.

然而,我非常不清楚的是我将如何在我的 shell 脚本中实现这一点.例如,我正在考虑这样构建它:

However, what I'm extremely unclear on is how exactly I would achieve this in my shell script. For example, I was thinking of constructing it like so:

#!/bin/sh
HOST="$1"

# Make sure we clean up after ourselves
on_complete() {
    kill $ssh_control_master_id
    rm -r "$tmp_dir"
}
trap 'on_complete 2> /dev/null' SIGINT SIGHUP SIGTERM EXIT

tmp_dir=$(mktemp -d "/tmp/$(basename "$0").XXXXXX")
ssh_control_socket="$tmp_dir/ssh_control_socket"

# Setup control master
ssh -o 'ControlMaster=yes' -S "$ssh_control_socket" "$HOST" &
ssh_control_master_id=$!

# Run initial commands
data=$(ssh -S "$ssh_control_socket" "$HOST" 'echo "Foo"')

# Process the data
echo "$data"

# Run some more commands
data=$(ssh -S "$ssh_control_socket" "$HOST" 'echo "Bar"')

# Process the second batch of data
echo "$data"

只是一个简单的例子来给你一个想法,但这似乎不是正确的方法,因为运行它会导致第二个 ssh 命令挂起,或者每个命令将正常运行(创建自己的连接).我也不知道如何等待主连接建立,即 - 我可能正在建立远程连接时运行我的实际命令.

Just a simple example to give you an idea, but this doesn't seem to be the correct way to do this, as running it will either cause the second ssh command to hang, or each command will just run normally (create their own connection). I'm also not sure how to go about waiting for the master connection to be established, i.e - I'm probably running my actual commands while the remote connection is still being established.

另外在相关说明中,一旦控制主机运行,关闭它的正确方法是什么,是否可以杀死它和/或删除它的套接字?

Also on a related note, what is the correct way to close the control master once it's running, is killing it and/or deleting its socket fine?

推荐答案

您的代码看起来不错.我还没有测试过它,但第一个尝试使用主连接的进程可能应该阻塞,直到主连接实际成功建立.您可以使用 -N 选项来避免在主连接上运行虚假 shell:

Your code looks fine. I haven't tested it, but the first process that tries to use the master connection should probably block until the master connection has actually successfully been established. You can use the -N option to avoid running a spurious shell on the master connection:

ssh -N -o 'ControlMaster=yes' -S "$ssh_control_socket" "$HOST" &

在所有从属会话完成后简单地终止 ssh 进程是完全没问题的.

It's perfectly fine to simply kill the ssh process once all the subordinate sessions have completed.

这篇关于在 Shell 脚本中设置和使用 SSH ControlMaster 会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆