0%

使用iTerm2上传下载文件

在 Mac 上使用 iTerm2 确实让 Linux 终端操作方便的很多,加上oh-my-zsh这个强大 shell 的利器,敲命令就像滚键盘一样。

但是 iTerm2 没有对文件上传下载进行支持,这个是要比 secureCRT 弱的地方,不过我们总有办法能够解决,因为 iTerm2 足够强大,下面是利用 rz/sz 工具来实现文件上传下载的方法:

在 Mac 电脑上安装 rz/sz:brew install lrzsz

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env bash

#
# iterm2-zmodem
#
# copyright (c) 2013 by Harald Lapp <[email protected]>
#
# AppleScript portion from:
# https://stackoverflow.com/questions/4309087/cancel-button-on-osascript-in-a-bash-script
# licensed under cc-wiki with attribution required
#

#
# This script can be found at:
# https://github.com/aurora/iterm2-zmodem
#

#
# This is a re-implementation of the shell scripts "iterm2-recv-zmodem.sh" and
# "iterm2-send-zmodem.sh" found at https://github.com/mmastrac/iterm2-zmodem
#

# usage
if [[ $1 != "sz" && $1 != "rz" ]]; then
echo "usage: $0 sz|rz"
exit
fi

# send Z-Modem cancel sequence
function cancel {
echo -e \\x18\\x18\\x18\\x18\\x18
}

# send notification using growlnotify
function notify {
local msg=$1

if command -v growlnotify >/dev/null 2>&1; then
growlnotify -a /Applications/iTerm.app -n "iTerm" -m "$msg" -t "File transfer"
else
echo "# $msg" | tr '\n' ' '
fi
}

#setup
[[ $LRZSZ_PATH != "" ]] && LRZSZ_PATH=":$LRZSZ_PATH" || LRZSZ_PATH=""

PATH=$(command -p getconf PATH):/usr/local/bin$LRZSZ_PATH
ZCMD=$(
if command -v $1 >/dev/null 2>&1; then
echo "$1"
elif command -v l$1 >/dev/null 2>&1; then
echo "l$1"
fi
)

# main
if [[ $ZCMD = "" ]]; then
cancel
echo

notify "Unable to find Z-Modem tools"
exit
elif [[ $1 = "rz" ]]; then
# receive a file
DST=$(
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 [[ $DST = "" ]]; then
cancel
echo
fi

cd "$DST"

notify "Z-Modem started receiving file"

$ZCMD -e -y
echo

notify "Z-Modem finished receiving file"
else
# send a file
SRC=$(
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 [[ $SRC = "" ]]; then
cancel
echo
fi

notify "Z-Modem started sending
$SRC"

$ZCMD -e "$SRC"
echo

notify "Z-Modem finished sending
$SRC"
fi

首先将这个脚本写到/usr/local/bin/iterm2-zmodem 文件下,给它可执行权限chmod +x /usr/local/bin/iterm2/zmodem,这个脚本是在 Linux 终端使用 rz/sz 时调用的,需要由 iTerm2 触发,在 iTerm2->Preferences…->Profiles->Advanced 下的 Triggers,点击 Edit 进入编辑。加入如下配置即可:

1
2
3
4
5
6
7
Regular expression: \*\*B0100
Action: Run Coprocess
Parameters: /usr/local/bin/iterm2-zmodem sz

Regular expression: \*\*B00000000000000
Action: Run Coprocess
Parameters: /usr/local/bin/iterm2-zmodem rz

Github 链接:https://github.com/Jack614/iterm2-zmodem