SSH - 一种无需打开单独的 SFTP 会话即可传输文件的方法?
这并不是一个真正的编程问题,但与许多程序员相关......
假设我已经打开了到另一台计算机的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这是我对此问题的首选解决方案[如我提出的重复问题所示]。 创建 ssh 会话时设置反向 ssh 隧道。 这可以通过两个 bash 函数变得容易:grabfrom() 需要在本地主机上定义,而grab() 应该在远程主机上定义。 您可以添加您认为合适的任何其他 ssh 变量(例如 -X 或 -Y)。
用法:
优点:
缺点:
未来工作的密钥:
这仍然是相当笨拙的。 显然,可以通过适当地设置 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.
Usage:
Positives:
Negatives:
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.
您可以使用 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.
如果您想修补东西(恕我直言,不应该修补),请查看 ssh -xfer
If you are into patching things (that IMHO shouldn't be patched), take a look at ssh-xfer
您可以编写一个名为 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 .'
我与保罗·汤布林(Paul Tomblin)在对该问题的评论中所写的想法相同。 旧的终端会话过去使用 x、y 和 z-调制解调器协议和工具(z-调制解调器变体的
sz
和rz
)来实现类似的功能。 我不确定这些是否可以通过 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
andrz
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...
在学校,如果我要将文件从一个 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
https://superuser.com/questions/259481/reverse-scp-over-ssh-connection< /a>
显然,您还可以在 sftp 控制台中浏览 n 个内容,并且
get
需要完整的文件路径。 复制到运行sftp
的目录。https://superuser.com/questions/259481/reverse-scp-over-ssh-connection
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 ransftp
.设置这样一个反向传输连接
您可以为
scp
。 使用
scp user@host:port
来访问它。You could set up such an inverted transfer connection w/
for
scp
back.Use
scp user@host:port
to access it.在研究过程中,我发现了一个可以直接替代 openssh 客户端 zssh 的程序。
奇迹般有效。
In researching this I found a program that works as a drop-in replacement for the openssh client, zssh.
Works like a charm.