天天看點

macos下iTerm2使用rz、sz指令傳輸檔案,解決通過堡壘機登陸後無法傳輸檔案

說明:使用rz sz實作終端的檔案傳輸,該指令主要使用場景為 macos中通過堡壘機登陸後無法使用ftp工具傳輸檔案。

工具:iTerm2、lrzsz、homebrew 

以及兩個腳本檔案(iterm2-recv-zmodem.sh、iterm2-send-zmodem.sh)

  • iTerm2

官網下載下傳 http://www.iterm2.com/

下載下傳完直接拖進應用程式就可以了

  • 安裝homebrew 

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

  • lrzsz

brew install lrzsz

配置rz sz 進行上傳下載下傳

cd /usr/local/bin

在該目錄下建立腳本

腳本内容如下:

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

           

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

儲存好後添加執行權限

sudo chmod u+x iterm2-*

iTerm2 配置添加rz sz 功能

Perference-> Profiles -> Default -> Advanced -> Triggers 的 Edit 按鈕

或者使用快捷鍵command+,

macos下iTerm2使用rz、sz指令傳輸檔案,解決通過堡壘機登陸後無法傳輸檔案
macos下iTerm2使用rz、sz指令傳輸檔案,解決通過堡壘機登陸後無法傳輸檔案
macos下iTerm2使用rz、sz指令傳輸檔案,解決通過堡壘機登陸後無法傳輸檔案

編輯内容如下:

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
           

配置完成後如果還無法正常使用,遇到下面的錯誤,需要建立rz、sz軟連接配接到/usr/local/bin 下即可

macos下iTerm2使用rz、sz指令傳輸檔案,解決通過堡壘機登陸後無法傳輸檔案
macos下iTerm2使用rz、sz指令傳輸檔案,解決通過堡壘機登陸後無法傳輸檔案

操作步驟如下:

1.找到檔案位置

macos下iTerm2使用rz、sz指令傳輸檔案,解決通過堡壘機登陸後無法傳輸檔案

2.建立軟連接配接

macos下iTerm2使用rz、sz指令傳輸檔案,解決通過堡壘機登陸後無法傳輸檔案

檢查軟連接配接是否建立

macos下iTerm2使用rz、sz指令傳輸檔案,解決通過堡壘機登陸後無法傳輸檔案

這兩個指令就可以正常使用了

rz 上傳功能

  • 在bash中,也就是iTerm2終端輸入rz 就會彈出檔案選擇框,選擇檔案 choose 就開始上傳,會上傳到目前目錄

sz 下載下傳功能

  • sz fileName(你要下載下傳的檔案的名字) 回車
  • 會彈出窗體 我們選擇要儲存的地方即可。