종료 시 SSH LocalCommand

종료 시 SSH LocalCommand

SSH를 통해 원격 시스템에 연결할 때마다 지시문을 ~/.ssh/config사용하여 로컬 명령을 실행할 수 있습니다 . LocalCommand하지만 다음과 같은 경우 명령을 어떻게 실행합니까?출구SSH 연결? 연결이 종료되거나 닫힐 때 *.bashrc/.bash_profile* 파일이 소싱되지 않는 것 같습니다.

답변1

로컬 또는 원격 컴퓨터에서 실행하려는 경우 질문에 지정되지 않았습니다. 또한 두 컴퓨터에 어떤 쉘이 있는지 지정되지 않았으므로 두 컴퓨터 모두에 대해 가정합니다 bash.

원격 컴퓨터에서 실행하려면 ~/.bash_logout로그인 셸이 정상적으로 로그아웃될 때 실행되는 을 살펴보세요. 에서 man bash:

~/.bash_logout로그인 셸이 종료되면 bash는 해당 파일 (있는 경우) 에서 명령을 읽고 실행합니다 .

~/.bash_logout로그아웃 중인 셸이 SSH 세션인지 확인하기 위해 테스트를 수행할 수 있습니다. 다음과 같이 작동해야 합니다.

if [[ $SSH_CLIENT || $SSH_CONNECTION || $SSH_TTY ]]; then
    # commands go here
fi

로컬 컴퓨터에서 실행하려면 주위에 함수 래퍼를 만듭니다 ssh. 다음과 같이 작동해야 합니다.

ssh() {
    if command ssh "$@"; then
        # commands go here
    fi
}

이는 귀하의 요구에 비해 너무 간단할 수 있지만 아이디어를 얻을 수 있습니다.

답변2

당신은 올바른 길을 가고 있습니다. 세션 ssh이 로그인 셸(원격 명령 대신) 인 경우 셸을 종료하면 소스 가 bash표시됩니다 ./etc/bash.logout~/.bash_logout

원격 명령을 실행하려면 강제로 bash로그인 쉘이 되도록 할 수 있습니다. 다음 LocalCommand과 유사할 수 있습니다.

bash -l -c /execute/some/command

에서man 1 bash

-c string   If  the  -c  option  is  present, then commands are read from 
string.  If there are arguments after the string, they are assigned to 
the positional parameters,  starting with $0.
-l   Make bash act as if it had been invoked as a login shell 

When  a login shell exits, bash reads and executes commands from the 
files ~/.bash_logout and /etc/bash.bash_logout, if the files exists.

관련 정보