0

I created a function in my bashrc file so that when i ssh into something it automatically opens a tmux session.

function ssht () {    
    /usr/bin/ssh $@ -t 'tmux a || tmux || /bin/bash'
}

This works great, but i don't want to use ssht, i want to just use ssh. When i change the function from ssht to ssh tmux doesn't open but it does successfully ssh into the machine.

I assume it isn't working because when i call ssh it ignores the function in the bashrc file and instead runs ssh.

Is there some way i can just use "ssh" instead of "ssht"

cwiggs
  • 69
  • 1
  • 4

1 Answers1

4

SSH Bash Function

Making an ssh function to run something on login like you did, you can do the following:

function ssht () {    
    \ssh $@ -t 'tmux a || tmux || /bin/bash'
}

alias ssh=ssht
  • Adding a \ in front of the ssh temporarily disables the alias.
  • The ssht function stays the same but we add an alias to ssh that point to it.
  • DOWNSIDE: other things may break, good example is tab-autocompleting hosts out of your ssh config

SSH Command (Alternative)

Alternatively you could use SSH's built-in way of doing this by appending Command="tmux" in front of your public key in the authorized_keys file. It can be used as follows:

command="echo howdy" ssh-rsa AAAAB3...
c4urself
  • 5,270
  • 3
  • 25
  • 39
  • Whilst this may theoretically answer the question, please provide some more context context around that code so others will have some idea what it is and why it makes the difference. – HBruijn Jan 06 '15 at 07:35
  • That was a really clever solution! – Jenny D Jan 06 '15 at 17:57
  • This function worked great until i tried to ssh into an OpenWRT router. The problem is that OpenWRT doesn't have /bin/bash, instead it has /bin/sh. In order to fix the problem you just add `|| /bin/sh` to the end of the function. Therefore it'll look like: `function ssht () { \ssh $@ -t 'tmux a || tmux || /bin/bash || /bin/sh' } alias ssh=ssht` – cwiggs Feb 24 '15 at 22:10
  • Can you explain why exactly interactive ssh fails when calling it in a bash function, but succeeds when you alias that function? Seems strange. – Luke Davis Feb 28 '18 at 21:50