Created
September 30, 2023 00:44
-
-
Save chriscz/ee552707efc142844c049b1778e99957 to your computer and use it in GitHub Desktop.
Bash script for speeding up vagrant ssh
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 | |
SOURCE=${BASH_SOURCE[0]} | |
while [ -L "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink | |
DIR=$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd ) | |
SOURCE=$(readlink "$SOURCE") | |
[[ $SOURCE != /* ]] && SOURCE=$DIR/$SOURCE # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located | |
done | |
SCRIPT_DIR=$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd ) | |
set -eou pipefail | |
# set -x | |
CONFIG="${SCRIPT_DIR}/.sshconfig" | |
vagrant_ssh () { | |
vagrant_ssh_prepare | |
ssh -F "${CONFIG}" default "$@" | |
} | |
vagrant_ssh_prepare () { | |
if ! vagrant_ssh_config_valid; then | |
vagrant_gen_ssh_config | |
fi | |
} | |
vagrant_ssh_config_valid () { | |
if [ -f "${CONFIG}" ]; then | |
host=$(cat "${CONFIG}" | grep 'HostName' | xargs | cut -d ' ' -f 2) | |
port=$(cat "${CONFIG}" | grep 'Port' | xargs | cut -d ' ' -f 2) | |
nc -w 0.5 "${host}" "${port}" < /dev/null &> /dev/null | |
else | |
return 1 | |
fi | |
} | |
vagrant_gen_ssh_config () { | |
vagrant ssh-config > "${CONFIG}" | |
} | |
vagrant_ssh "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment