边界服务器故障问题,但我正在编程一些shell脚本,所以我先在这里尝试:)

大多数*nixes都有一个命令,可以让您将输出管道/重定向到本地剪贴板/粘贴板,并从中检索。在OS X上,这些命令是

pbcopy, pbpaste 

当SSHed到另一个服务器时,是否有办法复制此功能?也就是说,

我用的是电脑A。 我打开一个终端窗口 我SSH到计算机B 我在计算机B上运行一个命令 计算机B的输出被重定向或自动复制到计算机A的剪贴板。

是的,我知道我可以(战栗)用鼠标从命令中选择文本,但我已经习惯了直接将输出输出到剪贴板的工作流程,所以我希望远程会话也能这样。

代码很有用,但通用方法也很有用。


有各种工具可以访问X11选择,包括xclip和XSel。注意,X11传统上有多个选择,大多数程序对剪贴板和主选择都有一定的理解(这是不一样的)。Emacs也可以使用二级选择,但这是罕见的,没有人真正知道如何处理剪切缓冲区…

$ xclip -help
Usage: xclip [OPTION] [FILE]...
Access an X server selection for reading or writing.

  -i, -in          read text into X selection from standard input or files
                   (default)
  -o, -out         prints the selection to standard out (generally for
                   piping to a file or program)
  -l, -loops       number of selection requests to wait for before exiting
  -d, -display     X display to connect to (eg localhost:0")
  -h, -help        usage information
      -selection   selection to access ("primary", "secondary", "clipboard" or "buffer-cut")
      -noutf8      don't treat text as utf-8, use old unicode
      -version     version information
      -silent      errors only, run in background (default)
      -quiet       run in foreground, show what's happening
      -verbose     running commentary

Report bugs to <astrand@lysator.liu.se>
$ xsel -help
Usage: xsel [options]
Manipulate the X selection.

By default the current selection is output and not modified if both
standard input and standard output are terminals (ttys).  Otherwise,
the current selection is output if standard output is not a terminal
(tty), and the selection is set from standard input if standard input
is not a terminal (tty). If any input or output options are given then
the program behaves only in the requested mode.

If both input and output is required then the previous selection is
output before being replaced by the contents of standard input.

Input options
  -a, --append          Append standard input to the selection
  -f, --follow          Append to selection as standard input grows
  -i, --input           Read standard input into the selection

Output options
  -o, --output          Write the selection to standard output

Action options
  -c, --clear           Clear the selection
  -d, --delete          Request that the selection be cleared and that
                        the application owning it delete its contents

Selection options
  -p, --primary         Operate on the PRIMARY selection (default)
  -s, --secondary       Operate on the SECONDARY selection
  -b, --clipboard       Operate on the CLIPBOARD selection

  -k, --keep            Do not modify the selections, but make the PRIMARY
                        and SECONDARY selections persist even after the
                        programs they were selected in exit.
  -x, --exchange        Exchange the PRIMARY and SECONDARY selections

X options
  --display displayname
                        Specify the connection to the X server
  -t ms, --selectionTimeout ms
                        Specify the timeout in milliseconds within which the
                        selection must be retrieved. A value of 0 (zero)
                        specifies no timeout (default)

Miscellaneous options
  -l, --logfile         Specify file to log errors to when detached.
  -n, --nodetach        Do not detach from the controlling terminal. Without
                        this option, xsel will fork to become a background
                        process in input, exchange and keep modes.

  -h, --help            Display this help and exit
  -v, --verbose         Print informative messages
  --version             Output version information and exit

Please report bugs to <conrad@vergenet.net>.

简而言之,您应该尝试xclip -i/xclip -o或xclip -i -sel clip/xclip -o -sel clip或xsel -i/xsel -o或xsel -i -b/xsel -o -b,这取决于您想要什么。


我重新启用这个帖子是因为我一直在寻找同样的解决方案,我已经找到了一个适合我的解决方案。这是对OSX日报建议的一个小修改。

在我的例子中,我使用本地OSX机器上的Terminal通过SSH连接到linux服务器。像OP一样,我希望能够仅使用键盘将小块文本从终端传输到本地剪贴板。

解决方案的精髓:

commandThatMakesOutput | ssh desktop pbcopy

当在远程计算机的ssh会话中运行时,该命令接受commandThatMakesOutput的输出(例如ls, pwd),并将输出管道输出到本地计算机的剪贴板(“desktop”的名称或IP)。换句话说,它使用嵌套ssh:您通过一个ssh会话连接到远程计算机,在那里执行命令,远程计算机通过另一个ssh会话连接到您的桌面,并将文本放入剪贴板。

它要求将您的桌面配置为ssh服务器(我把它留给您和谷歌)。如果您已经设置了ssh密钥以方便快速使用ssh,那么就会容易得多,最好使用每个会话的密码短语,或任何您的安全需求。

其他的例子:

ls  | ssh desktopIpAddress pbcopy
pwd |  ssh desktopIpAddress pbcopy

为了方便起见,我创建了一个bash文件来缩短管道后所需的文本:

#!/bin/bash
ssh desktop pbcopy

在我的情况下,我使用一个特殊命名的密钥

我用文件名cb(我的助记符(剪贴板)保存它。把脚本放在你路径的某个地方,让它可执行,瞧:

ls | cb

发现了一个很好的解决方案,不需要反向ssh连接!

您可以在远程主机上使用xclip,同时在OSX系统上使用ssh X11转发和XQuartz。

设置方法:

安装XQuartz(我用solist + pivotal_workstation:: XQuartz recipe做了这个,但你不必这样做) 运行XQuartz.app 打开XQuartz首选项(+,) 确保选中“启用同步”和“当剪贴板发生变化时更新剪贴板” ssh -X remote-host "echo 'hello from remote-host' | xclip -select剪贴板"


我最喜欢的方法是ssh [remote-machine] "cat log.txt" | xclip -selection c.当你不想(或不能)从远程ssh到本地时,这是最有用的。

编辑:在Cygwin ssh [remote-machine] "cat log.txt" > /dev/clipboard. txt

编辑:来自nbren12的有用评论:

几乎总是可以使用ssh端口转发建立反向ssh连接。只需将RemoteForward 127.0.0.1:2222 127.0.0.1:22添加到本地.ssh/config中的服务器条目中,然后在远程计算机上执行ssh -p 2222 127.0.0.1,然后将连接重定向到本地计算机。——nbren12


@rhileighalmgren解决方案很好,但pbcopy会烦人地复制最后一个“\n”字符,我使用“head”剥离最后一个字符来防止这种情况:

#!/bin/bash
head -c -1 |  ssh desktop pbcopy

我的完整解决方案在这里:http://taylor.woodstitch.com/linux/copy-local-clipboard-remote-ssh-server/


这是我基于SSH反向隧道、netcat和xclip的解决方案。

首先创建脚本(例如:Clipboard-daemon.sh)

#!/bin/bash
HOST=127.0.0.1
PORT=3333

NUM=`netstat -tlpn 2>/dev/null | grep -c " ${HOST}:${PORT} "`
if [ $NUM -gt 0 ]; then
    exit
fi

while [ true ]; do
    nc -l ${HOST} ${PORT} | xclip -selection clipboard
done

然后在后台启动。

./clipboard-daemon.sh&

在接收到部分数据后,将开始数控管道输出到xclip和重生过程

然后启动ssh连接到远程主机:

ssh user@host -R127.0.0.1:3333:127.0.0.1:3333

当登录在远程框上时,尝试这样做:

echo "this is test" >/dev/tcp/127.0.0.1/3333

然后在您的工作站上尝试粘贴

当然,您可以编写包装器脚本,首先启动clipboard-daemon.sh,然后启动ssh会话。这就是我的工作方式。享受。


不是一行程序,但不需要额外的ssh。

必要时安装netcat 使用termbin: cat ~/some_file.txt | nc termbin.com 9999。这将把输出复制到termbin网站,并将URL打印到输出中。 从您的计算机访问该url,您将获得输出

当然,不要将其用于敏感内容。


ssh服务器反向隧道端口

所有现有的解决方案要么需要:

客户端上的X11(如果有的话,服务器上的xclip效果很好)或 客户端和服务器在同一个网络中(如果你在工作时试图访问你的家用电脑,情况就不同了)。

这里有另一种方法,不过您需要修改ssh登录计算机的方式。

我已经开始使用这个,它远没有看起来那么吓人,所以试一试。

客户端(ssh会话启动)

ssh username@server.com -R 2000:localhost:2000

(提示:使此为键绑定,这样您就不必键入它)

客户端(另一个选项卡)

nc -l 2000 | pbcopy

注意:如果你没有pbcopy,那么就把它tee到一个文件。

服务器(SSH会话内部)

cat some_useful_content.txt | nc localhost 2000

其他的笔记

实际上,即使您在ssh会话中间,也有一种方法可以启动隧道,但我不想把人们吓跑,因为实际上并没有看起来那么糟糕。但如果有人感兴趣的话,我会稍后再补充细节


最简单的解决方案,如果你在OS X上使用Terminal,并且你已经在远程服务器上ssh了,并且希望获取文本文件或日志或csv的结果,简单地:

1) Cmd-K清除终端输出

2) cat <filename>显示文件的内容

