iTerm2 셸 통합 에는 it2copy
ssh를 통해 원격 컴퓨터에 로그인 한 경우에도 로컬 클립 보드에 복사 하는 명령 과 같은 멋진 트릭이 있습니다.
임의의 셸 명령을 실행하는 데 사용할 수 있습니까?
예를 들어 ssh를 통해 로그인 한 상태에서 로컬 컴퓨터에서 편집기를 여는 명령을 실행하고 싶습니다. VSCode는 다음 명령을 사용하여 원격 디렉터리를 열 수 있습니다.
code --remote ssh-remote+myserver /home/stuart/some-directory
원격 컴퓨터의 ssh 세션에서 해당 명령을 로컬로 트리거하고 싶습니다.
추신-대안이 있다는 것을 알고 있습니다 . iTerm2의 백 채널을 사용하는 대신 ssh를 통해 명령을 실행하기 위해 로컬 컴퓨터에 다시 (중첩 된) ssh 연결을 만듭니다 . 그러나 그것은 여러 가지 단점이 있으므로이 질문입니다.
또한 이스케이프 코드 ( )와 로컬 명령 ( ) 을 보낼 수 있는의 PermitLocalCommand
옵션을 알고 ~/.ssh/config
있습니다 . 하지만 스크립트 또는 bash 별칭에서 사용할 수있는 솔루션을 찾고 있습니다.~C
!code --remote ...
예를 들어, 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]
# https://stackoverflow.com/questions/61699447
# 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
#
# 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() (
# Tell zsh to use bash-style arrays
setopt ksh_arrays 2> /dev/null || true
CMD=ITERM-TRIGGER-open-with-local-vscode-remote
MACHINE=${LOCALCODE_MACHINE-login1}
FILENAMES=( "$@" )
if [[ ${#FILENAMES[@]} == 0 ]]; then
FILENAMES=.
fi
if [[ ${#FILENAMES[@]} == 1 && -d ${FILENAMES[0]} ]]; then
FILENAMES[0]=$(cd ${FILENAMES[0]}; pwd)
FTYPE=directory
else
# Convert filenames to abspaths
for (( i=0; i < ${#FILENAMES[@]}; i++ )); do
FN=${FILENAMES[i]}
if [[ -f ${FN} ]]; then
DIRNAME=$(cd $(dirname ${FN}); pwd)
FILENAMES[i]=${DIRNAME}/$(basename ${FN})
FTYPE=file
else
1>&2 echo "Not a valid file: ${FN}"
exit 1
fi
done
fi
echo ${CMD} ${FTYPE} ${MACHINE} ${FILENAMES[@]}
)
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() (
# Tell zsh to use bash-style arrays
setopt ksh_arrays 2> /dev/null || true
VSCODE=/usr/local/bin/code
LOGFILE=/tmp/iterm-vscode-trigger.log
FTYPE=$1
MACHINE=$2
FILEPATHS=( "$@" )
FILEPATHS=( "${FILEPATHS[@]:2}" )
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} ${FILEPATHS[@]}"
echo "${TS} ${CMD}" >> ${LOGFILE}
${CMD}
elif [[ "${FTYPE}" == "file" ]]; then
for FN in ${FILEPATHS[@]}; do
CMD="${VSCODE} --file-uri vscode-remote://ssh-remote+${MACHINE}${FN}"
echo "${TS} ${CMD}" >> ${LOGFILE}
${CMD}
done
else
echo "${TS} Error: Bad arguments." >> ${LOGFILE}
exit 1
fi
)
trigger_vscode_remote_editing "$@"
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다