Skip to content

Instantly share code, notes, and snippets.

@syabruk
Created November 3, 2012 22:59
Show Gist options
  • Save syabruk/4009224 to your computer and use it in GitHub Desktop.
Save syabruk/4009224 to your computer and use it in GitHub Desktop.
Manage ssh connection information
#!/bin/bash
SSHC_PATH=$HOME'/.sshc'
if [ ! -d $SSHC_PATH ] ; then
mkdir "$SSHC_PATH"
fi
case "$1" in
-a | --add)
touching_name="$2"
touching_env="$3"
touching_entrance="$4"
touching_file=$SSHC_PATH'/'$touching_name
touch "$touching_file"
if grep -q '^'$touching_env'=' $touching_file ; then
sed -i.bak '
/^'$touching_env'=.*/ {
c\
'$touching_env'='$touching_entrance'
}' $touching_file
echo "Change enviroment '$touching_env' to '$touching_name'"
else
echo $touching_env'='$touching_entrance >> $touching_file
echo "Added new enviroment '$touching_env' to '$touching_name'"
fi
exit 0
;;
-l | --list)
for file in $(find $SSHC_PATH -maxdepth 1 -type f \! -name "*.*") ; do
echo $(basename $file)":"
for line in $(cat $file ) ; do
echo " "$line
done
done
exit 0
;;
-v | --version)
echo "Version 1.0"
exit 0
;;
-h | --help)
# TODO
echo $0" usage information"
exit 0
;;
*)
touching_name="$1"
touching_file=$SSHC_PATH'/'$touching_name
if [ -f $touching_file ] ; then
if [ $2 ] ; then
touching_env="$2"
else
touching_env="global"
fi
touching_text=$(cat $touching_file)
found_entrence=$([[ $touching_text =~ $touching_env=([^[:space:]]*) ]]; echo -n "${BASH_REMATCH[1]}")
ssh "$found_entrence"
else
echo "ERROR"
exit 1
fi
exit 0
;;
esac
Usage:
./sshc.sh --add server_name development me@server.com
./sshc.sh --add server_name test test@test
./sshc.sh --add my_awesome_server global now@127.0.0.1
./sshc.sh server_name test #=> ssh test@test
./sshc.sh my_awesome_server #=> ssh now@127.0.0.1
./sshc.sh -l
my_awesome_server:
global=now@127.0.0.1
server_name:
development=me@server.com
test=test@test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment