iTerm2 使用 Zmodem 实现快速传输文件


=Start=

缘由:

之前在本地电脑和IDC服务器进行文件传输基本都是用的sftp服务器来进行的中转(因为中间需要经过跳板机器,不能直连,也就无法使用scp命令进行文件传输),操作起来很麻烦,尤其是涉及到多台IDC服务器的时候,每一台服务器用sftp命令上传下载的时候都需要进行ssh登录(多台服务器之间路由不通,没办法通过临时搭建SimpleHTTPServer服务器的方式将多台服务器上的文件进行汇聚,从而减少工作量)。前两天刚了解到可以用sz/rz命令进行本机和IDC服务器的文件传输,试了一下确实可以,不过还是很好奇原理是什么,这里简单记录一下sz/rz命令的环境搭建步骤和使用方法,方便后面参考。

正文:

参考解答:

iTerm2 是一款非常流行的终端工具,而 Zmodem 则是一种用于在终端中安全地传输文件的协议。为了在 iTerm2 中使用 Zmodem,您需要在本地安装 lrzsz 工具,并在远程服务器上配置相关设置。

1. 客户端配置(本地macOS系统)

1. 安装 lrzsz

brew install lrzsz

2. 下载并保存唤起 iTerm2 调用sz/rz命令的脚本

wget https://raw.githubusercontent.com/robberphex/iTerm2-zmodem/master/iterm2-send-zmodem.sh
wget https://raw.githubusercontent.com/robberphex/iTerm2-zmodem/master/iterm2-recv-zmodem.sh
chmod +x iterm2-send-zmodem.sh iterm2-recv-zmodem.sh
cp iterm2-send-zmodem.sh iterm2-recv-zmodem.sh /usr/local/bin/

3. 配置 iTerm2 上的触发器

打开 iTerm2 ,点击 Settings → Preferences(command+,) → Profiles 选择指定的 Profile(一般就是默认的Default)。然后继续选择 Advanced → Triggers,并点击 Edit 添加两个触发器。

    Regular expression: rz waiting to receive.\*\*B0100
    Action: Run Silent Coprocess
    Parameters: /usr/local/bin/iterm2-send-zmodem.sh
    Instant: checked

    Regular expression: \*\*B00000000000000
    Action: Run Silent Coprocess
    Parameters: /usr/local/bin/iterm2-recv-zmodem.sh
    Instant: checked

配置这两个触发器的作用就是让 iTerm2 根据终端上显示的字符通过指定的触发器调用相应的发送和接收脚本。

2. 服务端配置

lrzsz 软件包是 支持 Zmodem 协议的工具包。 其包含的 rz、sz 命令是通过 ZModem 协议在远程服务器和终端机器间上传下载文件的利器。

为了正确通过 sz、rz 命令传输文件,服务端需要安装 lrzsz 软件包的。

yum install lrzsz
apt-get install lrzsz

3. 使用 sz/rz 命令传输文件

sz/rz 的命令都是在IDC的Linux服务器(远端服务器)上执行的,这个要注意。

  1. 在远端服务器上发送文件到本地电脑
  • 远端服务器上执行 sz filename1 filename2…filenameN 命令
  • 本地选择目录保存
  • 等待传输即可
  1. 在远端服务器上接收来自本地电脑的文件
  • 远端服务器上执行 rz 命令
  • 本地选择文件传输
  • 等待传输即可

补充说明:
rz与sz只适合小的文件传输,大文件还是使用sftp等工具进行传输;
只能传输文件,而不能传输文件夹;
不是所有工具都支持rz与sz,必须支持ZModem协议才行,例如Putty不能使用rz与sz。

参考链接:

iTerm2 实现 ssh 自动登录,并使用 Zmodem 实现快速传输文件
https://icloudnative.io/posts/iterm2-auto-login/

在 iTerm2 中使用 Zmodem 实现快速传输文件
https://www.hi-linux.com/posts/9916.html

Triggers
https://iterm2.com/documentation-triggers.html

