SSH - 一种无需打开单独的 SFTP 会话即可传输文件的方法?

发布于 2024-07-11 13:41:01 字数 677 浏览 11 评论 0原文

这并不是一个真正的编程问题,但与许多程序员相关......

假设我已经打开了到另一台计算机的 SSH 会话。

远程:html avalys$ ls
welcome.msg index.html readme.txt
远程:html avals$

我可以在远程 shell 中键入任何命令,该命令会立即将当前目录中的文件之一(例如welcome.msg)传输到我的本地计算机,即

远程:html avalys$ stransferwelcome.msg
正在将 /home/avalys/html/welcome.msg 提取到welcome.msg
/home/avalys/html/welcome.msg 100% 23KB 23.3KB/秒 00:00
远程:html avals$

我知道执行此操作的唯一方法是打开并行 SFTP 会话并 CD 到 SSH 会话中的当前目录,这在远程管理服务器时是真正的 PITA。

编辑:我知道使用反向 sftp/scp 连接的可能性,但这涉及更多打字。 如果我可以只输入某个命令的名称(例如“stransfer”)和我想要传输的文件,然后让它正常工作,那就太好了。

Not really a programming question, but relevant to many programmers...

Let's say I have opened an SSH session to another computer.

remote:html avalys$ ls
welcome.msg index.html readme.txt
remote:html avalys$

Is there any command that I can type in my remote shell that will immediately transfer one of the files in the current directory (e.g. welcome.msg) to my local computer, i.e.

remote:html avalys$ stransfer welcome.msg
Fetching /home/avalys/html/welcome.msg to welcome.msg
/home/avalys/html/welcome.msg 100% 23KB 23.3KB/s 00:00
remote:html avalys$

The only way I know of to do this is to open a parallel SFTP session and CD to my current directory in the SSH session, which is a real PITA when administering a server remotely.

EDIT: I am aware of the possibility of using a reverse sftp/scp connection, but that involves more typing. It would be great if I could type just the name of some command (e.g. "stransfer"), and the file(s) I want transferred, and have it Just Work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

新一帅帅 2024-07-18 13:41:02

这是我对此问题的首选解决方案[如我提出的重复问题所示]。 创建 ssh 会话时设置反向 ssh 隧道。 这可以通过两个 bash 函数变得容易:grabfrom() 需要在本地主机上定义,而grab() 应该在远程主机上定义。 您可以添加您认为合适的任何其他 ssh 变量(例如 -X 或 -Y)。

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

用法:

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

优点:

  • 除了 OpenSSH 之外,它无需在任何主机上使用特殊软件即可工作。
  • 当本地主机位于 NAT 路由器后面时,它即可工作。
  • 它可以作为一对两个单行 bash 函数来实现。

缺点:

  • 它使用固定端口号,因此:
    • 不适用于与远程主机的多个连接
    • 可能与远程主机上使用该端口的进程发生冲突
  • 它需要本地主机接受 ssh 连接
  • 它需要在启动会话时使用特殊命令
  • 它不会隐式处理对远程主机的身份验证localhost
  • 它不允许在 localhost 上指定目标目录
  • 如果您从多个本地主机抓取到同一远程主机,ssh 不会喜欢更改

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

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

Here is my preferred solution to this problem [as given on a duplicate question I asked]. 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 $@ [email protected]:~; };

Usage:

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

Positives:

  • It works without special software on either host beyond OpenSSH
  • It works when local host is behind a NAT router
  • It can be implemented as a pair of two one-line bash function

Negatives:

  • It uses a fixed port number so:
    • won't work with multiple connections to remote host
    • might conflict with a process using that port on the remote host
  • It requires localhost accept ssh connections
  • It requires a special command on initiation the session
  • It doesn't implicitly handle authentication to the localhost
  • It doesn't allow one to specify the destination directory on localhost
  • If you grab from multiple localhosts to the same remote host, ssh won't like the keys changing

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()

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.

牵你手 2024-07-18 13:41:02

您可以使用 sshfs 安装 ssh 连接。 这样您就可以在同一个 ssh 连接中传输文件,同时使用您最喜欢的任何 ssh 客户端。 如果您愿意,您甚至可以将其设置为自动安装。

You can mount an ssh connection with sshfs. That way you can transfer files within the same ssh connection, while using whatever ssh client you like best. You can even set it up to automount if you like.

蓝梦月影 2024-07-18 13:41:02

