Mac下用iTerm2终端使用rz、sz进行服务器上传下载文件

linux服务器上安装了rz、sz,本地也安装好相应工具和配置触发即可。

原文链接:https://blog.csdn.net/ccor2002/article/details/105458152/

1.安装rz、sz

brew install lrzsz

2.配置iterm2触发器

/usr/local/bin/下创建脚本iterm2-send-zmodem.shiterm2-recv-zmodem.sh

  • iterm2-send-zmodem.sh内容如下:
#!/bin/bash
# Author: Matt Mastracci ([email protected])
# 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 ([email protected])
# 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

3.赋予两个脚本执行权限

chmod +x iterm2-recv-zmodem.sh
chmod +x iterm2-send-zmodem.sh

4.在iterm2中配置两条触发规则

iTerm2 -> Preferences -> Profiles -> Default -> Advanced-> Triggers下的Edit按钮

    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
配置效果.png

配置完了,新开一个iTerm2的窗口就可以试试执行rz,看能不能弹出选择文件窗口,如果可以就配置好了。

5.安装zssh(Zmodem SSH)

zssh(Zmodem SSH)是一个程序,用于在使用安全外壳(ssh)时以交互方式将文件传输到远程计算机。它旨在作为scp的便捷替代方法,允许您无需打开另一个会话并重新进行身份验证即可传输文件。

brew install zssh
  • 上传文件
    登录到服务器后,输入rz就会自动弹出选择文件对话框了,选择后确定开始上传传输。

  • 下载文件
    登录到服务器后,输入sz filename 就会自动弹出选择路径对话框了,选择后确定开始下载传输。

你可能感兴趣的:(Mac下用iTerm2终端使用rz、sz进行服务器上传下载文件)