01-renew-ssh-keys 766 B

123456789101112131415161718192021222324252627282930313233343536
  1. #!/bin/bash
  2. # Licence : GPL v3
  3. # Author: Laurent HUBERT
  4. #
  5. # Renews the ssh server keys if they exist
  6. #
  7. cat <<EOF
  8. +---------------------------------------------+
  9. | Renewing SSH server keys |
  10. +---------------------------------------------+
  11. EOF
  12. status=0
  13. renew_ssh_keys(){
  14. rm /etc/ssh/ssh_host_*key*
  15. dpkg-reconfigure openssh-server
  16. status=$?
  17. }
  18. # Disables to avoid error messages
  19. systemctl disable ssh
  20. systemctl stop ssh
  21. ls /etc/ssh/ssh_host_*key* > /dev/null 2>&1 && renew_ssh_keys || status=1
  22. systemctl enable ssh
  23. systemctl start ssh
  24. if [ 0 -ne "$status" ]
  25. then
  26. # Exit with non zero status
  27. echo "### Error renewing SSH keys ###"
  28. exit 1
  29. else
  30. echo "> Renewing SSH server keys : OK <"
  31. systemctl start ssh
  32. exit 0
  33. fi