如何从远程 SSH 会话将数据发送到本地剪贴板
大多数类 Unix 系统都有一个命令,可以让您通过管道/重定向输出到本地剪贴板/粘贴板,并从中检索。 在 OS X 上,这些命令是
pbcopy, pbpaste
SSHed 到另一台服务器? 也就是说,
- 我正在使用计算机 A。
- 我打开一个终端窗口 通过
- SSH 连接到计算机 B
- 我在计算机 B 上运行命令
- 计算机 B 的输出被重定向或自动复制到计算机 A 的剪贴板。
是的,我知道我可以(不寒而栗)使用鼠标从命令中选择文本,但我已经习惯了将输出直接传送到剪贴板的工作流程,我希望远程会话也能如此。
代码很有用,但通用方法也值得赞赏。
Most Unix-like systems have a command that will let you pipe/redirect output to the local clipboard/pasteboard, and retrieve from same. On OS X, these commands are
pbcopy, pbpaste
Is there a way to replicate this functionality while SSHed into another server? That is,
- I'm using Computer A.
- I open a terminal window
- I SSH to Computer B
- I run a command on Computer B
- The output of Computer B is redirected or automatically copied to Computer A's clipboard.
And yes, I know I could just (shudder) use my mouse to select the text from the command, but I've gotten so used to the workflow of pipping output directly to the clipboard that I want the same for my remote sessions.
Code is useful, but general approaches are appreciated as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
这是我的解决方案,基于 SSH 反向隧道、netcat 和 xclip。
首先在您的工作站上创建一个脚本(例如,clipboard-daemon.sh):
并在后台启动它。
它将启动
nc
将输出传输到 xclip 并在之后重新生成进程接收部分数据。然后启动与远程主机的 SSH 连接:
登录远程主机后,尝试以下操作:
然后尝试粘贴到您的工作站上。
当然,您可以编写一个包装器脚本,首先启动 clipboard-daemon.sh,然后启动 SSH 会话。 这对我来说就是这样的。
This is my solution based on an SSH reverse tunnel, netcat, and xclip.
First create a script (for example, clipboard-daemon.sh) on your workstation:
And start it in the background.
It will start
nc
piping output to xclip and respawning the process after receiving a portion of data.Then start the SSH connection to the remote host:
While logged in on the remote box, try this:
Then try to paste on your workstation.
You can of course write a wrapper script that starts clipboard-daemon.sh first and then the SSH session. This is how it works for me.
请允许我添加一个解决方案,如果我没有记错的话,之前没有建议过。
它不需要客户端暴露在互联网上(无反向连接),也不使用服务器上的任何xlib,并且完全使用ssh自己的功能实现(无第3方bins)
:
该解决方案利用 ssh 的 ControlMaster 功能,只需使用一个 TCP 连接即可完成所有操作,因此它甚至可以支持需要密码登录的主机,并提示您输入密码一次。
编辑:根据要求,代码本身:
将以下内容粘贴到您的 bashrc 中并使用
sshx host
进行连接。在远程机器上
echo SOMETHING > ~/clip
希望某些内容最终会出现在本地主机的剪贴板中。您将需要本地主机上的
xclip
实用程序。更详细的说明位于我的网站上:
https://xicod.com/2021/02/09/clipboard-over-ssh.html
Allow me to add a solution that if I'm not mistaken was not suggested before.
It does not require the client to be exposed to the internet (no reverse connections), nor does it use any xlibs on the server and is implemented completely using ssh's own capabilities (no 3rd party bins)
It involves:
The solution utilizes ssh's ControlMaster functionality to use just one TCP connection for everything so it will even support hosts that require a password to login and prompt you for it just once.
Edit: as requested, the code itself:
Paste the following into your bashrc and use
sshx host
to connect.On the remote machine
echo SOMETHING > ~/clip
and hopefully, SOMETHING will end up in the local host's clipboard.You will need the
xclip
utility on your local host.More detailed explanation is on my website:
https://xicod.com/2021/02/09/clipboard-over-ssh.html
最简单的解决方案是,如果您在 OS X 上使用终端,并且一直在远程服务器上进行 ssh 操作,并且希望获取文本文件、日志或 csv 的结果,只需:
1) < code>Cmd-K 清除终端的输出
2)
cat
显示文件内容3)
Cmd-S
到保存终端输出您将需要手动删除文件的第一行和最后一行,但这种方法比依赖要安装的其他软件包、“反向隧道”和尝试拥有静态 IP 等要简单一些。
The simplest solution of all, if you're on OS X using Terminal and you've been ssh'ing around in a remote server and wish to grab the results of a text file or a log or a csv, simply:
1)
Cmd-K
to clear the output of the terminal2)
cat <filename>
to display the contents of the file3)
Cmd-S
to save the Terminal OutputYou'll have the manually remove the first line and last line of the file, but this method is a bit simpler than relying on other packages to be installed, "reverse tunnels" and trying to have a static IP, etc.
它不是单行代码,但它不需要任何额外的 SSH 连接。
netcat
cat ~/some_file.txt | nc termbin.com 9999
。 这会将输出复制到termbin
网站,并将 URL 打印到输出中。当然,请勿将其用于敏感内容。
It is not a one-liner, but it doesn't require any extra SSH connection.
netcat
if necessarycat ~/some_file.txt | nc termbin.com 9999
. This will copy the output to thetermbin
website and prints the URL to your output.Of course, do not use it for sensitive content.
该答案通过增加更多安全性,在所选答案的基础上发展。
该答案讨论了一般形式,
可能缺乏安全性的地方是
ssh
权限允许
在host B>
上 < code>ssh 进入主机 A
并执行任何命令。当然,
B
到A
的访问可能已经由ssh
密钥控制,甚至可能有密码。 但另一层安全性可以限制B
可以在A
上执行的允许命令的范围,例如,使得rm -rf /
无法被调用。 (当ssh
密钥没有有密码时,这一点尤其重要。)幸运的是,
ssh
有一个内置功能,称为命令限制或强制命令。 请参阅 ssh.com,或此服务器故障问题。
下面的解决方案显示了通用表单解决方案以及强制执行的
ssh
命令限制。添加了命令限制的示例解决方案
此安全性增强的解决方案遵循一般形式 - 来自
host-B
上的ssh
会话的调用很简单:其余部分显示了使其正常工作的设置。
设置ssh命令限制
假设
B
上的用户帐户是user-B
,并且B有一个ssh密钥id-clip< /code>,已以通常方式创建 (
ssh-keygen
)。然后,在
user-A
的ssh目录中,有一个文件可以识别密钥
id-clip
并允许ssh
连接。通常每一行
authorized_keys
的内容正是被授权的公钥,例如id-clip.pub
的内容。但是,为了强制执行命令限制,公钥内容需要在要执行的命令前面(在同一行上)。
在我们的例子中:
指定的命令
“/home/user-A/.ssh/allowed-commands.sh id-clip”
,并且仅指定的命令被执行每当使用密钥id-clip
时,都会启动与host-A
的ssh
连接 - 无论写入什么命令,ssh
命令行。该命令指示一个脚本文件
allowed-commands.sh
,该脚本文件的内容是机器
B
上对ssh
的原始调用was字符串
to-clipboard
通过环境变量SSH_ORIGINAL_COMMAND
传递给allowed-commands.sh
。此外,我们还从
authorized_keys
中的行传递了密钥名称id-clip
,该密钥只能由id-clip
访问。该行
只是一个弹出消息框,让您知道剪贴板正在写入 - 这可能也是一个很好的安全功能。 (
notify-send
适用于 Ubuntu 18.04,也许不适用于其他版本)。在行中
参数
--display :0
是必要的,因为该进程没有自己的带剪贴板的 X 显示,所以必须明确指定。 此值
:0
恰好适用于 Ubuntu ;18.04 使用 Wayland 窗口服务器。 在其他设置上它可能不起作用。 对于标准 X 服务器这个答案可能会有所帮助。host-A
/etc/ssh/sshd_config
参数最后是主机
A
/etc/ssh/sshd_config 中的一些参数code> 应设置为确保连接权限,以及仅在没有密码的情况下使用ssh
密钥的权限:要使
sshd
服务器重新读取配置或
结论
,需要付出一些努力来设置它,但是除了
to-clipboard
之外的其他功能可以与同一框架并行构建。This answer develops both upon the chosen answer by adding more security.
That answer discussed the general form,
Where security may be lacking is in the
ssh
permissions allowing<user B>
onhost B>
tossh
intohost A
and execute any command.Of course
B
toA
access may already be gated by anssh
key, and it may even have a password. But another layer of security can restrict the scope of allowable commands thatB
can execute onA
, e.g. so thatrm -rf /
cannot be called. (This is especially important when thessh
key doesn't have a password.)Fortunately,
ssh
has a built-in feature called command restriction or forced command. See ssh.com, orthis Server Fault question.
The solution below shows the general form solution along with
ssh
command restriction enforced.Example solution with command restriction added
This security enhanced solution follows the general form - the call from the
ssh
session onhost-B
is simply:The rest of this shows the setup to get that to work.
Setup of ssh command restriction
Suppose the user account on
B
isuser-B
, and B has an ssh keyid-clip
, that has been created in the usual way (ssh-keygen
).Then in
user-A
's ssh directory there is a filethat recognizes the key
id-clip
and allowsssh
connection.Usually the contents of each line
authorized_keys
is exactly the public key being authorized, e.g., the contents ofid-clip.pub
.However, to enforce command restriction that public key content is prepended (on the same line) by the command to be executed.
In our case:
The designated command
"/home/user-A/.ssh/allowed-commands.sh id-clip"
, and only that designated command, is executed whenever keyid-clip
is used initiate anssh
connection tohost-A
- no matter what command is written thessh
command line.The command indicates a script file
allowed-commands.sh
, and the contents of that that script file isThe original call to
ssh
on machineB
wasThe string
to-clipboard
is passed toallowed-commands.sh
by the environment variableSSH_ORIGINAL_COMMAND
.Addition, we have passed the name of the key,
id-clip
, from the line inauthorized_keys
which is only accessed byid-clip
.The line
is just a popup messagebox to let you know the clipboard is being written - that's probably a good security feature too. (
notify-send
works on Ubuntu 18.04, and maybe not others).In the line
The parameter
--display :0
is necessary, because the process doesn't have its own X display with a clipboard,so it must be specified explicitly. This value
:0
happens to work on Ubuntu 18.04 with the Wayland window server. On other setups it might not work. For a standard X server this answer might help.host-A
/etc/ssh/sshd_config
parametersFinally a few parameters in
/etc/ssh/sshd_config
on hostA
that should be set to ensure permission to connect, and permission to use anssh
-key only without a password:To make the
sshd
server reread the configurationor
Conclusion
It's some effort to set it up, but other functions besides
to-clipboard
can be constructed in parallel with the same framework.对于任何通过谷歌搜索找到此内容的人:
如今最好的解决方案似乎是 lemonade
neovim 中也提到了各种解决方案剪贴板工具的帮助文本
For anyone googling their way to this:
The best solution in this day and age seem to be lemonade
Various solutions is also mentioned in the neovim help text for clipboard-tool
rhileighalmgren的解决方案 很好,但是 pbcopy 会烦人地复制最后一个“\n”字符。 我使用 head 删除最后一个字符以防止出现这种情况:
我的完整解决方案是这里: http://taylor.woodstitch.com/linux /copy-local-clipboard-remote-ssh-server/
rhileighalmgren's solution is good, but pbcopy will annoyingly copy the last "\n" character. I use head to strip out the last character to prevent this:
My full solution is here: http://taylor.woodstitch.com/linux/copy-local-clipboard-remote-ssh-server/
Far Manager Linux 端口 支持在本地和远程主机之间同步剪贴板。 您只需打开本地far2l,在里面执行“ssh somehost”,在该ssh会话中运行远程far2l并让远程far2l与本地剪贴板一起使用。
它支持Linux、*BSD和OS X; 我制作了一个特殊的 putty 构建来从 Windows 中利用此功能。
Far Manager Linux port supports synchronizing clipboard between local and remote host. You just open local far2l, do "ssh somehost" inside, run remote far2l in that ssh session and get remote far2l working with your local clipboard.
It supports Linux, *BSD and OS X; I made a special putty build to utilize this functionality from windows also.
如果您正在 Kubernetes 集群中的 pod 上工作,而不是直接 SSH,那么您无法进行文件传输,您可以使用
cat
,然后将终端输出保存为文本。 例如,在 macOS 中,您可以执行 Shell -> 导出为文本。If you're working over e.g. a pod in a Kubernetes cluster and not direct SSH, so that there is no way for your to do a file transfer, you could use
cat
and then save the terminal output as text. For example in macOS you can do Shell -> Export as text.与 Sridhar 的答案类似,但使用本地转发。
这是因为远程转发会在远程计算机上留下绑定和打开的端口,我认为这是不好的做法。
对我来说,其逻辑也是倒退的,远程应该提供内容,本地则请求内容。
输入
~
Shift + C
-L127.0.0.4:7777:127.0.0.8:7777
Enter
或者你可以从编辑器写入它,例如在 Vim 中:
<前><代码>:w !nc -lN 127.0.0.8 7777
xclip
或pbcopy
:<预><代码>nc 127.0.0.4 7777 < /dev/null | xclip-sel c
或者只是将输出重定向到文件。
.4
&.8
地址并不特殊,可以更改。它们有助于区分每个的使用位置。
Similar to Sridhar's answer but uses a local forward.
This is because remote forwards leave a port bound and open on the remote machine, which I think is bad practice.
To me, the logic of that is also backwards, it's the remote that should be serving the content, and the local requesting it.
Enter
~
Shift + C
-L127.0.0.4:7777:127.0.0.8:7777
Enter
or you can write to it from an editor, e.g. in Vim:
xclip
orpbcopy
:or just redirect output to a file.
The
.4
&.8
addresses are not special and can be changed.They help differentiate where each is used.
我最喜欢的方式是 ssh [remote-machine] "cat log.txt" | xclip -选择c。 当您不想(或不能)从远程 ssh 到本地时,这是最有用的。
在 Cygwin 上,
ssh [remote-machine] "cat log.txt" > /dev/剪贴板
。来自 nbren12 的有用评论:
My favorite way is
ssh [remote-machine] "cat log.txt" | xclip -selection c
. This is most useful when you don't want to (or can't) ssh from remote to local.On Cygwin,
ssh [remote-machine] "cat log.txt" > /dev/clipboard
.A helpful comment from nbren12:
我复活这个线程是因为我一直在寻找同样的解决方案,并且我找到了一个适合我的解决方案。 这是对 OSXDaily。
就我而言,我使用本地 OS X 计算机上的终端通过 SSH 连接到 Linux 服务器。 与 OP 一样,我希望能够仅使用键盘将少量文本从终端传输到本地剪贴板。
解决方案的本质:
当在远程计算机的 SSH 会话中运行时,此命令获取 commandThatMakesOutput 的输出(例如,ls, pwd >)并将输出通过管道传输到本地计算机的剪贴板(“桌面”的名称或IP)。 换句话说,它使用嵌套 SSH:您通过一个 SSH 会话连接到远程计算机,在那里执行命令,远程计算机通过另一个 SSH 会话连接到您的桌面,并将文本放入剪贴板。
它需要将您的桌面配置为 SSH 服务器(我将其留给您和 Google)。 如果您设置了 ssh 密钥以促进快速 ssh 使用,最好使用每个会话密码,或者您的安全需求所需的任何内容,那就容易得多。
其他示例:
为了方便起见,我创建了一个 Bash 文件来缩短管道后所需的文本:
在我的例子中,我使用了一个专门命名的键。
我使用文件名 cb(我的助记符 (ClipBoard))保存它。将脚本放在路径中的某个位置,使其可执行,然后瞧:
I'm resurrecting this thread because I've been looking for the same kind of solution, and I've found one that works for me. It's a minor modification to a suggestion from OSXDaily.
In my case, I use Terminal on my local OS X machine to connect to a Linux server via SSH. Like the OP, I wanted to be able to transfer small bits of text from terminal to my local clipboard, using only the keyboard.
The essence of the solution:
When run in an SSH session to a remote computer, this command takes the output of commandThatMakesOutput (e.g., ls, pwd) and pipes the output to the clipboard of the local computer (the name or IP of "desktop"). In other words, it uses nested SSH: you're connected to the remote computer via one SSH session, you execute the command there, and the remote computer connects to your desktop via a different SSH session and puts the text to your clipboard.
It requires your desktop to be configured as an SSH server (which I leave to you and Google). It's much easier if you've set up ssh keys to facilitate fast ssh usage, preferably using a per-session passphrase, or whatever your security needs require.
Other examples:
For convenience, I've created a Bash file to shorten the text required after the pipe:
In my case, I'm using a specially-named key.
I saved it with the file name cb (my mnemonic (ClipBoard). Put the script somewhere in your path, make it executable and voilà:
我找到了一个很好的解决方案,不需要反向 SSH 连接!
您可以在远程主机上使用 xclip,以及在远程主机上使用 SSH X11 转发和 XQuartz OS X 系统。
要进行此设置:
,
)ssh -X 远程主机 "echo '来自远程主机的问候' | xclip -selection剪贴板”
I found a great solution that doesn't require a reverse SSH connection!
You can use xclip on the remote host, along with SSH X11 forwarding and XQuartz on the OS X system.
To set this up:
,
)ssh -X remote-host "echo 'hello from remote-host' | xclip -selection clipboard"
ssh 服务器上的反向隧道端口
所有现有的解决方案要么需要:
xclip
效果很好),或者这是另一种方法,但您需要修改 ssh 进入计算机的方式。
我已经开始使用这个,它远没有看起来那么令人生畏,所以尝试一下。
客户端(ssh 会话启动)
(提示:将其设置为键绑定,这样您就不必键入它)
客户端(另一个选项卡)
注意:如果您没有
pbcopy
,则只需tee
将其保存到文件中。服务器(在 SSH 会话内)
其他说明
实际上,即使您处于 ssh 会话中间,也有一种方法可以启动隧道,但我不想吓跑人们,让他们远离实际上并不像看起来那么糟糕的情况。 但如果我看到有人感兴趣,我会在稍后添加详细信息
Reverse tunnel port on ssh server
All the existing solutions either need:
xclip
on the server works great) orHere's another way to do it, though you'll need to modify how you ssh into your computer.
I've started using this and it's nowhere near as intimidating as it looks so give it a try.
Client (ssh session startup)
(hint: make this a keybinding so you don't have to type it)
Client (another tab)
Note: if you don't have
pbcopy
then justtee
it to a file.Server (inside SSH session)
Other notes
Actually even if you're in the middle of an ssh session there's a way to start a tunnel but i don’t want to scare people away from what really isn’t as bad as it looks. But I'll add the details later if I see any interest
如果您在 Mac 上使用 iTerm2,还有一种更简单的方法。 此功能通过
it2copy
Shell 集成 功能中> 命令:要使其工作,
在登录远程主机时选择iTerm2-->Install Shell Integration菜单项(以便它运行
curl -L https:// iterm2.com/shell_integration/install_shell_integration_and_utilities.sh | bash
),将其安装到您自己的帐户。按照说明刷新您的环境(例如
source ~/.iterm2_shell_integration.zsh
)。完成后,您将可以访问
~/.iterm2/it2copy
以及许多其他提供出色功能的别名命令。这里的其他解决方案都是很好的解决方法,但相比之下,这个解决方案非常轻松。
If you use iTerm2 on the Mac, there is an easier way. This functionality is built into iTerm2's Shell Integration capabilities via the
it2copy
command:To make it work,
choose iTerm2-->Install Shell Integration menu item while logged into the remote host (so that it runs
curl -L https://iterm2.com/shell_integration/install_shell_integration_and_utilities.sh | bash
), to install it to your own account.Follow the instructions to refresh your environment (e.g.
source ~/.iterm2_shell_integration.zsh
).Once that is done, you'll have access to
~/.iterm2/it2copy
, as well as a bunch of other aliased commands that provide cool functionality.The other solutions here are good workarounds but this one is so painless in comparison.
有多种工具可以访问 X11 选择,包括 xclip 和 XSel。 请注意,X11 传统上具有多重选择,并且大多数程序对剪贴板和主选择都有一定的了解(它们并不相同)。 Emacs 也可以使用辅助选择,但这种情况很少见,而且没有人真正知道如何处理剪切缓冲区...
简而言之,您应该尝试
xclip -i
/xclip -o< /code> 或
xclip -i -sel 剪辑
/xclip -o -sel 剪辑
或xsel -i
/xsel -o< /code> 或
xsel -i -b
/xsel -o -b
,具体取决于您的需求。There are various tools to access X11 selections, including xclip and XSel. Note that X11 traditionally has multiple selections, and most programs have some understanding of both the clipboard and primary selection (which are not the same). Emacs can work with the secondary selection too, but that's rare, and nobody really knows what to do with cut buffers...
In short, you should try
xclip -i
/xclip -o
orxclip -i -sel clip
/xclip -o -sel clip
orxsel -i
/xsel -o
orxsel -i -b
/xsel -o -b
, depending on what you want.