1. 程式人生 > >Mac iTerm 2使用rz、sz從遠端上傳下載檔案

Mac iTerm 2使用rz、sz從遠端上傳下載檔案

最近有將本地檔案傳向伺服器的需求,於是從同事那裡聽來了rz、sz這個黑魔法,迫不及待就安裝使用了一下,極其好用。

(PS:不要問為嘛不使用scp,因為種種原因無法使用,而且有了這個之後即使可以使用scp我也不會用了,為嘛?懶!輸命令很麻煩!ip記不住更麻煩!)

百度的開發機需要跳兩次,所以scp不好使了

安裝配置

安裝lrzsz

使用 brew install lrzsz 。如果安裝遇到錯誤的話,使用以下方法:

curl -k https://ohse.de/uwe/releases/lrzsz-0.12.20.tar.gz -o lrzsz-0.12.20.tar.gz

將下載的檔案放到 ~/Library/Caches/Homebrew

目錄下

brew install lrzsz

將 iterm2-send-zmodem.sh 和 iterm2-recv-zmodem.sh 指令碼儲存在 /usr/local/bin/

#!/bin/bash
# iterm2-recv-zmodem.sh

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
#!/bin/bash
# iterm2-send-zmodem.sh

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

在iTerm 2新增Triggers

開啟iTerm的Preferences,或者使用 command + , ,點進Profiles項,Advanced項,進入Tirggers,點選Edit,新增兩條規則:

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 filename filename1 … filenameN

選擇本地的儲存目錄
等待下載
PS:遠端伺服器也需要安裝lrzsz。
centOS安裝方法: yum -y install lrzsz

存在的問題

大檔案傳輸不了,會卡住

參考