tags:

views:

802

answers:

5

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.

A: 

Assuming a linux target, put it in your .profile

Adam Liss
Won't work -- see my updated question info.
someguy
+1  A: 

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.

Ken Gentle
Won't work -- see my updated question info.
someguy
Fair enough - I'd go with @geocar's suggestion then.
Ken Gentle
+4  A: 

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.

Robert Gamble
A: 

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.

Elijah
+6  A: 

Put the commands in ~/.ssh/rc

geocar
+1, I knew there had to be a better way to do this.
Robert Gamble
+1, didn't know this existed.
Ken Gentle