utils/ssh-pw.sh
2024-08-05 07:01:48 +05:00

46 lines
1.1 KiB
Bash

#!/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
ENABLE_PASSWORD=$1
SSHD_CONFIG="/etc/ssh/sshd_config"
trap 'echo -e "\033[31mSomething went wrong\033[0m"; exit 1' ERR
set -e
echo "Backup $SSHD_CONFIG..."
cp $SSHD_CONFIG $SSHD_CONFIG.backup
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
echo "Restart SSH for apply password $1"
systemctl daemon-reload
systemctl restart ssh
trap - ERR
if [ "$1" == "y" ]; then
echo "Password authentication has been enabled"
else
echo "Password authentication has been disabled"
fi