如果您想修补东西(恕我直言,不应该修补),请查看 ssh -xfer

If you are into patching things (that IMHO shouldn't be patched), take a look at ssh-xfer

懷念過去 2024-07-18 13:41:02

您可以编写一个名为 stransfer 的 bash 脚本,该脚本将采用文件名参数,然后它将在 scp 命令中插入文件名,假设服务器和服务器上文件的路径没有更改。

或者,如果文件始终相同,您可以在 ~/.bashrc 文件中创建别名。

别名 getwelcome='scp avalys@remotehost:/home/avalys/html/welcome.msg 。'

You could write a bash script named stransfer that would take a filename argument, then it would interject the filename in the scp command, assuming the server and path to files on server don't change.

Or if the file is always the same you could create an alias in your ~/.bashrc file.

alias getwelcome='scp avalys@remotehost:/home/avalys/html/welcome.msg .'

玩物 2024-07-18 13:41:02

我与保罗·汤布林(Pau​​l Tomblin)在对该问题的评论中所写的想法相同。 旧的终端会话过去使用 x、y 和 z-调制解调器协议和工具(z-调制解调器变体的 szrz)来实现类似的功能。 我不确定这些是否可以通过 ssh 会话运行,但可能值得一试。

Fink 提供了一个 lrzsz 包,其中包含 Mac OS X 上的这些工具

。社区维基,因为在保罗先到达那里后,我会为获得代表而感到难过......

I had the same thought that Paul Tomblin wrote in a comment to the question. Old terminal sessions used to use x-, y-, and z-modem protocols and tools (sz and rz for the z-modem variants) to achieve something like this. I'm not sure if these will work over a ssh session, but it might be worth a try.

Fink supplies a lrzsz package with these tool on Mac OS X.

Making this a community wiki because I'd feel bad for getting rep after Paul got there first...

仲春光 2024-07-18 13:41:02

在学校,如果我要将文件从一个 Linux 系统传输到另一个 Linux 系统,我通常使用 ssh 进行远程,然后使用 scp 将文件从远程系统传输回我的计算机。 不确定这是否是您正在寻找的,或者这是否适用于您的情况,但这通常是我绕过必须打开另一个终端窗口的方法。

只是为了澄清,如果您不熟悉 scp,您仍然需要指定您想要将文件放在计算机上的位置,但对我来说,无论如何,这通常比远程文件的位置更容易记住电脑。 以下是可能有用的链接: http://www.computerhope.com/unix/scp .htm

At school if I am transferring a file from one linux system to another, I usually remote using ssh and then use scp to transfer the file from the remote system back to my computer. Not sure if that is what you are looking for or if that is even applicable in your case but that is usually my way of getting around having to open another terminal window.

Just to clarify if your not familiar with scp you would still have to specify the location of that you would like to put the file on your computer but for me anyways this is generally an easier thing to remember than the location of the file on the remote computer. Here is a link that may be helpful: http://www.computerhope.com/unix/scp.htm

醉生梦死 2024-07-18 13:41:02

https://superuser.com/questions/259481/reverse-scp-over-ssh-connection< /a>

sftp server
get file.txt

显然,您还可以在 sftp 控制台中浏览 n 个内容,并且 get 需要完整的文件路径。 复制到运行 sftp 的目录。

https://superuser.com/questions/259481/reverse-scp-over-ssh-connection

sftp server
get file.txt

Apparently, you can also navigate around n stuff in the sftp console, and get takes a full filepath. Copies to the directory from which you ran sftp.

罪#恶を代价 2024-07-18 13:41:01

设置这样一个反向传输连接

ssh -Rport:127.0.0.1:22 user@host

您可以为 scp

。 使用 scp user@host:port 来访问它。

You could set up such an inverted transfer connection w/

ssh -Rport:127.0.0.1:22 user@host

for scp back.

Use scp user@host:port to access it.

红尘作伴 2024-07-18 13:41:01

在研究过程中,我发现了一个可以直接替代 openssh 客户端 zssh 的程序。

sudo apt-get install zssh
zssh user@remote
sudo apt-get install zssh
sz file.name
<ctrl>+<space>
rz

奇迹般有效。

In researching this I found a program that works as a drop-in replacement for the openssh client, zssh.

sudo apt-get install zssh
zssh user@remote
sudo apt-get install zssh
sz file.name
<ctrl>+<space>
rz

Works like a charm.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文