解决item2+zsh使用lrzsz后在远端服务器不能使用rz的问题

4,777 阅读1分钟

使用item2实现快速登录远端服务器

新建一个脚本文件 loginBaiDuYun.exp

set HOST xxx.xxx.xxx.xxx
set USER xxxxxxx
set PASSWORD XXXXX

#连接远端服务器 
spawn ssh $USER@$HOST

#自动输入
expect {
	"(yes/no)?"
 	{send "yes\n";exp_continue}
	"password:"
	{send "$PASSWORD\n"}
}

interact

在item2中进行配置快速登录远端服务器

image.png

使用Homebrew安装lrzsz用于上传下载文件

brew install lrzsz

在终端使用 brew list lrzsz 查看安装的路径

image.png

创建执行文件 iterm2-recv-zmodem.sh

cd /usr/local/bin/

touch iterm2-recv-zmodem.sh

vim 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 这个需要根据自己安装的文件位置编写
    /usr/local/bin/rz -E -e -b
    sleep 1
    echo
    echo
    echo # Sent -> $FILE
fi

创建执行文件 iterm2-send-zmodem.sh

touch iterm2-send-zmodem.sh

vim iterm2-send-zmodem.sh

输入以下内容

#!/bin/bash

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 这个需要根据自己安装的文件位置编写
	/usr/local/bin/sz "$FILE" --escape --binary --bufsize 4096
	sleep 1
	echo
	echo \# Received $FILE
fi

授予可执行权限

chmod +x iterm2-send-zmodem.sh 
chmod +x iterm2-recv-zmodem.sh 

完成上述配置后

image.png

双击linbaiduyun或者使用自己定义的快捷键可以快速打开远端服务器

image.png

现在使用rz会 一直显示 waiting to receive.**B0100000023be50 并不会打开文件窗口

问题 是因为使用了expect导致rz失效 解决 新增一个脚本文件 login.sh

#!/bin/sh

basepath=$(cd `dirname $0`; pwd)

# 增添这个配置
export LC_CTYPE=en_US

#expect脚本所在位置
filepath=$1
if [ -f $filepath ]; then
  expect $filepath
else
  echo "$filepath not exits"
fi

image.png

配置Trigger

image.png

# 发送 sz
Regular expression: rz waiting to receive.\*\*B0100 
Action: Run Silent Coprocess
Parameters: /usr/local/bin/iterm2-send-zmodem.sh

# 接收 rz
Regular expression:\*\*B00000000000000
Action: Run Silent Coprocess
Parameters: /usr/local/bin/iterm2-recv-zmodem.sh

image.png

然后再登录远端服务器的时候直接使用login的这个perfile就OK啦

image.png