Mac配置iTerm2支持lrzsz功能

之前Windows上面使用XShell,可以很方便的使用lrzsz传输下载服务器上的小文件,Mac上使用比较广的应该算是iTerm2了,苦于一直没有此功能,只能额外使用sftp软件进行操作,下面简单介绍一下如何在Mac上支持此功能

lrzsz简介

lrzsz is a unix communication package providing the XMODEM, YMODEM ZMODEM file transfer protocols. lrzsz是一个unix通信包,提供XMODEM、YMODEM和ZMODEM文件传输协议。简单来讲通过某些协议支持下载和上传功能免费开源软件。

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

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

对 Terminal 的要求就是,遇到特殊指令,触发对应的操作(执行本地命令),这里我们使用iTerm2的Trigger功能实现

操作步骤

在Mac上安装lrzsz 保存并授权可执行 iterm2-send-zmodem.sh 和 iterm2-recv-zmodem.sh 至 /usr/local/bin/ 设置iTerm2的Trigger 在服务器上rz命令,选择文件,等待进度条消失即上传成功 在服务器上使用 sz 文件名,选择保存文件夹,等待进度条消失,即下载成功

安装lrzsz

打开终端在本机执行 brew install lrzsz

安装执行脚本

在本机 /usr/local/bin 路径保存iterm2-send-zmodem.sh和iterm2-recv-zmodem.sh

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

赋予可执行权限

执行 chmod 777 /usr/local/bin/iterm2-*

设置iTerm2的Trigger

设置Iterm2的Trigger特性,菜单项依次点击 iTerm2->Preference->Profiles->Default->Advanced->Trigger中的Edit

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

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

如图示:

小结

搞定! 使用rz上送文件,使用sz下载文件,不过更多是对小文件,大文件建议上sftp

另外最近发现finalshell这个终端,大家可以试试

The post Mac配置iTerm2支持lrzsz功能 first appeared on 司马他.

文章来源:

Author:司马他
link:https://www.congcong.us/post/mac_iterm2_support_lrzsz.html