better setup script
This commit is contained in:
parent
fe0aa9e412
commit
dd4fc8d0b2
@ -1,67 +1,94 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
if [[ $EUID -ne 0 ]]; then
|
read -p "Did you run this script on the remote host? " -n 1 -r
|
||||||
|
echo
|
||||||
|
if [[ ! $REPLY =~ ^[Yy]$ ]]
|
||||||
|
then
|
||||||
|
echo "Please rerun this script on the remote host as root user."
|
||||||
|
[[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
read -p "Did you setup various targets and adjusted configurations as described in README?" -n 1 -r
|
||||||
|
echo
|
||||||
|
if [[ ! $REPLY =~ ^[Yy]$ ]]
|
||||||
|
then
|
||||||
|
echo "Please read README and rerun this script."
|
||||||
|
[[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
REMOTE_USER="root"
|
||||||
|
|
||||||
|
function check_if_running_as_root {
|
||||||
|
if [[ $EUID -ne 0 ]]; then
|
||||||
echo "This script must be run as root."
|
echo "This script must be run as root."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "OK: Root user detected."
|
echo "OK: Root user detected."
|
||||||
echo "NEXT: Checking for apt-get executable."
|
}
|
||||||
|
|
||||||
APT_GET_CMD=$(which apt-get)
|
function check_necessary_packages {
|
||||||
|
echo "NEXT: Checking for valid package manager."
|
||||||
|
|
||||||
if [[ -z $APT_GET_CMD ]]; then
|
APT_GET_CMD=$(which apt-get)
|
||||||
echo "No apt-get executable found. Exiting."
|
YUM_CMD=$(which yum)
|
||||||
|
|
||||||
|
if [[ ! -z $APT_GET_CMD ]]; then
|
||||||
|
echo "OK: apt-get found."
|
||||||
|
apt-get --yes install autossh
|
||||||
|
elif [[ ! -z $YUM_CMD ]]; then
|
||||||
|
echo "OK: yum found."
|
||||||
|
yum install autossh
|
||||||
|
else
|
||||||
|
echo "No valid package manager found. Exiting."
|
||||||
exit 1;
|
exit 1;
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "OK: apt-get found."
|
echo "OK: Autossh installed"
|
||||||
echo "NEXT: Check and eventually install necessary packages."
|
}
|
||||||
|
|
||||||
REQUIRED_PKG="autossh"
|
function check_for_autossh_user {
|
||||||
PKG_OK=$(dpkg-query -W --showformat='${Status}\n' $REQUIRED_PKG|grep "install ok installed")
|
echo "NEXT: Check for existence of autossh dedicated user."
|
||||||
|
|
||||||
echo Checking for $REQUIRED_PKG: $PKG_OK
|
if [ ! id -u autossh >/dev/null 2>&1 ]; then
|
||||||
|
|
||||||
if [ "" = "$PKG_OK" ]; then
|
|
||||||
echo "No $REQUIRED_PKG. Setting up $REQUIRED_PKG."
|
|
||||||
apt-get --yes install $REQUIRED_PKG
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "NEXT: Check for existence of autossh dedicated user."
|
|
||||||
|
|
||||||
if [ ! id -u autossh >/dev/null 2>&1 ]; then
|
|
||||||
echo "The user is missing so we will create for you."
|
echo "The user is missing so we will create for you."
|
||||||
useradd -m -s /bin/false autossh
|
useradd -m -s /bin/false autossh
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ ! id -u autossh >/dev/null 2>&1 ]; then
|
if [ ! id -u autossh >/dev/null 2>&1 ]; then
|
||||||
echo "There are some problems with user creation. Exiting."
|
echo "There are some problems with user creation. Exiting."
|
||||||
exit 1;
|
exit 1;
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
echo "NEXT: Setup autossh home."
|
function adjust_ssh_folder_for {
|
||||||
|
homedir=$( getent passwd $REMOTE_USER | cut -d: -f6 )
|
||||||
|
|
||||||
mkdir -p "/home/autossh/.ssh"
|
echo "NEXT: Setup ${1} home: ${homedir}."
|
||||||
touch -a /home/autossh/.ssh/authorized_keys
|
|
||||||
|
|
||||||
if [ ! -s authorized_keys ]; then
|
mkdir -p "${homedir}/.ssh"
|
||||||
|
touch -a $homedir/.ssh/authorized_keys
|
||||||
|
|
||||||
|
if [ ! -s authorized_keys ]; then
|
||||||
echo "WARNING: authorized_keys in setup folder seems empty so you should manually setup host authorized_keys or rerun this script."
|
echo "WARNING: authorized_keys in setup folder seems empty so you should manually setup host authorized_keys or rerun this script."
|
||||||
fi
|
fi
|
||||||
cat authorized_keys >> /home/autossh/.ssh/authorized_keys
|
cat authorized_keys >> $homedir/.ssh/authorized_keys
|
||||||
|
|
||||||
echo "OK: Files and content ready."
|
echo "OK: Files and content ready."
|
||||||
echo "NEXT: Setup file and folder permissions."
|
echo "NEXT: Setup file and folder permissions."
|
||||||
|
|
||||||
chown -R autossh:autossh /home/autossh/.ssh
|
chown -R $1:$1 $homedir/.ssh
|
||||||
chmod 700 /home/autossh/.ssh
|
chmod 700 $homedir/.ssh
|
||||||
chmod 600 /home/autossh/.ssh/authorized_keys
|
chmod 600 $homedir/.ssh/authorized_keys
|
||||||
|
|
||||||
echo "OK: File and folder permissions setup."
|
echo "OK: File and folder permissions setup."
|
||||||
echo "NEXT: Checking for systemd."
|
}
|
||||||
|
|
||||||
SYSTEMCTL_CMD=$(which systemctl)
|
function setup_systemd_service_if_available {
|
||||||
if [[ ! -z $SYSTEMCTL_CMD ]]; then
|
echo "NEXT: Checking for systemd."
|
||||||
|
|
||||||
|
SYSTEMCTL_CMD=$(which systemctl)
|
||||||
|
if [[ ! -z $SYSTEMCTL_CMD ]]; then
|
||||||
echo "NEXT: Copy targets into /etc/default."
|
echo "NEXT: Copy targets into /etc/default."
|
||||||
|
|
||||||
cp -n targets/* /etc/default/
|
cp -n targets/* /etc/default/
|
||||||
@ -73,9 +100,17 @@ if [[ ! -z $SYSTEMCTL_CMD ]]; then
|
|||||||
systemctl daemon-reload
|
systemctl daemon-reload
|
||||||
|
|
||||||
echo "OK: Systemd service created."
|
echo "OK: Systemd service created."
|
||||||
else
|
else
|
||||||
echo "WARNING: No systemd installation found. You should manually setup an autossh service to keep tunnel alive."
|
echo "WARNING: No systemd installation found. You should manually setup an autossh service to keep tunnel alive."
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
check_if_running_as_root
|
||||||
|
check_necessary_packages
|
||||||
|
check_for_autossh_user
|
||||||
|
|
||||||
|
adjust_ssh_folder_for ${REMOTE_USER}
|
||||||
|
setup_systemd_service_if_available
|
||||||
|
|
||||||
echo "All done. What you need to do now:\n"
|
echo "All done. What you need to do now:\n"
|
||||||
echo "- generate an ssh keypair with ssh-keygen for user autossh and push signature to the jump server"
|
echo "- generate an ssh keypair with ssh-keygen for user autossh and push signature to the jump server"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user