3) Cmd-S保存“终端输出”

你需要手动删除文件的第一行和最后一行,但这种方法比依赖其他要安装的包、“反向隧道”和尝试使用静态IP等要简单一些。


这个答案在选择的答案基础上发展,增加了更多的安全性。

那个回答讨论了一般形式

<command that makes output> | \
    ssh <user A>@<host A> <command that maps stdin to clipboard>

可能缺乏安全性的地方是允许主机B>上<用户B> ssh到主机A并执行任何命令的ssh权限。

当然,从B到A的访问可能已经由ssh密钥进行了门控,它甚至可能有一个密码。但是另一个安全层可以限制B可以对A执行的允许命令的范围,例如,rm -rf /不能被调用。(当ssh密钥没有密码时,这一点尤其重要。)

幸运的是,ssh有一个称为命令限制或强制命令的内置功能。参见ssh.com,或者 这个serverfault.com问题。

下面的解决方案显示了一般的表单解决方案以及强制执行的ssh命令限制。

增加命令限制的解决方案

这个安全增强的解决方案遵循一般形式-从主机- b上的ssh会话调用简单:

cat <file> | ssh <user-A>@<host A> to_clipboard

剩下的部分显示了使其工作的设置。

ssh命令限制设置

假设B上的用户帐户是user-B, B有一个ssh密钥id-clip,它是以通常的方式创建的(ssh-keygen)。

