前言

本文主要介绍 macOS 环境下使用 iterm2 的 rz(上传)和 sz(下载)功能的安装流程,某些时候这些小命令可以方便的代替 scp 绕过安全限制执行简单的上传和下载任务,同理 SecureCRT 和 Xshell 也可以启用 rz 和 sz 功能。

更新历史

2021 年 11 月 18 日 - 完善 macOS lrzsz 安装配置
2020 年 11 月 02 日 - 初稿

阅读原文 - https://wsgzao.github.io/post/lrzsz/


lrzsz 简介

lrzsz: free x/y/zmodem implementation

lrzsz is a unix communication package providing the XMODEM, YMODEM ZMODEM file transfer protocols. lrzsz is a heavily rehacked version of the last public domain release of Omen Technologies rzsz package, and is now free software and released under the GNU General Public Licence.

Features of lrzsz

  • very portable, automagically configured with GNU autoconf.
  • crash recovery.
  • up to 8KB block sizes (ZMODEM8K).
  • internationalized (using GNU gettext). German translation of the programs output exists.
  • far more secure than the original sources.
  • high performance. say `make vcheck-z’ and have a look at the BPS rate - i recently saw 1.4 MB per second transfering a large file through pipes (on a I586/133 system. Beat that!).
  • good blocksize calculation (tries to compute an optimal blocksize based on the number of errors occured).
  • It’s free software.

https://www.ohse.de/uwe/software/lrzsz.html

lrzsz 原理

下载文件
在服务器上执行 sz(Send by ZMODEM),先在终端上输出 **B00000000000000,然后客户端在终端发送指令,表示拒绝,还是接收(接收的话,就在客户端运行 rz 指令与服务端交互)

上传文件
在服务器上执行 rz(Receive by ZMODEM),先在终端上输出 rz waiting to receive.**B0100000023be50,然后客户端发送指令,表示取消,还是上传(上传的话,在客户端运行 sz 命令与服务端交互)。

可以看到在上述流程中,对 Terminal 的要求就是,遇到特殊指令,触发对应的操作(执行本地命令)

由于 macOS 自带的 Terminal.app 不支持这个,所以网上大部分教程都是使用 iTerm2

安装 lrzsz

1
2
3
4
5
6
brew install lrzsz

==> Downloading https://homebrew.bintray.com/bottles/lrzsz-0.12.20_1.catalina.bottle.tar.gz
######################################################################## 100.0%
==> Pouring lrzsz-0.12.20_1.catalina.bottle.tar.gz
🍺 /usr/local/Cellar/lrzsz/0.12.20_1: 18 files, 473.9KB
  1. Install lrzsz on OSX: brew install lrzsz
  2. Save the iterm2-send-zmodem.sh and iterm2-recv-zmodem.sh scripts in /usr/local/bin/
  3. Set up Triggers in iTerm 2 like so:
    How to Create a Trigger
1
2
3
4
5
6
7
8
9
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

To send a file to a remote machine:

  1. Type rz on the remote machine
  2. Select the file(s) on the local machine to send
  3. Wait for the coprocess indicator to disappear

The receive a file from a remote machine

  1. Type sz filename1 filename2 … filenameN on the remote machine
  2. Select the folder to receive to on the local machine
  3. Wait for the coprocess indicator to disappear

安装执行脚本

iterm2-send-zmodem.shiterm2-recv-zmodem.sh 保存到 /usr/local/bin 目录下。

iterm2-send-zmodem.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/bin/bash
# Author: Matt Mastracci (matthew@mastracci.com)
# AppleScript from http://stackoverflow.com/questions/4309087/cancel-button-on-osascript-in-a-bash-script
# licensed under cc-wiki with attribution required
# Remainder of script public domain

osascript -e 'tell application"iTerm2"to version' > /dev/null 2>&1 && NAME=iTerm2 || NAME=iTerm
if [[ $NAME = "iTerm" ]]; then
FILE=$(osascript -e 'tell application"iTerm"to activate' -e 'tell application"iTerm"to set thefile to choose file with prompt"Choose a file to send"' -e "do shell script (\"echo \"&(quoted form of POSIX path of thefile as Unicode text)&\"\")")
else
FILE=$(osascript -e 'tell application "iTerm2" to activate' -e 'tell application "iTerm2" to set thefile to choose file with prompt "Choose a file to send"' -e "do shell script (\"echo \"&(quoted form of POSIX path of thefile as Unicode text)&\"\")")
fi
if [[ $FILE = "" ]]; then
echo Cancelled.
# Send ZModem cancel
echo -e \\x18\\x18\\x18\\x18\\x18
sleep 1
echo
echo \# Cancelled transfer
else
/usr/local/bin/sz "$FILE" --escape --binary --bufsize 4096
sleep 1
echo
echo \# Received"$FILE"
fi

iterm2-recv-zmodem.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/bin/bash
# Author: Matt Mastracci (matthew@mastracci.com)
# AppleScript from http://stackoverflow.com/questions/4309087/cancel-button-on-osascript-in-a-bash-script
# licensed under cc-wiki with attribution required
# Remainder of script public domain

osascript -e 'tell application"iTerm2"to version' > /dev/null 2>&1 && NAME=iTerm2 || NAME=iTerm
if [[ $NAME = "iTerm" ]]; then
FILE=$(osascript -e 'tell application"iTerm"to activate' -e 'tell application"iTerm"to set thefile to choose folder with prompt"Choose a folder to place received files in"' -e "do shell script (\"echo \"&(quoted form of POSIX path of thefile as Unicode text)&\"\")")
else
FILE=$(osascript -e 'tell application "iTerm2" to activate' -e 'tell application "iTerm2" to set thefile to choose folder with prompt "Choose a folder to place received files in"' -e "do shell script (\"echo \"&(quoted form of POSIX path of thefile as Unicode text)&\"\")")
fi

if [[ $FILE = "" ]]; then
echo Cancelled.
# Send ZModem cancel
echo -e \\x18\\x18\\x18\\x18\\x18
sleep 1
echo
echo \# Cancelled transfer
else
cd "$FILE"
/usr/local/bin/rz --rename --escape --binary --bufsize 4096
sleep 1
echo
echo
echo \# Sent \-\> $FILE
fi
1
2
3
4
5
6
7
# 在 / usr/loal/bin 目录下创建两个文件
cd /usr/local/bin
wget https://raw.githubusercontent.com/RobberPhex/iterm2-zmodem/master/iterm2-recv-zmodem.sh
wget https://raw.githubusercontent.com/RobberPhex/iterm2-zmodem/master/iterm2-send-zmodem.sh

# 赋予这两个文件可执行权限
chmod 777 /usr/local/bin/iterm2-*

设置 Iterm2 的 Tirgger 特性

配置好配置文件之后,开始对 iTerm2 进行配置

点击 iTerm2 的设置界面 Perference-> Profiles -> Default -> Advanced -> Triggers 的 Edit 按钮,加入以下配置

添加两条 trigger,分别设置 Regular expression,Action,Parameters,Instant 如下:

1
2
3
4
5
6
7
8
9
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

使用 rz 和 sz

前提是客户端也要安装 lrzsz

  • rz 上传功能
    • 在 bash 中,也就是 iTerm2 终端输入 rz 就会弹出文件选择框,选择文件 choose 就开始上传,会上传到当前目录
    • rz 中的 r 意为 received(接收),告诉客户端,我(服务器)要接收文件 received by cilent,就等同于客户端在上传。
  • sz 下载功能
    • sz fileName(你要下载的文件的名字) 回车,会弹出窗体 我们选择要保存的地方即可。
    • sz 中的 s 意为 send(发送),告诉客户端,我(服务器)要发送文件 send to cilent,就等同于客户端在下载。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
sz 用法:

下载一个文件
sz filename

下载多个文件
sz filename1 filename2

下载 dir 目录下的所有文件,不包含 dir 下的文件夹
sz dir/*

rz 用法:

输入 rz 回车后,会出现文件选择对话框,选择需要上传文件,一次可以指定多个文件,上传到服务器的路径为当前执行 rz 命令的目录。


-b 以二进制方式,默认为文本方式。(Binary (tell it like it is) file transfer override.)

-e 对所有控制字符转义。(Force sender to escape all control characters; normally XON, XOFF, DLE, CR-@-CR, and Ctrl-X are escaped.)

果要保证上传的文件内容在服务器端保存之后与原始文件一致,最好同时设置这两个标志,如下所示方式使用:

rz -be

备注说明

  1. rz 与 sz 只适合小的文件传输,大文件还是使用 Filezilla 与 xftp 等工具进行传输;
  2. 只能传输文件,而不能传输文件夹;
  3. 不是所有工具都支持 rz 与 sz,必须支持 ZModem 协议才行,例如 putty 不能使用 rz 与 sz。

参考文章

macOS 下使用 ZMODEM 协议上传 / 下载文件

ZModem integration for iTerm 2

通过 SecureCRT 的 rz、sz 和 sftp 实现文件的上传和下载

文章目录
  1. 1. 前言
  2. 2. 更新历史
  3. 3. lrzsz 简介
  4. 4. lrzsz 原理
  5. 5. 安装 lrzsz
  6. 6. 安装执行脚本
  7. 7. 设置 Iterm2 的 Tirgger 特性
  8. 8. 使用 rz 和 sz
  9. 9. 参考文章