在iTerm2中使用Zmodem传输文件
http://codelife.me/blog/2013/03/02/zmodem-integration-for-iterm2/

macOS下使用ZMODEM协议上传/下载文件
https://robberphex.com/use-zmodem-at-macos/

iTerm2-zmodem
https://github.com/RobberPhex/iTerm2-zmodem

在 Mac 下,实现与服务器进行便捷的文件上传和下载操作
https://github.com/aikuyun/iterm2-zmodem

文件传输命令 rz 与 sz 安装与使用
https://mp.weixin.qq.com/s/Zy1AGsM5rMJH9ngrfB1x1A

每天一个 Linux 命令(69):rz、sz命令
https://mp.weixin.qq.com/s/b1EwbhsSRGY7KG5oPrrrvA

ZMODEM
https://zh.wikipedia.org/wiki/ZMODEM
https://en.wikipedia.org/wiki/ZMODEM

iterm2 安装zmodem遇到的问题
https://juejin.cn/s/iterm2%20%E5%AE%89%E8%A3%85zmodem%E9%81%87%E5%88%B0%E7%9A%84%E9%97%AE%E9%A2%98

=END=

,

《 “iTerm2 使用 Zmodem 实现快速传输文件” 》 有 5 条评论

  1. 1.2 linux服务器上传下载协议zmodem
    https://www.jianshu.com/p/149aeb9b1456
    `
    SSH协议实现了服务器的远程登录,但是不支持文件的上传下载,上传下载需要zmodem协议的支持。
    `

    rz、sz是古老的ZModem协议的文件传输工具
    https://blog.51cto.com/u_15065851/4539283
    `
    rz、sz是古老的ZModem协议的文件传输工具。
    想必有人会问为什么不用scp或者sftp啊?但是这些还是没rz、sz方便,比如你ssh到主机A,再通过A ssh 到B,想把B的文件传到本地,不是得先scp到A,再到本地呢?用sz就可以一次搞定~
    `

    SecureCRT上传下载文件到linux服务器
    https://syddos.com/173.html
    `
    文件传输是数据交换的主要形式。在进行文件传输时,为使文件能被正确识别和传送,我们需要在两台计算机之间建立统一的传输协议。这个协议包括了文件的识别、传送的起止时间、错误的判断与纠正等内容。常见的传输协议有以下几种:

    ASCII:这是最快的传输协议,但只能传送文本文件。
    Xmodem:这种古老的传输协议速度较慢,但由于使用了CRC错误侦测方法,传输的准确率可高达99.6%。
    Ymodem:这是Xmodem的改良版,使用了1024位区段传送,速度比Xmodem要快。
    Zmodem:Zmodem采用了串流式(streaming)传输方式,传输速度较快,而且还具有自动改变区段大小和断点续传、快速错误侦测等功能。这是目前最流行的文件传输协议。
    `

    超级好用的文件传输命令rz与sz
    https://zhuanlan.zhihu.com/p/425303377

  2. zssh – interactive file transfer wrapper for ssh
    https://manpages.ubuntu.com/manpages/xenial/man1/zssh.1.html

    使用zssh远程传送文件
    https://blog.51cto.com/suchalin/1043502

    Help me drive Zmodem over ssh with expect
    https://stackoverflow.com/questions/5018479/help-me-drive-zmodem-over-ssh-with-expect

    SecureCRT supports file transfer using the old Xmodem and Zmodem protocols, secured via SSH.
    https://www.oreilly.com/library/view/ssh-the-secure/0596008953/ch17s07.html

    The ZMODEM Inter Application File Transfer Protocol
    http://gallium.inria.fr/~doligez/zmodem/zmodem.txt

    Evolution and Selection of File Transfer Protocols(By Chuck Forsberg)
    https://web.archive.org/web/20150107103454/http://www.omen.com/zmdmev.html

  3. Anyone still use or be familiar with the rz and sz command?
    https://unix.stackexchange.com/questions/635152/anyone-still-use-or-be-familiar-with-the-rz-and-sz-command
    `
    我的问题是

    1. How does rz and sz works internally? How does rz and sz work under my condition when scp and ssh directly to the remote servers are not available?
    rz 和 sz 在内部是如何工作的?当 scp 和 ssh 直接连接远程服务器不可用时, rz 和 sz 在我的条件下如何工作?

    2. Are rz and sz reliable? Does anyone still use that? Is there a better solution for my need?
    rz 和 sz 可靠吗?还有人在使用它们吗?有没有更好的解决方案来满足我的需求?

    ==

    They just basically use the terminal connection to talk to each other, and send the file over that, using a particular protocol. That involves some synchronisation (as in that the sz program doesn’t just dump the data over the moment you start it), metadata handling (e.g. the file name) and error detection and resending when necessary. Also they seem to do some terminal setup, probably to prevent any possible control characters from being interpreted in a way that would interfere with the transfer.
    它们基本上都是使用终端连接进行对话,并通过终端连接使用特定协议发送文件。这涉及到一些同步(比如 sz 程序不会在你启动它的那一刻就把数据转储过来)、元数据处理(如文件名)以及错误检测和必要时的重新发送。此外,它们似乎还做了一些终端设置,可能是为了防止任何可能的控制字符被解释成干扰传输的方式。

    =

    Well, they’ve been used since ages, and files are still (mostly) just 8 bits-per-byte dumps of data, so there’s no reason they shouldn’t basically still work. They are a bit archaic though, but that’s not wrong per se. Performance is another question, of course, and if you find a bug, you may want to prepare for having a hard time finding an active maintainer to fix it.
    不过,它们从很久以前就开始使用了,而且文件(大部分)仍然只是每字节 8 位的数据转储,所以它们基本上没有理由不能继续使用。虽然它们有点过时,但这本身并没有错。当然,性能是另一个问题,如果你发现了一个错误,你可能要做好很难找到一个积极的维护者来修复它的准备。

    =

    As for what’s best for your need, you’ll have to decide for yourself.
    至于什么最适合您的需求,您必须自己决定。

    ==

    zmodem uses the existing connection sending and receiving characters. This means that all the infrastructure needed is already in place.
    zmodem 使用现有连接发送和接收字符。这意味着所需的所有基础设施都已就位。

    ==

    SSH 有 ProxyCommand 功能,可以简化跳转主机的使用,可能会让只使用 scp 或 sftp 更容易。
    `

  4. SSH ProxyCommand example: Going through one host to reach another server
    https://www.cyberciti.biz/faq/linux-unix-ssh-proxycommand-passing-through-one-host-gateway-server/

    OpenSSH/Cookbook/Proxies and Jump Hosts
    https://en.wikibooks.org/wiki/OpenSSH/Cookbook/Proxies_and_Jump_Hosts

    How To Use SSH ProxyJump and SSH ProxyCommand
    https://goteleport.com/blog/ssh-proxyjump-ssh-proxycommand/

    SSH to remote hosts through a proxy or bastion with ProxyJump
    https://www.redhat.com/sysadmin/ssh-proxy-bastion-proxyjump

  5. Simple Python Http Server with Upload – Python3 version (测试OK)
    https://gist.github.com/touilleMan/eb02ea40b93e52604938

    Python SimpleHTTPServer to receive files (测试OK,页面仅支持浏览,上传需要通过curl等命令进行操作)
    https://stackoverflow.com/questions/39788591/python-simplehttpserver-to-receive-files
    https://gist.github.com/smidgedy/1986e52bb33af829383eb858cb38775c

    python simple http server with upload & download (测试OK,页面浏览功能不支持;另外就是因为仅改写了PUT操作的代码,所以仅支持用PUT方法上传文件)
    https://gist.github.com/darkr4y/761d7536100d2124f5d0db36d4890109
    Receiving files over HTTP with Python
    https://floatingoctothorpe.uk/2017/receiving-files-over-http-with-python.html

    Python’s http.server extended to include a file upload page (因为要额外安装包,所以暂未测试)
    https://pypi.org/project/uploadserver/
    https://github.com/Densaugeo/uploadserver

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注