Last active
July 27, 2016 02:41
-
-
Save allex/0f844bbe583a1e40548d to your computer and use it in GitHub Desktop.
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
# Extends with specified shortcut completion with host prefix and default user | |
# Author: Allex Wang ([email protected]) | |
# MIT Licensed | |
# GistID: 0f844bbe583a1e40548d | |
# GistURL: https://gist.github.com/allex/0f844bbe583a1e40548d | |
ssh() { | |
local user= | |
local host=$1 | |
local m=`grep -Po '\w+@\w+' <<< "$host"` | |
if [ "$m" ]; then | |
read user host <<< `echo $m|tr '@' ' '` | |
fi | |
if [[ "$host" =~ ^[0-9]+$ ]]; then | |
local x k v u prefix | |
local config=$HOME/.ssh/config | |
if [[ -r $config ]]; then | |
local fix=1 | |
while read t k v | |
do | |
if [ "$t" == "#def" ]; then | |
case "$k" in | |
USERNAME) user=${user:-$v} ;; | |
PREFIX) prefix=$v ;; | |
esac | |
elif [[ "$t" == "Host" && "$k" == "$host" ]]; then | |
fix=0 | |
break | |
fi | |
done < "$config" | |
# auto fix the default host prefix and user if not config specified. | |
if [ $fix -eq 1 ]; then | |
set -- "$user@$prefix.$host" | |
fi | |
fi | |
fi | |
if type -t ssh-pearl >/dev/null 2>&1; then | |
ssh-pearl "$@" | |
else | |
/usr/bin/ssh "$@" | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment