How to execute a command every time after ssh'ing from one machine to another?

How to execute a command every time after ssh'ing from one machine to another?

eg

ssh mymachine stty erase ^H 

I would prefer that "stty erase ^ H" be executed every time after my ssh connection is completed.

This command cannot just enter my .zshrc file. that is, for local sessions, I cannot run the command (it tightens my bindings). But I need it to start for my remote sessions.

+6
ssh
source share
6 answers

Put the commands in ~/.ssh/rc

+10
source share

You can put something like this in the shell startup file:

 if [ -n "$SSH_CONNECTION" ] then stty erase ^H end 

The -n test will determine if SSH_CONNECTION is SSH_CONNECTION , which occurs only when logging in through SSH.

+7
source share

If you enter the * nix field with the shell, why not put it in the shell launch?

.bashrc or .profile in most cases.

+1
source share

Suppose the linux target is placed in your .profile

0
source share

Try adding a command below the end of ~ / .bashrc. It must be removed upon logout. Do you want this command to be executed only when you exit an ssh session? What about local sessions, etc.?

 trap 'stty erase ^H; exit 0' 0 

You can probably configure the .logout file from / etc / profile using the same template.

0
source share

The answer for us screen / byobu users:

The geocar solution will not work, as the screen will complain that it should be connected to the terminal. (This is likely due to the fact that .ssh / rc processing is processed before the shell starts. See the INPUT PROCESS section from man 8 sshd ).

Robert's solution here is better, but since screen and byobu open his own instance of bash, we need to avoid infinite recursion. So, the convenient version has been edited here:

 ## RUN BYOBU IF SSH'D ## ## '''''''''''''''''' ## # (but only if this is a login shell) if shopt -q login_shell then if [ -n "$SSH_CONNECTION" ] then byobu exit fi fi 

Note that I also added exit after byobu , since IMO, if you use byobu in the first place, you usually don't want to do anything outside of it.

0
source share

All Articles