Add checks to setup script

This commit is contained in:
Kevin Baensch 2022-11-18 16:20:24 +01:00
parent 8b9850a003
commit df4ce2a4b6

View file

@ -1,21 +1,23 @@
#!/usr/bin/env bash #!/usr/bin/env bash
PROJECT_PATH=$(realpath $(dirname $0)) PROJECT_PATH=$(realpath $(dirname $0))
which myssh > /dev/null 2>&1 # Always copy newest version to bin
if [ $? -eq 1 ] mkdir -p $HOME/bin
cp $PROJECT_PATH/script/myssh $HOME/bin/myssh
# Detect Shell Init Path
if [[ $SHELL =~ bin/bash$ ]]
then then
mkdir -p $HOME/bin RC_FILE=.bashrc
cp $PROJECT_PATH/script/myssh $HOME/bin/myssh elif [[ $SHELL =~ bin/zsh$ ]]
if [[ ! $PATH =~ $HOME/bin ]] then
then RC_FILE=.zshrc
if [[ $SHELL =~ bin/bash$ ]] fi
then
echo -e 'PATH=$PATH:$HOME/bin' >> $HOME/.bashrc grep -qe '^PATH=$PATH:$HOME/bin$' $HOME/$RC_FILE 2> /dev/null
elif [[ $SHELL =~ bin/zsh$ ]] if [ ! -z $HOME/$RC_FILE ] && [[ ! $PATH =~ $HOME/bin ]] && [ $? -ne 0 ]
then then
echo -e 'PATH=$PATH:$HOME/bin' >> $HOME/.zshrc echo -e 'PATH=$PATH:$HOME/bin' >> $HOME/$RC_FILE
fi
fi
fi fi
if [ ! -f $PROJECT_PATH/etc/ssh/ssh_host_ed25519_key ] if [ ! -f $PROJECT_PATH/etc/ssh/ssh_host_ed25519_key ]
@ -27,11 +29,25 @@ fi
read -r -p "Auto generate client keys+config? [Y/n] " GEN_KEYS read -r -p "Auto generate client keys+config? [Y/n] " GEN_KEYS
case $GEN_KEYS in case $GEN_KEYS in
[yY]*) [yY]*)
mkdir -p ~/.ssh mkdir -p $HOME/.ssh
read -r -p "Key Name (should not already exist in ~/.ssh): " KEY_NAME read -r -p "Key Name (will not be overridden if it already exists in ~/.ssh): " KEY_NAME
ssh-keygen -t ed25519 -f ~/.ssh/$KEY_NAME.key -C "$(date --iso-8601)_$(whoami)@$HOST" # Only add key if it does not already exist
if [ ! -f $HOME/.ssh/$KEY_NAME.key ]
then
ssh-keygen -t ed25519 -f $HOME/.ssh/$KEY_NAME.key -C "$(date --iso-8601)_$(whoami)@$HOSTNAME"
fi
read -r -p "Target Host: " HOST_NAME read -r -p "Target Host: " HOST_NAME
echo -ne "\n\nHost $HOST_NAME\n User sqlproxy\n IdentityFile ~/.ssh/$KEY_NAME.key" >> ~/.ssh/config # Check if there is an entry for $HOST_NAME in the users ssh config
echo -e command=\"/sqlproxy_cli.sh\" $(cat ~/.ssh/$KEY_NAME.key.pub) >> $PROJECT_PATH/etc/ssh/.ssh/authorized_keys;; grep -qe "^Host $HOST_NAME$" $HOME/.ssh/config
if [ $? -ne 0 ]
then
echo -ne "\n\nHost $HOST_NAME\n User sqlproxy\n IdentityFile ~/.ssh/$KEY_NAME.key" >> $HOME/.ssh/config
fi
# Check if public key is already in the containers authorized_keys file
grep -qe "$(cat $HOME/.ssh/$KEY_NAME.key.pub)$" $PROJECT_PATH/etc/ssh/.ssh/authorized_keys
if [ $? -ne 0 ]
then
echo -e command=\"/sqlproxy_cli.sh\" $(cat $HOME/.ssh/$KEY_NAME.key.pub) >> $PROJECT_PATH/etc/ssh/.ssh/authorized_keys
fi;;
*) echo -e "Not generating client ssh key.\nPlease put your desired public keys into $PROJECT_PATH/etc/ssh/.ssh/authorized_keys\nAlso add 'command=\"/sqlproxy_cli.sh\" ' in front of your key";; *) echo -e "Not generating client ssh key.\nPlease put your desired public keys into $PROJECT_PATH/etc/ssh/.ssh/authorized_keys\nAlso add 'command=\"/sqlproxy_cli.sh\" ' in front of your key";;
esac esac