Mac服务器文件交互
在Mac中使用rz,sz命令去和服务器进行文件交互,下面介绍一下如何配置MAC上的rz,sz。
1、安装iterm2
Mac自带的终端是不支持lrzsz,需要下载Mac上强大的终端工具–Iterm2。官网链接附上。
2、安装brew
brew是Mac上的一个包管理工具,可以很方便的安装各种软件,Mac上默认安装了ruby,在item2终端下执行如下命令即可:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
3、使用brew 安装lrzsz
lrzsz是一款在linux里可代替ftp上传和下载的程序。它使用unix通信套件提供的X,Y,和ZModem文件传输协议。通过下载它来使用rz,sz。
命令:
brew install lrzsz
4、使用brew安装wget
下载lrzsz之后,需要使用wget下载iterm2-zmodem。Mac默认不安装wget,可以通过brew安装。
命令:
brew install wget
5、配置Item2使用Zmodem传输文件
方法1.使用wegt下载脚本
下载的命令如下:
#在指定的文件夹下载上传和下载的脚本配置文件
cd /usr/local/bin
wget https://raw.github.com/mmastrac/iterm2-zmodem/master/iterm2-send-zmodem.sh
wget https://raw.github.com/mmastrac/iterm2-zmodem/master/iterm2-recv-zmodem.sh
# 改变文件的权限
chmod 777 /usr/local/bin/iterm2-*
方法2.自己编写脚本文件
在我本人的验证中很多次遇到网络连接失败的报错,这里我把两个脚本的具体内容写在下面。
5.1先到指定的目录:
cd /usr/local/bin
5.2然后新建脚本文件,注意不用使用root权限。个人用户权限就行:
vim iterm2-send-zmodem.sh
- iterm2-send-zmodem.sh脚本文件的具体内容如下:
#!/bin/bash
# Author: Matt Mastracci (matthew@mastracci.com)
# 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
5.3然后给该文件执行权限
chmod 777 iterm2-send-zmodem.sh
5.4再新建脚本文件,注意同样不用使用root权限。个人用户权限就行:
vim iterm2-recv-zmodem.sh
- iterm2-recv-zmodem.sh脚本文件的具体内容如下:
#!/bin/bash
# Author: Matt Mastracci (matthew@mastracci.com)
# 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
5.5然后给该文件执行权限
chmod 777 iterm2-recv-zmodem.sh
6、配置item2–>Profiles–>Edit Profiles -->Profiles–>Triggers–>Edit:
那么Triggers的配置如下所示:
上图三列中,第一列是正则,第二列是触发操作,第三列是脚本路径参数。
- 正则的书写如下:
#第一行正则
rz waiting to receive.\*\*B0100
#第二行正则
\*\*B00000000000000
- 脚本路径如下:
#第一行路径
/usr/local/bin/iterm2-send-zmodem.sh
#第二行路径
/usr/local/bin/iterm2-recv-zmodem.sh
7、命令的使用:
要确保服务器也存在rz命令,如使用yum下载lrzsz命令。yum install lrzsz -y
7.1上传文件:
在服务器的指定上传文件路径执行如下命令:
rz
然后选择mac指定文件即可
7.1下载文件:
在服务器的指定需要下载的文件路径存在文件test.sh
在该路径执行如下命令:
sz test.sh
然后文件存入本地即可。