然后在用户a的ssh目录中有一个文件

/home/user-A/.ssh/authorized_keys

识别密钥id-clip并允许SSH连接。

通常每行authorized_keys的内容就是被授权的公钥,例如id-clip.pub的内容。

但是,为了加强命令限制,公钥内容必须在要执行的命令前(在同一行)。 在我们的例子中:

command="/home/user-A/.ssh/allowed-commands.sh id-clip",no-agent-forwarding,no-port-forwarding,no-user-rc,no-x11-forwarding,no-pty <content of file id-clip.pub>

指定的命令“/home/user-A/.ssh/allowed-commands.sh id-clip”,并且只有指定的命令会在key id-clip被使用时执行到host-A的ssh连接-无论ssh命令行中写了什么命令。

该命令指示允许的脚本文件-commands.sh,该脚本文件的内容为

#/bin/bash
#
# You can have only one forced command in ~/.ssh/authorized_keys. Use this
# wrapper to allow several commands.

Id=${1}

case "$SSH_ORIGINAL_COMMAND" in
    "to-clipboard")
          notify-send "ssh to-clipboard, from ${Id}"
        cat | xsel --display :0 -i -b
          ;;
    *)
        echo "Access denied"
        exit 1
        ;;
esac

在机器B上对ssh的原始调用是

... | ssh <user-A>@<host A> to_clipboard

clipboard的字符串通过环境变量SSH_ORIGINAL_COMMAND传递给allowed-commands.sh。 另外,我们已经从authorized_keys中传递了密钥的名称id-clip,它只能被id-clip访问。

这条线

          notify-send "ssh to-clipboard, from ${Id}"

只是一个弹出的消息框,让你知道剪贴板正在写入-这可能是一个很好的安全功能。(通知发送在Ubuntu 18.04上有效,其他系统可能不行)。

在队列中

cat | xsel --display :0 -i -b

