Skip to content

@splitbrain /sbm.sh
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
CLI tool to create an SSH alias
# SSH bookmark creator
sbm(){
if [ $# -lt 2 ]; then
echo "Usage: sbm <short> [<user>@]<hostname> [-p <port>]" >&2
return 1
fi
short=$1
arg=$2
if $(echo "$arg" | grep '@' >/dev/null); then
user=$(echo "$arg"|sed -e 's/@.*$//')
fi
host=$(echo "$arg"|sed -e 's/^.*@//')
if [ "$3" == "-p" ]; then
port=$4
fi
if $(grep -i "host $short" "$HOME/.ssh/config" > /dev/null); then
echo "Alias '$short' already exists" >&2
return 1
fi
if [ -z "$host" ]; then
echo "No hostname found" >&2
return 1
fi
echo >> "$HOME/.ssh/config"
echo "host $short" >> "$HOME/.ssh/config"
echo " hostname $host" >> "$HOME/.ssh/config"
[ ! -z "$user" ] && echo " user $user" >> "$HOME/.ssh/config"
[ ! -z "$port" ] && echo " port $port" >> "$HOME/.ssh/config"
echo "added alias '$short' for $host"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.