Skip to content

Instantly share code, notes, and snippets.

@hiono
Last active October 24, 2016 14:03
Show Gist options
  • Save hiono/8264752 to your computer and use it in GitHub Desktop.
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
#!/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
@byteshiva
Copy link

$(vagrant global-status | grep $HOME)  returns >> b6a470e  default virtualbox running  /Users/xyzdrv/work/virtbox

if you are aimming to return dir - you would need to add awk '{print $5}'

$(vagrant global-status | grep $HOME | awk '{print $5}')

@andkirby
Copy link

andkirby commented Oct 24, 2016

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

@andkirby
Copy link

andkirby commented Oct 24, 2016

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