Mac下使用iterm在服务器进行上传下载文件

客户端通过iTerm工具连接服务器,使用rz和sz命令可以实现服务器与本地之间文件的上传与下载

本机环境

macOS 10.14.6 item2 Build 3.2.6

1. 安装iterm2

建议去官网下载 http://www.iterm2.com/

2. 安装brew

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

3. 安装lrzsz

brew install lrzsz

如果提示文件写权限等问题,按照错误提示修改文件夹的写权限即可

4. 配置rz sz 进行上传下载

cd /usr/local/bin

手动创建两个脚本文件 iterm2-recv-zmodem.sh和iterm2-send-zmodem.sh

iterm2-recv-zmodem.sh内容如下

#!/bin/bash

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 -E -e -b --bufsize 4096
        sleep 1
        echo
        echo
        echo \# Sent \-\> $FILE
fi

iterm2-send-zmodem.sh内容如下

#!/bin/bash

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" -e -b
        sleep 1
        echo
        echo \# Received $FILE
fi

更改两个脚本的权限: chmod 777 /usr/local/bin/iterm2-*

5. 配置iTerm2添加rz sz

依次点击 iTerm2 的设置界面 Perference-> Profiles -> Default -> Advanced -> Triggers 的 Edit

Mac下使用iterm在服务器进行上传下载文件_第1张图片
image-20191128134946893.png
Mac下使用iterm在服务器进行上传下载文件_第2张图片
image-20191128135032733.png
Mac下使用iterm在服务器进行上传下载文件_第3张图片
image-20191128135116671.png

输入以下对应内容

Regular expression: rz waiting to receive.**B0100

Action: Run Silent Coprocess

Parameters: /usr/local/bin/iterm2-send-zmodem.sh

Regular expression: **B00000000000000

Action: Run Silent Coprocess

Parameters: /usr/local/bin/iterm2-recv-zmodem.sh

6. 安装完成

登录服务器,使用rz和sz进行文件上传下载

微信公众号

你可能感兴趣的:(Mac下使用iterm在服务器进行上传下载文件)