iterm2(1)-mac iterm2安装lrzsz可使用rz和sz实现对远程服务器上传下载文件

在iTerm2中使用Zmodem的方法

iTerm通过trigger的方式可以使用sz、rz命令方便的通过Terminal在远程主机文件传输。
首先需要安装iTerm Build 1.0.0.20120724以上版本,因为从这个版本开始支持trigger。

前提:

首先mac自带的终端是不支持lrzsz的,需要下载安装iterm2,下载地址:

http://www.iterm2.cn/download

也需要安装homebrew去下载其他

已安装好brew和iterm2

1.安装lrzsz(要先安装brew,如果没有安装homeBrew需要先安装,安装完成后检查 ls -alh /usr/local/bin/sz 是否存在)

brew install lrzsz

2.添加trigger

lrszs命令安装成功之后就是要创建两个脚本到/usr/local/bin目录,脚本地址:

cd /usr/local/bin

   在/usr/loal/bin 目录下创建两个文件

命令:

vi iterm2-recv-zmodem.sh
vi iterm2-send-zmodem.sh

创建好两个文件后分别添加内容:

1. iterm2-recv-zmodem.sh

 1 #!/bin/bash
 2 # Author: Matt Mastracci ([email protected])
 3 # AppleScript from http://stackoverflow.com/questions/4309087/cancel-button-on-osascript-in-a-bash-script
 4 # licensed under cc-wiki with attribution required 
 5 # Remainder of script public domain
 6  
 7 osascript -e 'tell application "iTerm2" to version' > /dev/null 2>&1 && NAME=iTerm2 || NAME=iTerm
 8 if [[ $NAME = "iTerm" ]]; then
 9     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)&\"\")"`
10 else
11     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)&\"\")"`
12 fi
13  
14 if [[ $FILE = "" ]]; then
15     echo Cancelled.
16     # Send ZModem cancel
17     echo -e \\x18\\x18\\x18\\x18\\x18
18     sleep 1
19     echo
20     echo \# Cancelled transfer
21 else
22     cd "$FILE"
23     /usr/local/bin/rz -E -e -b
24     sleep 1
25     echo
26     echo
27     echo \# Sent \-\> $FILE
28 fi
29  

2. iterm2-send-zmodem.sh

 1 #!/bin/bash
 2 # Author: Matt Mastracci ([email protected])
 3 # AppleScript from http://stackoverflow.com/questions/4309087/cancel-button-on-osascript-in-a-bash-script
 4 # licensed under cc-wiki with attribution required 
 5 # Remainder of script public domain
 6  
 7 osascript -e 'tell application "iTerm2" to version' > /dev/null 2>&1 && NAME=iTerm2 || NAME=iTerm
 8 if [[ $NAME = "iTerm" ]]; then
 9     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)&\"\")"`
10 else
11     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)&\"\")"`
12 fi
13 if [[ $FILE = "" ]]; then
14     echo Cancelled.
15     # Send ZModem cancel
16     echo -e \\x18\\x18\\x18\\x18\\x18
17     sleep 1
18     echo
19     echo \# Cancelled transfer
20 else
21     /usr/local/bin/sz "$FILE" -e -b
22     sleep 1
23     echo
24     echo \# Received $FILE
25 fi 
26  

将文件写好后保存好,使用如下命令添加权限

chmod 777 iterm2-*
配置好配置文件之后,开始对iTerm2进行配置

点击 iTerm2 的设置界面 Perference-> Profiles -> Default -> Advanced -> Triggers 的 Edit 按钮,加入以下配置

在弹出的界面点"+"新增两项参数,分别是:

\*\*B010                                  Run Silent Coprocess        /usr/local/bin/iterm2-send-zmodem.sh

\*\*B00000000000000              Run Silent Coprocess        /usr/local/bin/iterm2-recv-zmodem.sh

红色的也行,框中的内容也行;新增后点击"Close"即完成配置。

1 Regular expression: rz waiting to receive.\*\*B0100
2 Action: Run Silent Coprocess
3 Parameters: /usr/local/bin/iterm2-send-zmodem.sh
4  
5 Regular expression: \*\*B00000000000000
6 Action: Run Silent Coprocess
7 Parameters: /usr/local/bin/iterm2-recv-zmodem.sh
8  

备注:

rz 上传功能   :在bash中,也就是iTerm2终端输入rz 就会弹出文件选择框,选择文件 choose 就开始上传,会上传到当前目录
sz 下载功能  :sz fileName(你要下载的文件的名字) 回车,会弹出窗体 我们选择要保存的地方即可。

参考:https://blog.csdn.net/xiasohuai/article/details/100775272

猜你喜欢

转载自www.cnblogs.com/yiyaxuan/p/12545655.html