Last active
October 24, 2016 14:03
-
-
Save hiono/8264752 to your computer and use it in GitHub Desktop.
Add a vagrant ssh-config data to ".ssh/config" file. Require plugin: vagrant-global-status
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
SSH=$HOME/.ssh | |
[ ! -f $SSH/config ] && touch $SSH/config | |
[ -f $SSH/config ] && [ ! -f $SSH/config.orig ] && mv $SSH/config{,.orig} | |
rm -f $SSH/config.vagrant | |
touch $SSH/config.vagrant | |
for dir in $(vagrant global-status | grep $HOME) | |
do | |
pushd $dir > /dev/null | |
vagrant ssh-config --host $(basename $dir) >> $SSH/config.vagrant | |
popd > /dev/null | |
done | |
cat $SSH/config.orig $SSH/config.vagrant > $SSH/config |
Another approach within Vagrant directory :)
#!/usr/bin/env bash
# This file can generate SSH configuration in `~/.ssh/config` file for current Vagrant instance.
# Please put this file in the same directory of Vagrant file.
# Using:
# # by default, base hostname will be taken from config.vm.hostname or directory name
# $ vagrant_ssh_config.sh
# # The same but with additional aliases
# $ vagrant_ssh_config.sh host.alias.1 host.alias.2
SSH=$HOME/.ssh
if [ -f ${SSH}/config ] && [ ! -f ${SSH}/config.orig ]; then
mv ${SSH}/config{,.orig} && \
echo 'Exists ~/ssh/config file stored in ~/.ssh/config.orig. Please use it for further custom changes.'
elif [ ! -f ${SSH}/config ]; then
touch ${SSH}/config
fi
dir=$(pwd)
# Define hostname
# If there is no declaration config.vm.hostname in Vagrant file, it will use directory name
hostname=$(cat ${dir}/Vagrantfile | grep 'config.vm.hostname = ' | cut -d '=' -f 2 | tr -d "'"'"' | xargs)
[ -z "${hostname}" ] && hostname=$(basename ${dir})
# add aliases from arguments list
hostname+=" $@"
pushd ${dir} > /dev/null
vagrant ssh-config --host "${hostname}" > ${SSH}/config.$(echo ${dir} | tr '/\\' '-').vagrant
popd > /dev/null
cat ${SSH}/config.orig ${SSH}/config.*.vagrant > ${SSH}/config
It's allowed to add some hostname aliases.
vagrant_ssh_config.sh host.alias.1 host.alias.2
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @hiono,
I've made some optimizations.
grep
'ing any path (not only home directory, it doesn't work for me, for instance).