macOS 使用 iterm2 启用 rz 与 sz 功能

前言

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

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

如果你已经升级到最新的apple silicon比如M1/M2/M3/…

brew安装lrzsz的位置是在 /opt/homebrew/

➜  ~ brew install lrzsz
==> Downloading https://formulae.brew.sh/api/formula.jws.json
##################################################################################################################### 100.0%
==> Downloading https://formulae.brew.sh/api/cask.jws.json
##################################################################################################################### 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/lrzsz/manifests/0.12.20_1
##################################################################################################################### 100.0%
==> Fetching lrzsz
==> Downloading https://ghcr.io/v2/homebrew/core/lrzsz/blobs/sha256:427b498d1c8853fdcd90bc96572f5f622c7c17b229101382fa235aad
##################################################################################################################### 100.0%
==> Pouring lrzsz--0.12.20_1.arm64_sonoma.bottle.tar.gz
🍺  /opt/homebrew/Cellar/lrzsz/0.12.20_1: 19 files, 660.0KB
==> Running `brew cleanup lrzsz`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

# 设置ln软连接修复问题
ln -s /opt/homebrew/Cellar/lrzsz/0.12.20_1/bin/rz /usr/local/bin/rz
ln -s /opt/homebrew/Cellar/lrzsz/0.12.20_1/bin/sz /usr/local/bin/sz

安装lrzsz

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

#!/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

#!/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
# 在/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如下:

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,就等同于客户端在下载。
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。