I regularly have to ssh into four or so servers we run, at the same time. I have aliased these like.

alias sshserver1='ssh -t ${SERVER1} "bash -rcfile .my_bashrc"'
# Above line runs my specific bashrc called my_bashrc 
# (this is because many users log in as root, and we 
# have separate bashrcs for each of us. Yes, we should
# have different user accounts etc, but it is what it is).

My ssh is passwordless to these servers.

Currently I do open an instance of gnome-terminal, then tun Ctrl+T four times to open four tabs. Then cycle through them typing sshserver1 in the first, sshserver2 in the second etc. This is annoying.

What I want is to be able to create a function like

function sshallservers()
     #Function to open tabs and ssh into four servers
     1.) Open first tab, which must source specific .my_bashrc 
     to pick up the aliases, run alias sshserver1, thereby 
     ssh-ing to the relevant server.
     2.) Repeat for N servers

So I have tried with

gnome-terminal --tab --command "bash -c 'ssh root@server1'" 
               --tab --command "bash -c 'ssh root@server2'"

And this works as far as opening the tabs and sshing, however it has several problems ...

  1. I do not want to do root@server1, I want to do sshserver1 (defined as an alias in my local .my_bashrc file. So I want somehow this to be sourced and available such that I can use this alias.

  2. When the ssh command has been run, I want it to source the remote .my_bashrc file, this may be achieved just by being able to use the alias as my alias is defined to source this with the -rcfile flag

I cannot find a way to make the new tab source a local bashrc file, and so cannot get my alias working. Since I don't have my alias working, I cannot tell ssh to run the bash -rcfile .my_bashrc command, so the remote bashrc file isn't automatically sourced either.

  • As I remember alias can't use variables inside of it - so use function instead. function sshserv(){gnome-terminal --tab --command "bash -c 'ssh root@$1 "bash -rcfile .my_bashrc"'"} and use as sshserv server1 – Costas Nov 7 '14 at 12:09
  • With the multiple use of double quotes I get "Argument to "--command/-e" is not a valid command: Text ended before matching quote was found for '." (The text was 'bash -c 'ssh root@server1 bash') – Joey O Nov 7 '14 at 12:18
  • Try this --command="bash -c 'ssh root@$1 \"bash -rcfile .my_bashrc\"'" – Costas Nov 7 '14 at 12:53
  • Have you considered tmux? it is a great tool for text-only workers. A bit of a learning curve but it pays off. – Luis Antolín Cano Nov 11 '14 at 21:21
  • @costas, please move your answer to the answers section so that it can be marked as correct, and noticed by others arriving at this post. – Catskul Jan 20 '15 at 20:33

As I remember alias can't use variables inside of it - so use function instead.

function sshserv {
    gnome-terminal --tab --command "
        bash -c 'ssh root@$1 \"bash -rcfile .my_bashrc\"'"

and use as

sshserv server1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.