参数——display:0是必要的,因为进程没有自己的X显示和剪贴板, 所以必须明确指定。这个值:0恰好在Ubuntu 18.04和Wayland窗口服务器上工作。在其他设置中,它可能不起作用。对于标准的X服务器,这个答案可能会有所帮助。

host-A /etc/ssh/sshd_config参数

最后,在主机a的/etc/ssh/sshd_config中设置几个参数,以确保有连接权限,以及只使用ssh-key而没有密码的权限:

PubkeyAuthentication yes
PasswordAuthentication no
ChallengeResponseAuthentication no
AllowUsers user-A

使sshd服务器重新读取配置

sudo systemctl restart sshd.service

or

sudo service sshd.service restart

结论

这是一些努力建立,但其他功能除了to-剪贴板可以在相同的框架并行构建。


远程管理器Linux端口支持在本地和远程主机之间同步剪贴板。您只需打开本地far2l,在里面执行“ssh somehost”,在ssh会话中运行远程far2l,并使用本地剪贴板获得远程far2l。

它支持Linux, *BSD和OS X;我做了一个特殊的putty构建来利用这个功能从窗口也。


请允许我补充一个解决方案,如果我没记错的话,之前没有建议过。

它不需要客户端暴露在互联网上(没有反向连接),也不需要在服务器上使用任何xlib,并且完全使用ssh自己的功能来实现(没有第三方的箱子)。

它包括:

打开一个到远程主机的连接,然后在它上面创建一个fifo文件,并并行地等待这个fifo(所有事情都是相同的实际TCP连接)。 回显到fifo文件的任何内容都将在本地剪贴板中结束。 当会话完成时,删除服务器上的fifo文件并一起干净地终止连接。

该解决方案利用ssh的ControlMaster功能,只使用一个TCP连接的一切,因此它甚至将支持需要密码登录的主机,并提示您一次。

编辑:按照要求,代码本身:

将以下内容粘贴到bashrc并使用sshx主机进行连接。

在远程机器上返回SOMETHING > ~/clip,希望SOMETHING最终会出现在本地主机的剪贴板中。

您需要在本地主机上使用xclip实用程序。

_dt_term_socket_ssh() {
    ssh -oControlPath=$1 -O exit DUMMY_HOST
}
function sshx {
    local t=$(mktemp -u --tmpdir ssh.sock.XXXXXXXXXX)
    local f="~/clip"
    ssh -f -oControlMaster=yes -oControlPath=$t $@ tail\ -f\ /dev/null || return 1
    ssh -S$t DUMMY_HOST "bash -c 'if ! [ -p $f ]; then mkfifo $f; fi'" \
        || { _dt_term_socket_ssh $t; return 1; }
    (
    set -e
    set -o pipefail
    while [ 1 ]; do
        ssh -S$t -tt DUMMY_HOST "cat $f" 2>/dev/null | xclip -selection clipboard
    done &
    )
    ssh -S$t DUMMY_HOST \
        || { _dt_term_socket_ssh $t; return 1; }
    ssh -S$t DUMMY_HOST "rm $f"
    _dt_term_socket_ssh $t
}

更详细的解释在我的网站上:

https://xicod.com/2021/02/09/clipboard-over-ssh.html


对于任何通过谷歌搜索到这个的人来说: 在这个时代,最好的解决方案似乎是柠檬水

在剪贴板工具的neovim帮助文本中也提到了各种解决方案


如果你在Kubernetes集群中的pod上工作,而不是直接使用SSH,所以你没有办法进行文件传输,你可以使用cat,然后将终端输出保存为文本。例如,在macOS中,您可以执行Shell ->导出为文本。


如果在Mac上使用iTerm2,有一种更简单的方法。该功能通过it2copy命令内置到iTerm2的Shell集成功能中:

Usage: it2copy
          Copies to clipboard from standard input
       it2copy filename
          Copies to clipboard from file

要使其工作,请在登录到远程主机时选择iTerm2—>Install Shell Integration菜单项,将其安装到您自己的帐户中。完成此操作后,您就可以访问it2copy以及其他一些别名命令,这些命令提供了很酷的功能。

这里的其他解决方案都是很好的变通方法,但相比之下,这个解决方案非常简单。