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 hidden or 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 |
Hi @hiono,
I've made some optimizations.
- Minor code flow changes.
- Added
grep
'ing any path (not only home directory, it doesn't work for me, for instance).
#!/usr/bin/env bash
SSH=$HOME/.ssh
[ ! -f ${SSH}/config ] && touch ${SSH}/config
if [ ! -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.'
fi
# erase file config.vagrant
echo > ${SSH}/config.vagrant
for dir in $(vagrant global-status | grep -E ':?/[A-z]+' | awk '{print $5}')
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
if you are aimming to return dir - you would need to add awk '{print $5}'