Improved appearance. Added error checking.

This commit is contained in:
Doc
2025-12-14 21:03:24 -05:00
parent ff6e1ddeb3
commit ae8c5d9c3e

View File

@@ -9,33 +9,152 @@ if [[ "${UID}" -ne 0 ]]; then
exit 1 exit 1
fi fi
ANSIUSER=ansiuser ANSIUSER=${1:-ansiuser}
ANSIUSERDIR=/home/$ANSIUSER ANSIUSERDIR=/home/$ANSIUSER
TMP_PORT=46347 TMP_PORT=${2:-46347}
cat << EOF
#####################
# Updating System #
#####################
EOF
# Update to current # Update to current
apt update && apt upgrade -y apt update && apt upgrade -y
err=$?
# Was there an error
if [ $err -ne 0 ]; then
cat << EOF
# Install requirements ###########################################
apt install -y --no-install-recommends openssh-client openssh-server sudo git curl wget # # # # # # # # # # # # # # # # # # # # # #
### ERROR while updating the system! ###
# # # # # # # # # # # # # # # # # # # # # #
###########################################
# Create a user for Ansible Correct the issues and try again. Exiting.
EOF
exit $err
fi
cat << EOF
#############################
# Installing ssh and sudo #
#############################
EOF
apt install -y --no-install-recommends openssh-client openssh-server sudo
err=$?
if [ $err -ne 0 ]; then
cat << EOF
###############################################
# # # # # # # # # # # # # # # # # # # # # # # #
### ERROR while installing ssh and sudo! ###
# # # # # # # # # # # # # # # # # # # # # # # #
###############################################
Correct the issues and try again. Exiting.
EOF
exit $err
fi
cat << EOF
###################################
# Creating and configuring user #
###################################
EOF
echo -e "- Creating user '$ANSIUSER'..."
useradd -m -s /bin/bash -c "Ansible User" $ANSIUSER useradd -m -s /bin/bash -c "Ansible User" $ANSIUSER
echo "Configuring sudo for user $ANSIUSER" echo "- Configuring sudo for user '$ANSIUSER'..."
usermod -aG sudo $ANSIUSER usermod -aG sudo $ANSIUSER
mkdir -p /etc/sudoers.d mkdir -p /etc/sudoers.d
cat << EOF > /etc/sudoers.d/99-ansible-user cat << EOF > /etc/sudoers.d/99-ansible-user
$ANSIUSER ALL=(ALL) NOPASSWD:ALL $ANSIUSER ALL=(ALL) NOPASSWD:ALL
EOF EOF
echo "" echo ""
mkdir -p $ANSIUSERDIR/.ssh mkdir -p $ANSIUSERDIR/.ssh
# Prompt to paste public key
echo "Paste public key for $ANSIUSER. Ctl+d when done." ; cat >> $ANSIUSERDIR/.ssh/authorized_keys
echo ""
echo "Configuring ssh..." # Prompt to paste public key
cat << EOF
########################################################
# IMPORTANT! | #
#-------------+ #
# #
# Past public key for '$ANSIUSER'. #
# Ctrl+d when done. #
# #
########################################################
EOF
if [ -f "$ANSIUSERDIR/.ssh/authorized_keys" ]; then
BEFORESZ=$(wc -c "$ANSIUSERDIR/.ssh/authorized_keys")
else
BEFORESZ=0
fi
cat >> $ANSIUSERDIR/.ssh/authorized_keys
err=$?
if [ -f "$ANSIUSERDIR/.ssh/authorized_keys" ]; then
AFTERSZ=$(wc -c "$ANSIUSERDIR/.ssh/authorized_keys")
if [ ! $AFTERSZ -gt $BEFORESZ ]; then
cat << EOF
###############################################
# # # # # # # # # # # # # # # # # # # # # # # #
### ERROR authorized_keys did not change ###
# # # # # # # # # # # # # # # # # # # # # # # #
###############################################
Exiting!
EOF
exit $?
fi
else
cat << EOF
#####################################################
# # # # # # # # # # # # # # # # # # # # # # # # # # #
### ERROR could not append to authorized_keys! ###
# # # # # # # # # # # # # # # # # # # # # # # # # # #
#####################################################
Exiting!
EOF
exit $err
fi
cat << EOF
###################################
# (Re)setting SSH configuration #
###################################
EOF
chown -Rc ${ANSIUSER}:${ANSIUSER} $ANSIUSERDIR/.ssh chown -Rc ${ANSIUSER}:${ANSIUSER} $ANSIUSERDIR/.ssh
chmod 700 $ANSIUSERDIR/.ssh && chmod 600 $ANSIUSERDIR/.ssh/authorized_keys chmod 700 $ANSIUSERDIR/.ssh && chmod 600 $ANSIUSERDIR/.ssh/authorized_keys
@@ -61,20 +180,43 @@ cat << EOF > /etc/ssh/sshd_config.d/enable_$ANSIUSER.conf
AllowUsers $ANSIUSER AllowUsers $ANSIUSER
EOF EOF
cat << EOF
################################
# Opening a port (if needed) #
################################
EOF
if command -v ufw &> /dev/null; then if command -v ufw &> /dev/null; then
echo "Opening port $TMP_PORT with ufw..." echo "Opening port $TMP_PORT with ufw..."
ufw allow $TMP_PORT/tcp comment 'Allow temporary SSH port' ufw allow $TMP_PORT/tcp comment 'Allow temporary SSH port' || echo "## WARNING error occurred while openning a port ##"
elif command -v firewall-cmd &> /dev/null; then elif command -v firewall-cmd &> /dev/null; then
echo "Opening port $TMP_PORT with firewalld..." echo "Opening port $TMP_PORT with firewalld..."
firewall-cmd --permanent --add-port=$TMP_PORT/tcp firewall-cmd --permanent --add-port=$TMP_PORT/tcp || echo "## WARNING error occurred while openning a port ##"
firewall-cmd --reload firewall-cmd --reload
elif command -v iptables &> /dev/null; then elif command -v iptables &> /dev/null; then
echo "Temporarily opening port $TMP_PORT with iptables (this session only)..." echo "Temporarily opening port $TMP_PORT with iptables (this session only)..."
iptables -I INPUT -p tcp --dport $TMP_PORT -j ACCEPT iptables -I INPUT -p tcp --dport $TMP_PORT -j ACCEPT || echo "## WARNING error occurred while openning a port ##"
fi fi
echo "User: $ANSIUSER" cat << EOF
echo "Port: $TMP_PORT"
###################################
# Complete | #
#-----------+ #
# #
# Process completed. #
# After action summary below: #
# #
###################################
EOF
echo " - User: $ANSIUSER"
echo " - Port: $TMP_PORT"
echo "" echo ""
# Partially redact authorized_keys # Partially redact authorized_keys
@@ -83,6 +225,8 @@ grep -Poi 'ssh\-.*' $ANSIUSERDIR/.ssh/authorized_keys | awk '{ print $1, substr(
echo "-----------------------" echo "-----------------------"
echo "" echo ""
echo "Restarting SSH server and ending script" echo "Restarting SSH server in 10 seconds and ending script"
sleep 10
systemctl enable sshd
systemctl restart sshd systemctl restart sshd