Mac 上使用 zmodem 发送和接收堡垒机文件

it2025-11-02  2

背景

公司的 CentOS 服务器需要通过堡垒机登录,上传下载文件不能直接使用 scp 的方式操作,于是采用 zmodem 来实现

实现

首先服务器系统 CentOS 上要安装 zmodem 的实现工具 lrzsz,通过命令(通常情况下是已经安装过的)

yum -y install lrzsz

本地的 PC 我使用 Mac 电脑,按如下流程进行

1,安装 lrzsz

brew install lrzsz

如果 brew 没有修改过镜像服务器,先按这篇说明进行操作,这样下载速度才不会很慢

HomeBrew 镜像加速

2, 创建 item2 的 zmodem 脚本

cd /usr/local/bin touch item2-send-zmodem.sh touch iterm2-recv-zmodem.sh

文件内容如下

item2-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 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)&\"\")"` if [[ $FILE = "" ]]; then echo Cancelled. # Send ZModem cancel echo -e \\x18\\x18\\x18\\x18\\x18 echo \# Cancelled transfer echo else echo $FILE /usr/local/bin/sz "$FILE" echo \# Received $FILE echo 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 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)&\"\")"` if [[ $FILE = "" ]]; then echo Cancelled. # Send ZModem cancel echo -e \\x18\\x18\\x18\\x18\\x18 echo \# Cancelled transfer echo else echo $FILE cd "$FILE" /usr/local/bin/rz echo \# Received $FILE echo fi

给文件赋予权限

chmod 777 item2-*

3, 设置 item2

修改 iTerm2 的 default trigger( iTerm 工具栏 -> Profiles -> Open Profiles -> Edit Profiles -> 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

如下图

使用

现在可以使用了,尝试一下

本地发文件到远端服务器

1,item2 登录到远端服务器

2,服务器上执行 sudo rz (要提权,否则文件传不上去)

3,此时会弹出文件选择框,选择要上传的文件后会进行传输

本地下载远端服务器文件

1,item2 登录到远端服务器

2,在服务器上执行 sz filename

3,此时会弹出文件选择框,选择要下载到本地的路径后会进行传输

大功告成,可以舒服的玩转上传下载了

最新回复(0)