通过 SSH 会话将文件从远程主机传送到本地主机的最佳方法是什么? [英] Which is the best way to bring a file from a remote host to local host over an SSH session?

查看:57
本文介绍了通过 SSH 会话将文件从远程主机传送到本地主机的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当通过 ssh 连接到远程主机时,我经常想将该系统上的文件带到本地系统进行查看或处理.有没有办法复制文件而无需 (a) 打开新终端/暂停 ssh 会话 (b) 再次向本地或远程主机进行身份验证NAT 路由器?

When connecting to remote hosts via ssh, I frequently want to bring a file on that system to the local system for viewing or processing. Is there a way to copy the file over without (a) opening a new terminal/pausing the ssh session (b) authenticating again to either the local or remote hosts which works (c) even when one or both of the hosts is behind a NAT router?

目标是尽可能多地利用当前状态:两台机器之间存在连接,我在两台机器上都经过身份验证,我在文件的工作目录中——- 所以我不必打开另一个终端并复制并粘贴远程主机和路径,这就是我现在所做的.最好的解决方案也不需要在会话开始之前进行任何设置,但是如果设置是一次性的或能够自动进行的,那么这是完全可以接受的.

The goal is to take advantage of as much of the current state as possible: that there is a connection between the two machines, that I'm authenticated on both, that I'm in the working directory of the file---so I don't have to open another terminal and copy and paste the remote host and path in, which is what I do now. The best solution also wouldn't require any setup before the session began, but if the setup was a one-time or able to be automated, than that's perfectly acceptable.

推荐答案

这是我对这个问题的首选解决方案.在创建 ssh 会话时设置反向 ssh 隧道.这通过两个 bash 函数变得容易:grabfrom() 需要在本地主机上定义,而grab() 应该在远程主机上定义.您可以根据需要添加您使用的任何其他 ssh 变量(例如 -X 或 -Y).

Here is my preferred solution to this problem. Set up a reverse ssh tunnel upon creating the ssh session. This is made easy by two bash function: grabfrom() needs to be defined on the local host, while grab() should be defined on the remote host. You can add any other ssh variables you use (e.g. -X or -Y) as you see fit.

function grabfrom() { ssh -R 2202:127.0.0.1:22 ${@}; };
function grab() { scp -P 2202 $@ localuser@127.0.0.1:~; };

用法:

localhost% grabfrom remoteuser@remotehost
password: <remote password goes here>
remotehost% grab somefile1 somefile2 *.txt
password: <local password goes here>

正面:

  • 它可以在 OpenSSH 之外的任一主机上运行,​​无需特殊软件
  • 它在本地主机位于 NAT 路由器后面时工作
  • 可以实现为一对两个单行的bash函数

否定:

  • 它使用固定端口号,因此:
    • 不适用于与远程主机的多个连接
    • 可能与远程主机上使用该端口的进程发生冲突

    未来工作:这仍然很笨拙.显然,可以通过适当设置 ssh 密钥来处理身份验证问题,并且通过将参数添加到grab() 来允许指定远程目录更容易

    Future work: This is still pretty kludgy. Obviously, it would be possible to handle the authentication issue by setting up ssh keys appropriately and it's even easier to allow the specification of a remote directory by adding a parameter to grab()

    更困难的是解决其他负面问题.选择一个动态端口会很好,但据我所知,没有优雅的方法可以将该端口传递给远程主机上的 shell;尽我所知,OpenSSH 不允许您在远程主机上设置任意环境变量,并且 bash 不能从命令行参数中获取环境变量.即使您可以选择一个动态端口,如果不先连接,也无法确保它不会在远程主机上使用.

    More difficult is addressing the other negatives. It would be nice to pick a dynamic port but as far as I can tell there is no elegant way to pass that port to the shell on the remote host; As best as I can tell, OpenSSH doesn't allow you to set arbitrary environment variables on the remote host and bash can't take environment variables from a command line argument. Even if you could pick a dynamic port, there is no way to ensure it isn't used on the remote host without connecting first.

    这篇关于通过 SSH 会话将文件从远程主机传送到本地主机的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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