Created
September 6, 2012 22:01
-
-
Save splitbrain/3660698 to your computer and use it in GitHub Desktop.
CLI tool to create an SSH alias
This file contains 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
# 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