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

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

pbcopy, pbpaste 

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

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

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

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


当前回答

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

其他回答

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

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

这是我基于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,您将获得输出

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

有各种工具可以访问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