1
1
Fork 0
sconfig/sconfig.sh

248 lines
5.6 KiB
Bash
Executable File

#!/usr/bin/env bash
## name: sconfig.sh
## author: david@socialnerds.org
## version: 0.1
##
## decription: This is a script to help you configure
## various aspects of a linux server.
##
## features: manage user accounts and their keys
## update operating system packages
## install admin tools
## harden sshd
## enable ufw
## setup docker
## setup nginx & acme.sh
## setup duplicity
## setup snmpd
##
## usage: ./sconfig.sh
##
## changelog: 2020-06-29 - implemented basic features, not quite there yet
## 2020-06-22 - first draft of a working menu
## 2020-06-21 - started development of v0.1
# **** start of script ****
# **** bash trap initialisation ****
trap bashtrap INT
## functions
#detect Ctrl-c
bashtrap() {
header
echo "Detected Ctrl-C"; echo "Dropping to shell"; sleep 1
clear; exit 0
}
#print header
header() {
clear
local cols=$(tput cols); local char="="
if [ $cols -gt 59 ]; then
local i=0; while [ $i != $((cols)) ]; do local chars="$chars$char"; i=$((i+1)); done
echo $chars; echo "Server configuration"; echo -e "$chars\n"
else
echo "error: terminal not wide enough (it must be at least 60 characters wide)"; exit 1
fi
}
#print content
content() {
local lines=$(tput lines)
if [ $lines -gt 14 ]; then
for i in $(seq 1 $#); do
echo "${!i}"
done
local l=5; while [ $l != $((lines-$#)) ]; do echo ""; l=$((l+1)); done
else
clear
echo "error: terminal not high enough (it must be at least 15 lines high)"; exit 1
fi
}
#get user input
prompt() {
local input=""
case $1 in
password)
read -s -p "Enter [sudo] password: " input; echo $input
;;
string)
read -p "Enter string: " input; echo $input
;;
*)
read -n 1 -p "Enter number to select an option: " input
if [[ $input =~ ^[1-9]+$ ]]; then echo $input; fi
;;
esac
}
#get sudo
makemeasandwich()
{
while true; do
if $(sudo -n true 2>/dev/null); then
return 0
else
header
content "With great power comes great responsibility!"
password=$(prompt password)
echo "$password" | sudo -S -v 2>/dev/null
if [ $? -ne 0 ]; then
header
echo "Wrong password. Try again."; sleep 1
continue
fi
fi
done
}
#print results of system checks
status() {
pass
}
## run main loop
while true; do
# Main menu
header
content "1) System status" \
"2) Configuration" \
"3) Maintenance" \
"" \
"4) Reboot" \
"5) Shutdown" \
"6) Exit to shell"
case $(prompt) in
1)
header
echo "Not yet implemented"; sleep 1
;;
2)
while true; do
# Configuration submenu
header
content "1) Users" \
"2) Install admin utilities" \
"" \
"*) Return to main menu"
case $(prompt) in
1)
header
echo "Not yet implemented"; sleep 1
;;
2)
header
echo "Not yet implemented"; sleep 1
;;
*)
break
;;
esac
done
;;
3)
while true; do
# Maintenance submenu
header
content "1) System updates" \
"2) Backup" \
"3) Update this script" \
"" \
"*) Return to main menu"
case $(prompt) in
1)
#update system packages
makemeasandwich
if [ $? -eq 0 ]; then
clear; header; sudo apt update && sudo apt dist-upgrade -y && sudo apt autoremove -y && sudo apt autoclean -y
clear; header; echo "System is up-to-date"; sleep 1
fi
;;
2)
header
echo "Not yet implemented"; sleep 1
;;
3)
# Update this script submenu
header
#TODO: check for new version
if false; then #this should run the check
content "A new version of sconfig is available." \
"Do you want to update?" \
"" \
"1) Yes" \
"2) No"
if [ $(prompt) -eq 1 ]; then
#run self update
header
echo "Update not yet implemented"; sleep 1
fi
else
header
echo "You are already up-to-date."; sleep 1
fi
;;
*)
break
;;
esac
done
;;
4)
# Reboot submenu
header
content "1) Yes, i really want to reboot." \
"*) Get me out of here!"
case $(prompt) in
1)
#reboot server
makemeasandwich
if [ $? -eq 0 ]; then
header
echo "Rebooting server"; sleep 1
sudo shutdown -r now
fi
;;
*)
continue
;;
esac
;;
5)
# Shutdown submenu
header
content "1) Yes, i really want to shutdown." \
"*) Get me out of here!"
case $(prompt) in
1)
#shutdown server
makemeasandwich
if [ $? -eq 0 ]; then
header
echo "Shutting down server"; sleep 1
sudo shutdown -h now
fi
;;
*)
continue
;;
esac
;;
6)
clear; exit 0
;;
esac
done
# **** end of script ****
exit 0