From c913d1780be378ac69280a418e5f129e7cce6205 Mon Sep 17 00:00:00 2001 From: leo <426742@gmail.com> Date: Mon, 5 Aug 2024 07:01:48 +0500 Subject: [PATCH] changes --- init-server.sh | 2 +- ssh-pw.sh | 42 ++++++++++++++++-------------------------- 2 files changed, 17 insertions(+), 27 deletions(-) diff --git a/init-server.sh b/init-server.sh index d931359..bf9de65 100644 --- a/init-server.sh +++ b/init-server.sh @@ -14,7 +14,7 @@ set -e export DEBIAN_FRONTEND=noninteractive echo "Upgrade packages..." -apt-get update -y +apt-get update apt-get upgrade -y echo "Install apps..." diff --git a/ssh-pw.sh b/ssh-pw.sh index cdb05fa..c0a2574 100644 --- a/ssh-pw.sh +++ b/ssh-pw.sh @@ -5,49 +5,39 @@ if [ "$(id -u)" != "0" ]; then exit 0 fi -# Проверка наличия одного аргумента if [ "$#" -ne 1 ]; then echo "Usage: $0 " exit 1 fi -# Проверка аргумента if [ "$1" != "y" ] && [ "$1" != "n" ]; then echo "Invalid argument. Use 'y' to enable password authentication and 'n' to disable it" exit 1 fi -trap 'echo -e "\033[31mSomething went wrong\033[0m"; exit 1' EXIT -set -e - -# Путь к конфигурационному файлу sshd +ENABLE_PASSWORD=$1 SSHD_CONFIG="/etc/ssh/sshd_config" -# Функция включения или отключения доступа по паролю -toggle_password_access() { - local enable=$1 - if [ "$enable" == "y" ]; then - sudo sed -i 's/^#PasswordAuthentication yes/PasswordAuthentication yes/' $SSHD_CONFIG - sudo sed -i 's/^PasswordAuthentication no/PasswordAuthentication yes/' $SSHD_CONFIG - else - sudo sed -i 's/^#PasswordAuthentication yes/PasswordAuthentication no/' $SSHD_CONFIG - sudo sed -i 's/^PasswordAuthentication yes/PasswordAuthentication no/' $SSHD_CONFIG - fi -} +trap 'echo -e "\033[31mSomething went wrong\033[0m"; exit 1' ERR +set -e -# Бэкапим текущий конфигурационный файл -sudo cp $SSHD_CONFIG $SSHD_CONFIG.backup +echo "Backup $SSHD_CONFIG..." +cp $SSHD_CONFIG $SSHD_CONFIG.backup -# Применение изменений -toggle_password_access "$1" +if [ "$ENABLE_PASSWORD" == "y" ]; then + sed -i 's/^#PasswordAuthentication yes/PasswordAuthentication yes/' $SSHD_CONFIG + sed -i 's/^PasswordAuthentication no/PasswordAuthentication yes/' $SSHD_CONFIG +else + sed -i 's/^#PasswordAuthentication yes/PasswordAuthentication no/' $SSHD_CONFIG + sed -i 's/^PasswordAuthentication yes/PasswordAuthentication no/' $SSHD_CONFIG +fi -# Перезапуск службы sshd для применения изменений -sudo systemctl restart sshd +echo "Restart SSH for apply password $1" +systemctl daemon-reload +systemctl restart ssh +trap - ERR -trap - EXIT - -# Уведомление о завершении if [ "$1" == "y" ]; then echo "Password authentication has been enabled" else