utils/ssh-pw.sh
2024-07-27 04:15:43 +05:00

56 lines
1.7 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
if [ "$(id -u)" != "0" ]; then
echo -e "\033[31mThis script requires superuser rights\033[0m"
exit 0
fi
# Проверка наличия одного аргумента
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <y/n>"
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
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
}
# Бэкапим текущий конфигурационный файл
sudo cp $SSHD_CONFIG $SSHD_CONFIG.backup
# Применение изменений
toggle_password_access "$1"
# Перезапуск службы sshd для применения изменений
sudo systemctl restart sshd
trap - EXIT
# Уведомление о завершении
if [ "$1" == "y" ]; then
echo "Password authentication has been enabled"
else
echo "Password authentication has been disabled"
fi