4

I have a script that starts my vagrant machine, opens up multiple terminals and connects to the vagrant machine via ssh in every newly opened terminal. My problem is that I need about five terminals, and I don't want to type in the password for each terminal manually. Is there a way to get prompted for the password only once in the main terminal, and use the same password for the ssh command?

#!/bin/bash
cd /home/kkri/public_html/freitag/vagrant
vagrant up
for run in $(seq 1 $1)
 do
  gnome-terminal --window-with-profile=dark -e "ssh vagrant@localhost -p 2222" --$
 done
gnome-terminal --window-with-profile=git          
clear
echo "~~~ Have fun! ~~~"
  • 12
    Why don't you set up passwordless access and use ssh keys instead? That's both more secure and simpler. Would that be an acceptable solution? – terdon Sep 5 '16 at 9:16
  • As terdon said, you can use a private/public SSH key; this will also make actions like scp more comfortable without compromising security. The following link provides a HowTo that should be applicable to your remote system, too. If you trust your environment enough (firewalled intranet) you can even generate a key without passphrase. linode.com/docs/security/use-public-key-authentication-with-ssh – Murphy Sep 5 '16 at 9:35
  • 3
    There is such as cssh and pssh (Cluster SSH respectively Parallel SSH) – Valentin Bajrami Sep 5 '16 at 9:36
  • As an aside: Why use ssh vagrant@localhost -p 2222 rather than vagrant ssh? The latter is better as it will correct the port if you have more then one vagrant box running. – Michael Daffin Sep 5 '16 at 9:53
  • Use vagrant ssh!! – Boris the Spider Sep 5 '16 at 11:08
8

In general (ignoring vagrant or other system-specific details) your best bet is to set up authentication with SSH keys, and run ssh-agent. Then open the ssh sessions with something like:

# load the key to the agent with a 10 s timeout
# this asks for the key passphrase
ssh-add -t10  ~/.ssh/id_rsa  
for x in 1 2 3 ; do 
    ssh .... 
done

Or, if you can't use keys, you could rig something up with sshpass.

read -p "Enter password: " -s SSHPASS ; echo
for x in 1 2 3 ; do 
    sshpass -e ssh ...
done
unset SSHPASS

Though with the terminal in the middle, this would leave the password set in the terminal's environment. To work around that, you could save the password temporarily in a file:

read -p "Enter password: " -s SSHPASS ; echo
PWFILE=~/.ssh/secret_password
cat <<< "$SSHPASS" > "$PWFILE"
unset SSHPASS
for x in 1 2 3 ; do 
    sshpass -f "$PWFILE" ssh ...
done
shred --remove "$PWFILE"

This still isn't optimal since there's a chance the password hits the disk, so keys would be better.

3

Since you are using vagrant you can simply run vagrant ssh to ssh to the box without having to enter a password. This will also handle port collisions if you have more then one vagrant box running and various other things.

Alternatively you can use tmux instead of gnome-terminals and make use of its synchronize-panes feature:

#!/bin/bash
tmux_name="vagrantssh-$BASHPID"
tmux new -s $tmux_name -d
for run in $(seq 1 $1); do
    tmux splitw -t $tmux_name -v "ssh vagrant@localhost -p 2222"
    tmux select-layout -t $tmux_name tiled
done
tmux kill-pane -t 1
tmux set-window-option -t $tmux_name synchronize-panes on
tmux select-layout -t $tmux_name tiled
tmux new-window -t $tmux_name
tmux kill-window
tmux attach -t $tmux_name

This will launch tmux and run the ssh command in the number of panes that you have specified, with all the panes synchronized (tmux forwards keys to all the panes). Once you have done what you want to all the panes you can press ctrl+b then :set synchronize-panes off to turn off the input sync. This can be bound to a key for convenience.

  • 1
    tmux seems like a good idea, however, I'm wondering why you would want multiple SSH connections instead of simply one connection and running tmux on the vagrant machine? – tmh Sep 5 '16 at 18:26
  • That also works, assuming you have it installed inside vagrant - not always the case. You also get to use your tmux config if you have modified it at all without needing to copy it to every vagrant box you provision. This script was also adapted from a script I used to tmux to multiple different hosts so is more flexible in that regard. – Michael Daffin Sep 6 '16 at 9:24
  • Also, you can modify the script to start some panes not inside vagrant and have them all managed by one tmux session. – Michael Daffin Sep 6 '16 at 9:25
3

The approach would be to use key authentication, it is safer, and solves the authentication problem.

Just generate a key pair with:

ssh-keygen -t rsa

And copy/paste the content of ~/.ssh/id_rsa.pub file to the ~/.authorized_keys in the server machine. If the file doesn't exist, check the permissions to user only.

If you use multiples terminals I recommend terminator, it actually solves this kind of problem with broadcast keybord to all terminals features

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.