The official documentation for Iterm2 is an introduction to Triggers

background

Uploading files from the local PC to the server looks like this:

  1. You can log in to the server directlyscprsyncUpload and download files directly from local computer
  2. After logging in to the jumper, log in to the server from the jumperscprsyncUpload and download files from the local PC to the jumper, and use them from the jumperscprsyncUpload and download files to the target server
  3. After logging in to the jumper (the jumper has limited permissions, such as only a menu to select which server to jump to), log in to the server from the jumper

In the third case above, the jumpers are not used as a file transfer station as in the second case, and you need to upload files directly from the local computer to the server. This involves installing iTerm2 on Mac OSX and using RZ SZ to upload and download iTerm2 (with Homebrew configuration). Rz and sZ commands can be used.

For the current terminal window, whether it is currently on a local computer or a remote server, RZ means receiving files. For example, in the terminal window, enter Rz and press Enter.

How do I use RZ

**B0100000023be50 **B0100000023be50 **B0100000023be50 **B0100000023be50 You can now send the file to the directory of the environment in which the Rz command was executed (if rZ was executed locally, the folder in which rZ was executed locally).

Using the sZ command manually is not operator friendly, we want to pop up the file selection window. We do this through a shell script: pop up the file selection window + send the file after the file is selected. Iterm2-send-zmodem. sh: iterm2-send-zmodem.sh:

#! /bin/bash # This script is from Github and deletes some ** comments. 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  /opt/homebrew/bin/sz "$FILE" --escape --binary --bufsize 4096 sleep 1 echo echo \# Received $FILE fiCopy the code

As you can see, if you execute the script directly (assuming no parameters), the first step is to go to the else above and do a prompt, that is, open a file selection window to select the file. Because the script is synchronous, if you click Cancel after selecting the file, The ‘if’ is entered, which responds to ‘Cancelled’. If the file is selected, the ‘else’ is entered, which executes the sZ command to send the file.

Brew Install LRZSZ (brew install LRZSZ, brew Install LRZSZ, Brew Install LRZSZ, brew Install LRZSZ, brew Install LRZSZ (brew Install LRZSZ, Brew Install LRZSZ)) The paths are /opt/homebrew/bin/sz and /opt/homebrew/bin/rz.

You can check the chip by clicking on the Apple icon in the upper left corner of the computer -> About this machine, and further check the path by using the which sz which rz command. If the path is different, you need to manually modify the path in the script above. Otherwise, some students will report errors when executing, can not find the SZ command and other errors.

Set the triggers

The iterm2-send-zmomodem. Sh script must be executed manually after each rZ return. And because the last rz command has not finished, the script cannot be executed in the current session window. It is best to trigger the script automatically. This introduces the Triggers function of Iterm2.

A trigger is an action that is performed when text matching some regular expression is received in a terminal session.

Trigger is a trigger that, when a string appears in an Iterm2 session window (either by hand or by code printing), determines whether the string matches one of the trigger’s preset regex conditions. The corresponding action specified in the trigger setting is executed (there are many, such as highlighting, ringing a bell, executing a script, etc.).

Rz waiting to receive.**B0100000023be50 Execute our iterm2-send-zmomodem. Sh script.

Install iTerm2 on Mac OSX and use RZ SZ to upload and download iTerm2 (with Homebrew configuration).

In addition, you can set any trigger. For example, click the + sign to add a trigger, set the regular rule to 123, and set the action to Hilight Line red. This will highlight the Line red whenever a string containing 123 is entered at the terminal.

Set the small pit when triggers

This is a pit for iterm2’s default profile. You can set a profile to the default configuration in iter2 so that new Windows (including those using other profiles) are loaded based on its configuration.

If the trigger is not set to the default profile as documented, the trigger configuration will not be loaded when a new window opens, and the re will never fire and the script will not be executed as expected.

The default profile (the list of profiles is automatically prefixed by a star) is the one whose name is default when iterm2 is installed. However, the profile name can be changed as well. Make sure that the profile is configured with a star.

Item2 How to set a profile as default: Select a profile and select Set as default in Other Actions.

The flow when rZ is used on the server

  • Log in to the server in the terminal session window of Iterm2
  • The inputrzenter
  • The session window immediately prints a stringrz waiting to receive.**B0100000023be50
  • Trigger executioniterm2-send-zmodem.shThe script
  • Popup file selection window, select file
  • Select the file and trigger the sz command in the script to transfer the file
  • Is transferred

About other unused scripts and commands

sz

To execute sZ directly is to send a file, which is followed by parameters.

iterm2-send-zmodem.sh

Run the /usr/local/bin/iterm2-send-zmode. sh command on the terminal. After the window is displayed, **B00000000000000 is printed. /usr/local/bin/iterm2-recv-zmodem.sh This script is to select which folder to put the target file and then execute the rz command in the script to establish a file transfer channel to receive the file.

iterm2-recv-zmodem.sh

Run the /usr/local/bin/iterm2-recv-zmodem.sh command on the terminal to select the directory for storing the received files. After selecting the directory, run the rz command on the terminal. However, when you run the rz command on the terminal, the current folder is selected as the path for receiving files by default.

other

How to open the bin directory on MAC Finder: The /usr/local directory on Mac is hidden from Finder by default. If you need to go to /usr/local, open Finder and use Command + Shift +G to fill in /usr/local in the directory that pops up.

SSH login When you use config to configure the pem file, other users cannot even have the r permission for the pem file.