someguy
someguy

Reputation: 3389

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

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

e.g

ssh mymachine
stty erase ^H

I'd rather just have "stty erase ^H" execute every time after my ssh connection completes.

This command can't simply go into my .zshrc file. i.e. for local sessions, I can't run the command (it screws up my keybindings). But I need it run for my remote sessions.

Upvotes: 7

Views: 3771

Answers (6)

Alois Mahdal
Alois Mahdal

Reputation: 11243

An answer for us, screen/byobu users:

The geocar's solution will not work as screen will complain that "Must be connected to a terminal.". (This is probably caused by the fact that .ssh/rc is processed before shell is started. See LOGIN PROCESS section from man 8 sshd).

Robert's solution is better here but since screen and byobu open it's own bash instance, we need to avoid infinite recursion. So here is adjusted byobu-friendly version:

## 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 normally don't want to do anything outside of it.

Upvotes: 0

geocar
geocar

Reputation: 9305

Put the commands in ~/.ssh/rc

Upvotes: 12

Robert Gamble
Robert Gamble

Reputation: 109022

You can put something like this into your shell's startup file:

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

The -n test will determine if SSH_CONNECTION is set which happens only when logged in via SSH.

Upvotes: 7

Elijah
Elijah

Reputation: 13604

Try adding the command below the end of your ~/.bashrc. It should be exited upon logoff. Do you want this command only executed when logging off a ssh session? What about local sessions, etc?

trap 'stty erase ^H; exit 0' 0

You probably could setup a .logout file from /etc/profile using this same pattern as well.

Upvotes: 0

Ken Gentle
Ken Gentle

Reputation: 13357

If you're logging into a *nix box with a shell, why not put it in your shell startup?

.bashrc or .profile in most cases.

Upvotes: 1

Adam Liss
Adam Liss

Reputation: 48290

Assuming a linux target, put it in your .profile

Upvotes: 0

Related Questions