dotfiles/bin/setup-server

67 lines
1.3 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env bash
NORMAL=$(tput sgr0)
GREEN=$(tput setaf 2; tput bold)
YELLOW=$(tput setaf 3)
RED=$(tput setaf 1)
function red() { echo -e "$RED$*$NORMAL"; }
function green() { echo -e "$GREEN$*$NORMAL"; }
function yellow() { echo -e "$YELLOW$*$NORMAL"; }
if [ $# -lt 1 ]; then
echo "Usage: $0 <host|IP> [friendly-name] [user]"
echo "Example: $0 127.0.0.1 server-name $USER"
echo
echo "Afterwards you can login with: ssh server-name"
exit 1;
fi
HOST=$1
NAME=$2
USERNAME=$3
IP=`dig +short $HOST`
if [ -z "$USERNAME" ]; then
USERNAME=$USER
fi
if [ -z "$IP" ]; then
IP=$HOST
fi
if [ -z "$NAME" ]; then
NAME=$HOST
fi
yellow "Setting up server $USERNAME@$HOST with name '$NAME'"
grep "Host $NAME" ~/.ssh/config 2>&1 > /dev/null
if [ 0 -eq $? ]; then
echo $NAME is already in .ssh/config
else
echo Adding server to .ssh/config...
cat >> ~/.ssh/config <<END_OF_HOST
Host $NAME
HostName $IP
User $USERNAME
END_OF_HOST
fi
echo Copying .ssh directory...
scp -qr $HOME/.ssh/ $NAME:
echo Installing dotfiles...
ssh $NAME 'find . -maxdepth 1 -type l -exec unlink {} \;; rm $HOME/.bash*; curl -s https://dot.jacobkiers.net | bash'
echo
green "Done installing server. You can now log in with: ssh $NAME"
green "As a convenience, you will be logged in immediately."
ssh $NAME