iTerm2:如何从远程会话中触发本地命令?外壳集成 有一些整洁的技巧,例如它的 it2copy
命令,即使我通过ssh登录到远程机器,它也会复制到本地剪贴板中。
它可以用来运行任意的shell命令吗?
例如,当我通过ssh登录时,我想在本地机器上执行一个命令来打开一个编辑器。 VSCode可以用这个命令打开一个远程目录。
code --remote ssh-remote+myserver /home/stuart/some-directory
我想从远程机器上的ssh会话在本地触发这个命令。
PS--我知道有一个替代方法。创建一个(嵌套的)ssh连接 后面 到我的本地机器上通过ssh执行命令,而不是使用iTerm2的后道。 但这有各种缺点,所以才有这个问题。
我也知道 PermitLocalCommand
选项中 ~/.ssh/config
,这让我可以发送一个转义码(~C
),然后是一条本地命令(!code --remote ...
). 但我希望有一个解决方案,我可以在脚本或bash别名中使用。
例如,如果 it2local
存在,我会这样使用它。
alias code_here='it2local "code --remote ssh-remote+$(uname -n) $(pwd)"'
如果仅靠ssh就能实现的话,我很乐意听到这个消息。
正确的方法是通过 iTerm2触发器它可以在终端输出中出现特定模式时运行任意命令(以及其他选项)。
假设的 it2local
我上面描述的命令只需要将一些预定义的触发模式和你要执行的命令一起回传到你的终端。
在我的例子中,我没有实现通用的 it2local
命令。(也许我以后会更新这个答案。)现在,我已经实现了一个脚本,为我的特定使用情况服务。用VSCode打开一个远程文件 我正在使用的代码如下所示。
#!/bin/sh
#
# This file contains the code and instructions to set up an iTerm2 "Trigger" from a
# remote ssh session that will open up VSCode on your local machine to edit a
# file on the remote server over ssh.
#
# Author: Stuart Berg
# https://github.com/stuarteberg
# [email protected]
# SETUP OVERVIEW
# --------------
# - Install the VS Code Remote Development Extension Pack
# - Ideally, setup passwordless ssh access to the remote machines you want to access
# - Place this script somewhere on your local machine (and make sure it's executable).
# - Copy the localcode() shell function below into your remote machine's .bashrc
# - Define the Trigger in iTerm2 as defined below.
#
# Notes:
# Docs for iTerm2 Triggers: https://iterm2.com/documentation-triggers.html
# Docs for VSCode Remote Extension: https://code.visualstudio.com/docs/remote/remote-overview
# - CLI: https://github.com/microsoft/vscode-remote-release/issues/585#issuecomment-536580102
# iTerm2 Preferences Setup
# ------------------------
#
# In your iTerm2 preferences, set up a Trigger (Profiles > Advanced > Triggers > Edit)
#
# Regular Expression: .*ITERM-TRIGGER-open-with-local-vscode-remote ([^ ]+) ([^ ]+) ([^ ]+)
# Action: Run Command...
# Parameters: /path/to/this/script \1 \2 \3
#
# Tip: For additional feedback, try adding a duplicate entry with a "Post Notifcation" action.
# HOW TO TEST
# -----------
#
# NOTE: The new trigger will not be active for already-open terminal sessions.
# Open a new terminal after you add the trigger to your preferences.
#
# To test it, ssh into the remote machine, and try the 'localcode' function:
#
# localcode .
# localcode /some/dir
# localcode /some/file
# localcode /some/file remote-machine-name
#
# If something is going wrong, inspect /tmp/iterm-vscode-trigger.log
#
# Put this in your remote ~/.bashrc
#
localcode() (
CMD=ITERM-TRIGGER-open-with-local-vscode-remote
MACHINE=${2-$(uname -n)}
FILENAME=${1-.}
if [[ -d ${FILENAME} ]]; then
FILENAME=$(cd $FILENAME; pwd)
FTYPE=directory
elif [[ -f ${FILENAME} ]]; then
DIRNAME=$(cd $(dirname $FILENAME); pwd)
FILENAME=${DIRNAME}/$(basename ${FILENAME})
FTYPE=file
else
1>&2 echo "Not a valid file or directory: ${FILENAME}"
exit 1
fi
echo ${CMD} ${FTYPE} ${MACHINE} ${FILENAME}
)
export -f localcode
#
# Copy this whole file onto your local machine, or at least the following lines.
# Make sure it is executable (chmod +x /path/to/this/script)
#
trigger_vscode_remote_editing() (
VSCODE=/usr/local/bin/code
LOGFILE=/tmp/iterm-vscode-trigger.log
FTYPE=$1
MACHINE=$2
FILEPATH=$3
TS="["$(date "+%Y-%m-%d %H:%M:%S")"]"
echo "${TS} Triggered: ""$@" >> ${LOGFILE}
# https://github.com/microsoft/vscode-remote-release/issues/585#issuecomment-536580102
if [[ "${FTYPE}" == "directory" ]]; then
CMD="${VSCODE} --remote ssh-remote+${MACHINE} ${FILEPATH}"
elif [[ "${FTYPE}" == "file" ]]; then
CMD="${VSCODE} --file-uri vscode-remote://ssh-remote+${MACHINE}${FILEPATH}"
else
echo "${TS} Error: Bad arguments." >> ${LOGFILE}
exit 1
fi
echo "${TS} ${CMD}" >> ${LOGFILE}
${CMD}
)
trigger_vscode_remote_editing "$@"