Mac与服务器传文件

Mac与服务器传文件

存在堡垒机(跳板机)时,scp命令失效。传小文件最好用的方法是 rz、sz。

安装

使用 homebrew 安装:

brew install lrzsz

如果没有装 homebrew,先安装这个mac下神级的包管理工具:

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

配置

定义两个脚本文件控制文件传输

cd /usr/local/bin

iterm2-send-zmodem.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/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

iterm2-recv-zmodem.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/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中添加triggers,使得在终端中传文件时顺利跳转,防止卡死。

iterm2 => Preferences => Profiles => Advanced => Triggers => Edit

添加两条规则:

1
2
3
4
5
6
7
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

OK!